import pymysql
host = "IP地址"
username="用户名"
password="密码"
db="数据库名"
port=3306
charset="utf8"
conn = pymysql.connect(host=host,user=username,
passwd=password,db=db,port=port,charset=charset,autocommit=True)
cur = conn.cursor(pymysql.cursors.DictCursor)#游标
uname = input("username:")
age = input("age:")
sql = "select * from students3_info where name = '%s'" % uname
#sql = "select * from students3_info where name = %s and age = %s"
#sql注入
#insert_sql = "insert into students3_info (name,age,password) values ('周ahha',18,'sdfsfsfd');"
#update_sql = "update students3_info set name='xxx_xiugai' where id=3;"
#delete_sql = "delete from students3_info where id = 5; "
# cur.execute(sql,[uname,age]) #只是用来执行sql
cur.execute(sql) #只是用来执行sql
#conn.commit()
#conn.rollback()#回滚
# print(cur.fetchone()) #
print(cur.fetchone()) #
# print(cur.fetchall()) #所有的结果
print(cur.rowcount) #查询结果总共多少条
# print(cur.fetchmany(2))
# print(cur.fetchmany(2))
# for c in cur: #执行循环游标,也是能把它里面所有的数据都获取到
# print(c)
cur.close()
conn.close()
Python 操作mysql(pymysql)
最新推荐文章于 2024-07-15 12:05:45 发布