import pymsqlmysql
conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='123456',database='db3',charset='utf8')
cursor = conn.cursor() data = [('小明',123),('小花',456)] sql = "insert into user(name,password) values(%s,%s);"
cursor.execute(sql,['王五','888'])
批量執行多條sql語句 cursor.executemany(sql,data)
print(cursor.lastrowid)
conn.commit()
cursor.close() conn.close()
sql = "delete from user where name=%s;" cursor.execute(sql,['小明'])
sql = "update user set password=%s where name=%s;" cursor.execute(sql,['888','張三'])
以字典的格式返回數據
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)sql
sql = "select * from user;" cursor.execute(sql)
ret = cursor.fetchall()
ret = cursor.fetchone()
ret = fetchmany(2)
try: cursor.execute(sql) conn.commit() except Exception as e: conn.rollback()