1. 程式人生 > >python操作mysql資料庫

python操作mysql資料庫

安裝mysql-connector

import mysql.connector
config
{
 'host':'127.0.0.1',
 'port':'3306',
 'user':'usr',
 'password':'pwd',
 'charset':'utf8'
}

try:
    conn =mysql.connector.connect(**config)
    cur=conn.cursor()
    strSql="select id,name from t"
    cur.excute(strSql)
    for id,name in cur:
        print (id,name)
except Exception as e:
    print (e)
finally:
    cur.close()
    conn.close()