pymysql的连接

import pymysql
username=input('请输入用户名:')
pwd=input('请输入密码:')
pymysql.connect()
# 创建连接
conn=pymysql.connect(
    host='127.0.0.1',
    # localhost
    user='root',
    password='',
    database='db2',
    port=3306,
    charset='utf8'
)
# 创建游标
cur=conn.cursor()
# 使用列表,元组传时
# sql = "select * from userinfo where name=%s and pwd=%s"
# 使用字典传
sql = "select * from userinfo where name=%(name)s and pwd=%(pwd)s"
print(sql)
# 使用列表传sql
# res=cur.execute(sql,[username,pwd])
# 使用元组传sql
# res=cur.execute(sql,(username,pwd))
# 使用字典
res=cur.execute(sql,{'name':username,'pwd':pwd})
print(res)
# 关闭游标
cur.close()
conn.close()
if res:
    print('登录成功')
else:
    print('登录失败')

 

posted @ 2019-08-30 10:15  你我皆牛马  阅读(531)  评论(0编辑  收藏  举报