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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM