Connection對象
.用于建立與數(shù)據(jù)庫的鏈接
.創(chuàng)建對象:調(diào)用connect()方法
conn=connect(參數(shù)列表)
.參數(shù)host:連接的mysql主機丐黄,如果本機是'localhost'
.參數(shù)port:連接的mysql主機的端口罐栈,默認是3306
.參數(shù)db:數(shù)據(jù)庫的名稱
.參數(shù)user:連接的用戶名
.參數(shù)password:連接的密碼
.參數(shù)charset:通信采用的編碼方式二汛,默認是'gb2312',要求與數(shù)據(jù)庫創(chuàng)建時指定的編碼一致租谈,否則中文會亂碼
對象的屬性
rowcount只讀屬性凛澎,表示最近一次execute()執(zhí)行后受影響的行數(shù)
connection獲得當前連接對象
增加數(shù)據(jù)
count=cs1.execute("insert?into?students(sname)values('張三')")
修改數(shù)據(jù)
conut=cs1.execute("update?students?set?sname='李四'?where?id=6")
刪除數(shù)據(jù)
conte=cs1.execute("delete?from?students?where?id=6")
查詢數(shù)據(jù)
查詢一行數(shù)據(jù):cur.execute('select?*?from?students?(表名)?where?(條件)?id=7')??result=cur.fetchone()
查詢多行數(shù)據(jù):?cur.execute('select?*?from?students?(表名)?')?result=cur.fetchall()
導入數(shù)據(jù)庫模塊
import?pymysql
連接數(shù)據(jù)庫
db?=?pymysql.connect("MySQL地址","賬號","密碼","d1")
創(chuàng)建數(shù)據(jù)庫表
使用預(yù)處理語句創(chuàng)建表
sql?=?"""CREATE?TABLE?STUDENTS?()
數(shù)據(jù)庫的插入操作
#SQL插入語句
sql?=?"""INSERT?INTO?EMPLOYEE?()