我研究过的文档表明,为其他数据库执行此操作的方法是在查询中使用多个语句,a:
>>> cursor = connection.cursor()
>>> cursor.execute("set session transaction isolation level read uncommitted;
select stuff from table;
set session transaction isolation level repeatable read;")
Run Code Online (Sandbox Code Playgroud)
不幸的是,这样做没有结果,因为显然Python DB API(或者只是它的实现?)不支持单个查询中的多个记录集.
过去有没有其他人成功?
我需要从 Python 中重复查询 MySQL 数据库,因为数据正在快速变化。每次读取数据时,都会将其传输到列表中。
我曾假设只需将查询放入循环中即可在每次迭代时从数据库中获取数据。看来不是。
import mysql.connector
from mysql.connector import Error
from time import sleep
# Create empty list to store values from database.
listSize = 100
myList = []
for i in range(listSize):
myList.append([[0,0,0]])
# Connect to MySQL Server
mydb = mysql.connector.connect(host='localhost',
database='db',
user='user',
password='pass')
# Main loop
while True:
# SQL query
sql = "SELECT * FROM table"
# Read the database, store as a dictionary
mycursor = mydb.cursor(dictionary=True)
mycursor.execute(sql)
# Store data in rows
myresult = …Run Code Online (Sandbox Code Playgroud)