0
我有一個字典列表中的數據,我試圖插入數據到我的數據庫然後我得到錯誤sqlalchemy.exc.CompileError:具有當前數據庫版本設置的「默認」方言不支持就地多行插入。當前數據庫版本設置的'默認'方言不支持就地多行插入
from sqlalchemy import create_engine,Table,Column,String,Integer,MetaData,ForeignKey
from sqlalchemy_utils import database_exists,create_database,drop_database
from sqlalchemy.orm import join
engine = create_engine("mysql+mysqldb://root:[email protected]:3306/sqlalchemy")
conn = engine.connect()
if database_exists(engine.url):
drop_database(engine.url)
create_database(engine.url)
metaData = MetaData()
userTable = Table("user", metaData
, Column("Id", Integer, primary_key = True)
, Column("Name", String(25)))
orderTable = Table("order", metaData
, Column("Id", Integer, primary_key = True)
, Column("UserId", Integer, ForeignKey("user.Id"))
, Column("Desc", String(250)))
try:
metaData.create_all(engine)
except Exception as ex:
print(ex)
users = [
{"Id": 1,
"Name": "user1"},
{"Id": 2,
"Name": "user2"},
{"Id": 3,
"Name": "user3"},
{"Id": 4,
"Name": "user4"}
]
orders = [
{"Id": 1,
"UserId": 1,
"Desc": "desc1"},
{"Id": 2,
"UserId": 1,
"Desc": "desc2"},
{"Id": 3,
"UserId": 2,
"Desc": "desc3"},
{"Id": 4,
"UserId": 2,
"Desc": "desc4"},
]
sql = userTable.insert().values(users)
print(sql) #this line causes the exception
conn.execute(sql)
sql = orderTable.insert().values(orders)
print(sql)
conn.execute(sql)
sql = userTable.select()
print(sql)
returnUsers = conn.execute(sql)
print(returnUsers)
sql = orderTable.select()
print(sql)
returnOrders = conn.execute(sql)
print(returnOrders)
conn.close()
更新:沒有打印(SQL),它可以完美的作品。
我真正需要的是一批insert.Conn.execute(mutRowsInsertSql)可以工作,但打印(mutRowsInsertSql)不是。 –
好的。所以我猜想,在這種情況下'print'調用的方法不知道方言(從引擎傳遞到連接,但無法傳遞到Table類)並使用標準方法。 – AndreasT