操作mysql(import pymysql模块)

pymysql模块
import pymysql

#1、连上数据库、账号、密码、ip、端口号、数据库
#2、建立游标
#3、执行sql
#4、获取结果
#5、关闭游标
#6、连接关闭
#charest必须写utf8

FI = pymysql.connect(
   host='192.168.0.12',user='root',passwd='myjcyf',
   port=3306,db='us_sys',charset='utf8'
   #port必须写int类型,
   #charset这里必须写utf8
)
cur = FI.cursor() #建立游标
sql = "select * from us_sys.tb_sys_person where pers_name like '%于%'"
cur.execute(sql)
res = cur.fetchall()  #获取所有返回的结果
print(res)
for i in res:
   print(i)
# select 不需要提交操作
# delete update insert
# coon.commit()  #必须得commit
mysql函数

# def FI(host,user,passwd,db,sql,port=3306,charset='utf8'):
#     import pymysql
#     coon=pymysql.connect(user=users,host=host,passwd=passwd,port=port,db=db,charset=charset)
#     cur=coon.connect()#建立游标
#     cur.execute(sql)#执行sql
#     if sql.strip()[:6].upper()=='SELECT':
#         res=cur.fetchall()
#     else:
#         coon.commit()
#         res='OK'
#     cur.colse
#     coon.colse
#     return res

 

 

转载于:https://www.cnblogs.com/ruijie/p/10407001.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值