1. 基本流程
2. 連接到數(shù)據(jù)庫(kù)
3. connection對(duì)象的方法
4. cursor對(duì)象的方法
5. 通用的示例代碼
# -*- coding: utf-8 -*-
# @Time : 2017/10/31 13:43
# @Author : 李紹俊
# @Email : 188792829@qq.com
# @File : testMySQL.py
import MySQLdb as con
cnx = con.connect(host = 'localhost',
port = 3306,
user='root',
passwd='supermap',
database='test03',
charset = 'utf8')
cur = cnx.cursor()
tbName = '員工表'
try:
cur.execute(f"drop table if EXISTS {tbName}")
cur.execute(f"create table {tbName}(編號(hào) varchar(10), b int, c int)")
for i in range(200):
strSQL = f"insert into {tbName} values('中文測(cè)試',{i},{i})"
cur.execute(strSQL)
cnx.commit()
except Exception as e:
print(e)
cnx.rollback()
cur.execute(f"select * from {tbName}")
# for row in cur.fetchall():
# print(row)
print(cur.fetchone())
print(cur.fetchone())
print(cur.fetchone())
print(cur.fetchmany(3))
print(cur.fetchall())
cur.close()
cnx.close()
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者