MYSQL——pymysql模块
1、基本规则及其input命令
import pymysqlconn
=pymysql.connect(host="127.0.0.1",port=3306,user="root",password="123",database="db4",charset="utf8mb4")#游标cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
# cursor.execute("insert into user(name,pwd) values("egon","123"),("tom","456"),("jack","111");")
# sql="insert into user(name,pwd) values("%s","%s");" %("lili","123")
# cursor.execute(sql)
# %s不要加引号
# cursor.execute("insert into user(name,pwd) values(%s,%s);",("kkk","123"))
username = input("username>>>: ").strip()
password = input("password>>>: ").strip()
# sql = "select * from user where name="%s" and pwd="%s"" %(username,password)
# select * from user where name="egon" -- hello" and pwd="%s"
# select * from user where name="xxx" or 1=1 -- hello" and pwd="%s";
# rows=cursor.execute(sql)
rows=cursor.execute("select * from user where name=%s and pwd=%s",(username,password))
if rows:
print("ok")
else:
print("no")
conn.commit()
cursor.close()
conn.close()
2、用法
import pymysql # pip3 install pymysqlconn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123", db="db13", charset="utf8mb4")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 记录=》增删改
# cursor.execute("insert into student values(%s,%s)",[5,"kkk"])
# cursor.executemany("insert into student values(%s,%s)",[(6,"xxx"),(7,"yyy"),(8,"zzzz")])
cursor.execute("insert into student(name) values(%s)",["zzz"])
print(cursor.lastrowid)
# cursor.execute("delete from student where id > 6;")
# 记录=》查
# rows = cursor.execute("select * from student;")
# print(rows)
# line = cursor.fetchone()
# print(line)
# line = cursor.fetchone()
# print(line)
#
# lines = cursor.fetchmany(2)
# print(lines)
# lines = cursor.fetchall()
# print(lines)
# for line in cursor:
# print(line)
# cursor.scroll(3,mode="absolute") # 相对绝对位置移动
# print(cursor.fetchone())
# print(cursor.fetchall())
# cursor.scroll(0,mode="absolute") # 相对绝对位置移动
# print(cursor.fetchall())
# cursor.fetchone()
# cursor.scroll(1,mode="relative") # 相对当前位置移动
# cursor.scroll(0,mode="absolute") # 相对当前位置移动
# print(cursor.fetchone())
conn.commit()
cursor.close()
conn.close()
----46---
以上是 MYSQL——pymysql模块 的全部内容, 来源链接: utcz.com/z/530382.html