Python3链接MySQL数据库之基本方法
作者:互联网
# -*- coding: UTF-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect("localhost", "登录用户名", "登录密码", "数据库名", charset='utf8' ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行SQL语句 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取一条数据 data = cursor.fetchone() print("Database version : %s " % data) # 关闭数据库连接 db.close()
运行结果如下:
标签:data,数据库,db,cursor,MySQLdb,MySQL,fetchone,Python3 来源: https://blog.csdn.net/u011924274/article/details/120265356