注:用到的python庫 pymysql
1.導(dǎo)入pymysql庫:????import pymysql
2.打開數(shù)據(jù)庫連接:????db = pymysql.connect("服務(wù)器","用戶名","密碼","數(shù)據(jù)庫",charset="utf8")
3.使用 cursor() 方法創(chuàng)建一個游標對象 cursor: ? cursor=db.cursor()
4.使用 execute() 方法執(zhí)行 SQL 查詢: ? ?cursor.execute(sql) ??
5.使用 fetchall()方法獲取數(shù)據(jù) \ fetchone() 方法獲取單條數(shù)據(jù): ? ?data = cor.fetchall()
(注:更新數(shù)據(jù)庫時需要此步)提交到數(shù)據(jù)庫執(zhí)行:db.commit()
6.關(guān)閉數(shù)據(jù)庫連接: ? ?db.close()