import pymysql
user = input("用户名:").strip()
pwd = input("密码:").strip()
# 链接
conn = pymysql.connect(host="localhost", user="root", password="123456", database="ias",
charset="utf8")
# host -- 数据库IP地址
# user -- 用户名
# password -- 用户名对应的密码
# database -- 数据库名称
# charset -- 编码设置
cursor = conn.cursor()
sql = "select * from userinfo where user=%s and password=%s "
res = cursor.execute(sql, [user, pwd])
sql = "insert into userinfo (user,password,age)values (%s,%s,%s);"
res1 = cursor.executemany(sql,[("ai","123",34),("ni","234",46)])
sql="insert into userinfo(user,password,age) values('root','123456',34);"
res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数
print(res)
sql = "delete from userinfo where user = %s"
res =cursor.execute(sql,"suo")
sql = "update userinfo set password =222 where password = %s"
res = cursor.execute(sql, "123")
sql = "select * from userinfo;"
res = cursor.execute(sql)
res2 = cursor.fetchone()
只查询出一条记录
res3 = cursor.fetchmany(3)
查询出3条记录
res1 = cursor.fetchall()
查询出所有的记录
conn.commit() # 增删改必须提交,数据库表中才会有对应的记录
cursor.close() # 释放资源
conn.close() # 释放资源
Python之MySql的JDBC
最新推荐文章于 2024-04-15 10:45:39 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)