# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host="192.168.1.1",
user="root",
password="password",
db ="test",
charset ="utf8")
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
# cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 定义要执行的SQL语句
sql = "select version()"
# 执行SQL语句
cursor.execute(sql)
# #使用fetchone()方法获取一条数据
data = cursor.fetchone()
print("Mysql version : %s"%data)
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()