How do I connect to a MySQL Database in Python?

后端 未结 25 1507
眼角桃花
眼角桃花 2020-11-21 07:43

How do I connect to a MySQL database using a python program?

相关标签:
25条回答
  • 2020-11-21 08:00

    you can connect your python code to mysql in this way.

    import MySQLdb
    db = MySQLdb.connect(host="localhost",
                     user="appuser",
                     passwd="",
                     db="onco")
    
    cursor = db.cursor()
    
    0 讨论(0)
  • 2020-11-21 08:02

    For python 3.3

    CyMySQL https://github.com/nakagami/CyMySQL

    I have pip installed on my windows 7, just pip install cymysql

    (you don't need cython) quick and painless

    0 讨论(0)
  • 2020-11-21 08:03

    Here's one way to do it, using MySQLdb, which only supports Python 2:

    #!/usr/bin/python
    import MySQLdb
    
    # Connect
    db = MySQLdb.connect(host="localhost",
                         user="appuser",
                         passwd="",
                         db="onco")
    
    cursor = db.cursor()
    
    # Execute SQL select statement
    cursor.execute("SELECT * FROM location")
    
    # Commit your changes if writing
    # In this case, we are only reading data
    # db.commit()
    
    # Get the number of rows in the resultset
    numrows = cursor.rowcount
    
    # Get and display one row at a time
    for x in range(0, numrows):
        row = cursor.fetchone()
        print row[0], "-->", row[1]
    
    # Close the connection
    db.close()
    

    Reference here

    0 讨论(0)
  • 2020-11-21 08:03

    As a db driver, there is also oursql. Some of the reasons listed on that link, which say why oursql is better:

    • oursql has real parameterization, sending the SQL and data to MySQL completely separately.
    • oursql allows text or binary data to be streamed into the database and streamed out of the database, instead of requiring everything to be buffered in the client.
    • oursql can both insert rows lazily and fetch rows lazily.
    • oursql has unicode support on by default.
    • oursql supports python 2.4 through 2.7 without any deprecation warnings on 2.6+ (see PEP 218) and without completely failing on 2.7 (see PEP 328).
    • oursql runs natively on python 3.x.

    So how to connect to mysql with oursql?

    Very similar to mysqldb:

    import oursql
    
    db_connection = oursql.connect(host='127.0.0.1',user='foo',passwd='foobar',db='db_name')
    cur=db_connection.cursor()
    cur.execute("SELECT * FROM `tbl_name`")
    for row in cur.fetchall():
        print row[0]
    

    The tutorial in the documentation is pretty decent.

    And of course for ORM SQLAlchemy is a good choice, as already mentioned in the other answers.

    0 讨论(0)
  • 2020-11-21 08:06

    Just a modification in above answer. Simply run this command to install mysql for python

    sudo yum install MySQL-python
    sudo apt-get install MySQL-python
    

    remember! It is case sensitive.

    0 讨论(0)
  • 2020-11-21 08:07

    Try using MySQLdb. MySQLdb only supports Python 2.

    There is a how to page here: http://www.kitebird.com/articles/pydbapi.html


    From the page:

    # server_version.py - retrieve and display database server version
    
    import MySQLdb
    
    conn = MySQLdb.connect (host = "localhost",
                            user = "testuser",
                            passwd = "testpass",
                            db = "test")
    cursor = conn.cursor ()
    cursor.execute ("SELECT VERSION()")
    row = cursor.fetchone ()
    print "server version:", row[0]
    cursor.close ()
    conn.close ()
    
    0 讨论(0)
提交回复
热议问题