一、查询语句
pymsql.cursors的应用
https://blog.csdn.net/tcy23456/article/details/86665211
# 使用pymysql,设计登录 import pymysql user_name = input('username:') user_password = input('password:') conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db2') # 连接数据库 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 设置游标卡尺和fetc的格式,以字典的形式返回结果 sql = "select * from userinfo where username =%s and password = %s" # sql语句,注意在此次不能用格式化,——在sql语句是注释 cursor.execute(sql, [user_name, user_password]) # list tuple dict ret = cursor.fetchone() # 取一个值 # print(ret) cursor.close() # 关闭游标卡尺 conn.close() # 关闭连接 if ret: print('登录成功!') else: print('登录失败')
import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db2') # 连接数据库 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 设置游标卡尺和fetc的格式 sql = "select * from userinfo limit 3" # sql语句,分页 cursor.execute(sql) # list tuple dict ret = cursor.fetchall() # 取所有值 print(ret) cursor.close() # 关闭游标卡尺 conn.close() # 关闭连接
import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db2') # 连接数据库 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 设置游标卡尺和fetc的格式 sql = "select * from userinfo limit 3" # sql语句,分页 cursor.execute(sql) # list tuple dict cursor.scroll(1, mode='absolute') # 光标到1,默认的模式是相对 ret = cursor.fetchall() # 取所有值 print(ret) cursor.close() # 关闭游标卡尺 conn.close() # 关闭连接
模糊查询
"select id from tp_region where title like '%{}%' ".format(cityname)
二、插入语句
# 插入一条数据 import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db2') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "insert into userinfo(username, password) values(%s,%s) " cursor.execute(sql, ['joker', '1234']) conn.commit() # 提交 cursor.close() conn.close()
import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db2') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "insert into userinfo(username, password) values(%s, %s)" ret = cursor.executemany(sql, [('peter', '123'), ('talk', '234')]) # 连续添加多个数据,ret 为受影响的行数 conn.commit() print(ret) cursor.close() conn.close()
# 获取自增值 import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db2') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "insert into userinfo(username, password) values(%s, %s)" ret = cursor.execute(sql, ['talk', '234']) # 连续添加多个数据,ret 为受影响的行数 conn.commit() print(cursor.lastrowid) # 获取自增值 print(ret) cursor.close() conn.close()
三、改
import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db3', charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = 'update test4 set age =%s where name=%s' cursor.execute(sql, [20, 'tom']) conn.commit() sql1 = 'select * from test4' cursor.execute(sql1) ret = cursor.fetchall() print(ret) cursor.close() conn.close()
四、删
import pymysql conn = pymysql.connect(host='localhost', user='root', password='@WSX3edc', database='db3', charset='utf8') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = 'delete from test4 where name = %s' cursor.execute(sql, 'tom') conn.commit() sql1 = 'select * from test4' cursor.execute(sql1) ret = cursor.fetchall() print(ret) cursor.close() conn.close()