import logging
import mysql.connector
mydb = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="root",
password="root",
database="classicmodels")
mycursor = mydb.cursor(buffered=True , dictionary=True)
sql = "SELECT * FROM information_schema.tables where table_schema = 'classicmodels'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
tables = [d['TABLE_NAME'] for d in myresult]
for x in tables:
sql1 = "select * from {}".format(x)
mycursor.execute(sql1)
myresult1 = mycursor.fetchone()
for val, cal in myresult1.items():
print(f'{val} is {cal}')
我可以使用 phyton sql 連接器讀取所有表的最新記錄。可能有更好的方法來做到這一點,但因為我不允許在正在運行的資料庫中工作,所以我選擇了這種方法。