首页 > 数据库技术 > 详细

pymysql模块常用操作

时间:2019-08-31 01:04:27      阅读:90      评论:0      收藏:0      [点我收藏+]

pymysql安装

pip install pymysql

链接数据库、执行sql、关闭连接

import pymysql
user = input('请输入用户名请输入密码:').strip()
pwd= input("请输入密码:").strip()

# 建立连接
conn = pymysql.connect(
    host = '192.168.1.1',
    port = '3306',
    user = 'root',
    password = '123',
    db = 'myTestDB',
    charset = 'utf8',
)

# 获取游标
cursor = conn.cursor()

# 执行sql语句
# sql = 'select * from USER_TABLE where user="%s" and pwd=%s' % (user,pwd) 自己拼接sql语句有安全风险
# rows = cursor.excute(sql)
sql = 'select * from USER_TABLE where user="%s" and pwd=%s'
rows = cursor.excute(sql,(user,pwd))
#插入多个值
rows = cursor.excutemany(sql,[(user,pwd),('aaa','123'),('bbb','123')])
conn.commit()

cursor.close()
conn.close()

if rows:
    print("登录成功")
else:
    print("登录失败")

pymysql模块常用操作

原文:https://www.cnblogs.com/SebastianHan/p/11437705.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!