python + mysql 實現查詢表數據


實例如下:

import pymysql

def select_form():
# 打開數據庫連接
db = pymysql.connect("localhost", "root", "123456", "test")

# 使用cursor()方法獲取操作游標
cursor = db.cursor()

# SQL 查詢語句
sql = """SELECT * FROM student WHERE ID = %s"""%(3)

try:
# 執行SQL語句
cursor.execute(sql)
# 獲取所有記錄列表
results = cursor.fetchall()
for row in results:
id = row[0]
name = row[1]
age = row[2]
address = row[3]
create_time = row[4]
print("id=%s,name=%s,age=%s,address=%s,create_time=%s"%\
(id,name,age,address,create_time))

except Exception as e:
print("查詢出錯:case%s"%e)

finally:
# 關閉游標連接
cursor.close()
# 關閉數據庫連接
db.close()

def main():
select_form()

if __name__ == '__main__':
main()



免責聲明!

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



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