一、链接数据库
#sql1.py
import pymysql
# user=input('用户名: ').strip()
# pwd=input('密码: ').strip()
#链接
# conn=pymysql.connect(host='192.168.8.129',user='testuser',password='testuser',database='ShareTekArchimedes24',charset='utf8')
#游标
# cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
# cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
# cursorclass=MySQLdb.cursors.DictCursor
def query(sql):
conn=pymysql.connect(host='192.168.8.129',user='testuser',password='testuser',database='ShareTekArchimedes24',charset='utf8')
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
res=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
results = cursor.fetchall()
cursor.close()
conn.close()
return res,results
# #执行sql语句
# # sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引号
# # sql='SELECT m.Name from DataModel_T_Material m'
# print(sql)
# res=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
# results = cursor.fetchall()
# print(results)
# print(res)
# cursor.close()
# conn.close()
调用:
from sql1 import query
sql='SELECT m.Name from DataModel_T_Material m'
num,set=query(sql)
print(num,set)
标签:python,数据库,results,pymysql,cursor,sql,close,链接,conn
From: https://www.cnblogs.com/relap-li/p/16981922.html