Python 操作mysql(pymysql)

 

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值