How do I connect to a MySQL Database in Python?

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

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

25条回答
  •  天涯浪人
    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 ()
    

提交回复
热议问题