Python 3.2 script to connect to local MySQL database

前端 未结 3 1815
無奈伤痛
無奈伤痛 2021-01-21 17:24

I am running an Ubuntu Server. I would like for it to have a Python (v3.2) CGI script that would connect, and run a query, to the local MySQL database I have set up. Currently,

3条回答
  •  鱼传尺愫
    2021-01-21 17:43

    pymysql - Pure Python MySQL client is very good.
    It works with Python 3.x, and doesn't have any dependencies.

    This pure Python MySQL client provides a DB-API to a MySQL database by talking directly to the server via the binary client/server protocol.

    Example:

    import pymysql
    conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql')
    cur = conn.cursor()
    cur.execute("SELECT Host,User FROM user")
    for r in cur:
        print(r)
    cur.close()
    conn.close()
    

提交回复
热议问题