数据库-将列名对应的值一起打印出来

 1 import pymysql
 2 
 3 class Get_sql():
 4     def __init__(self):
 5         self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='**', db='book')
 6 
 7     def get_sql(self,sql):
 8         corsor=self.db.cursor()
 9         corsor.execute(sql)
10 
11         #获取列名属性
12         index = corsor.description
13         print(index)
14         #获取查询结果
15         rows=corsor.fetchall()
16         print(rows)
17 
18         for res in rows:
19             row={}
20             for i in range(len(index)):
21                 row[index[i][0]]=res[i]
22             return row
23 
24         self.db.commit()
25         self.db.close()
26 
27 if __name__ == '__main__':
28     sql='select user,password from test_api where id=2'
29     print(Get_sql().get_sql(sql))

 

posted on 2019-07-13 11:14  cherry_ning  阅读(364)  评论(0)    收藏  举报

导航