python 学习_第四模块 python 对mysql的操作
python 学习_第四模块 python 对mysql的操作
1 查询
import pymysql user="egon1" pwd="123" conn = pymysql.connect( host="10.0.0.87", user="test", password="123456", database="db10", charset="utf8" ) cursor = conn.cursor() sql = "select * from test1 where name=%s and password=%s" res = cursor.execute(sql,[user,pwd]) if res: print("ok") else: print("error") cursor.close() conn.close()
2. 插入 修改 删除
import pymysql conn = pymysql.connect( host="10.0.0.87", user="test", password="123456", database="db10", charset="utf8") # 游标 cursor = conn.cursor() sql = 'insert into test1(name,password) value("ysl","111");' res = cursor.execute(sql) print(res) conn.commit() cursor.close() conn.close()
import pymysql conn = pymysql.connect( host="10.0.0.87", user="test", password="123456", database="db10", charset="utf8") # 游标 cursor = conn.cursor() sql = 'insert into test1(name,password) value(%s,%s);' cursor.execute(sql,['yy','666']) conn.commit() cursor.close() conn.cursor()
import pymysql conn = pymysql.connect( host="10.0.0.87", user="test", password="123456", database="db10", charset="utf8") # 游标 cursor = conn.cursor() sql = 'insert into test1(name,password) value(%s,%s);' cursor.executemany(sql,[("y1",'1'),("y2",'2'),("y3",'3'),("y4",'4')]) conn.commit() cursor.close() conn.cursor()
3. 查询
import pymysql conn = pymysql.connect( host="10.0.0.87", user="test", password="123456", database="db10", charset="utf8") # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor = conn.cursor() sql='select * from test1;' rows=cursor.execute(sql) # cursor.scroll(3,mode='absolute') # 相对绝对位置移动 # cursor.scroll(3,mode='relative') # 相对当前位置移动 #res1=cursor.fetchone() # res3=cursor.fetchone() # res4=cursor.fetchmany(2) res5=cursor.fetchall() print(res5) cursor.close() conn.close()
posted on 2019-07-08 14:57 augustyang 阅读(181) 评论(0) 编辑 收藏 举报