相关疑难解决方法(0)

如何使用MySQLdb从Python更改SQL隔离级别?

我研究过的文档表明,为其他数据库执行此操作的方法是在查询中使用多个语句,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 isolation-level python-db-api

9
推荐指数
1
解决办法
5153
查看次数

来自 Python 的重复 MySQL 查询返回相同的数据

我需要从 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)

python mysql python-3.x

3
推荐指数
1
解决办法
2012
查看次数