python连接数据库--查询数据

 1 #!/usr/bin/python
 2 # -*- coding: utf-8 -*-
 3 import pymysql
 4 
 5 def fileDB():
 6         # 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
 7         db = pymysql.connect("192.168.10.42", "用户名", "密码", "数据库名称")
 8         # 使用 cursor() 方法创建一个游标对象 cursor
 9         # 使用cursor()方法获取操作游标
10         cursor = db.cursor()
11 
12         # 使用execute方法执行SQL语句
13         cursor.execute("SELECT tx_id FROM tx_record_attchment  GROUP BY tx_id HAVING (count(tx_id)<4)  ORDER BY  create_time  DESC")
14 
15         # 使用 fetchone() 方法获取一条数据
16         txId = cursor.fetchone()
17 
18         print "txId : %s " % txId[0]
19         print "txId[0][0] : %s " % type(txId[0])
20         cursor.execute("SELECT enterprise_tx_no FROM tx_record  WHERE  id="+str(txId[0]))
21 
22         # 使用 fetchone() 方法获取一条数据
23         txNo = cursor.fetchone()
24         print "Database version : %s " % txNo
25 
26         # 关闭数据库连接
27         db.close()
28         return txNo[0]
29 #fileDB()

猜你喜欢

转载自www.cnblogs.com/wangxiaoqun/p/9045112.html