加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在python中使用pymysql连接数据库

发布时间:2020-12-20 10:53:44 所属栏目:Python 来源:网络整理
导读:# 导入pymysql import pymysql # 连接服务端 conn = pymysql.connect( host = ‘ 127.0.0.1 ‘ , # MySQL地址 port = 3306, # MySQL端口 user = ‘ root ‘ , # MySQL用户名 password = ‘ 123 ‘ , # MySQL密码 db = ‘test ‘ , # 选库 charset = ‘ utf8
# 导入pymysql
import pymysql

# 连接服务端
conn = pymysql.connect(
    host = 127.0.0.1,# MySQL地址
    port = 3306,# MySQL端口
    user = root,# MySQL用户名
    password = 123,#MySQL密码
    db = ‘test,# 选库
    charset = utf8,# 设置字符编码
    autocommit = True     # 设置增删改后自动提交
)

# 创建游标,并设置返回值为字典
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

#用户输入
user_name = input(user_name:).strip()
pwd = input(pwd:).strip()

# 拼接sql语句
sql = select * from user_info where name=%s and pwd=%s

# 执行sql语句
cursor.execute(sql,(user_name,pwd))

# 接收返回值
data = cursor.fetchone()

# 判断返回值
if data:
    print(登录成功)
else:
    print(登录失败)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读