import pymysql
DBHOST = '' #數(shù)據(jù)庫地址
DBUSER = '' #數(shù)據(jù)庫用戶名
DBPASS = '' #數(shù)據(jù)庫密碼
DBNAME = '' #數(shù)據(jù)庫名稱
#連接數(shù)據(jù)庫
dp = pymysql.connect(host=DBHOST,user=DBUSER,password=DBPASS,database=DBNAME)
#創(chuàng)建游標(biāo)
con = dp.cursor()
#查
con.execute()
res = con.fetchall()
# fetchall 查詢時(shí)獲取結(jié)果集中的所有行,一行構(gòu)成一個(gè)元組,然后再將這些元組返回(即嵌套元組)。
# ferchone 查詢時(shí)獲取結(jié)果集的第一行數(shù)據(jù),返回一個(gè)元組,該元組元素即為第一行數(shù)據(jù),如果沒有則為null(注:在python中使用應(yīng)為None)
# execute 為單條數(shù)據(jù)插入
# executemany 批量數(shù)據(jù)插入
# cursor.rowcount 用來記錄操作次數(shù)
#增
con.executemany()
dp.commit()
#刪
con.execute()
dp.commit()
#改
con.execute()
dp.commit()
#返回錯(cuò)誤
dp.rollback()
#關(guān)閉
con.close()
dp.close( )