[英]“Invalid object name” error when executing query using sqlalchemy
I connect database with pycharm and I use sqlAlchemy.我使用 pycharm 连接数据库并使用 sqlAlchemy。 When I am trying to execute an insert query it shows the following error:当我尝试执行插入查询时,它显示以下错误:
Invalid object name 'pfAnalytics.optPrice'
The error is due to the fact that it add "[" and "]" to my tables' name when I do :该错误是由于它在我执行以下操作时将“[”和“]”添加到我的表名称中:
ins = table.insert()
if I check the string I see:如果我检查我看到的字符串:
str(ins) == 'INSERT INTO [pfAnalytics.optPrice] DEFAULT VALUES'
instead of:代替:
str(ins) == 'INSERT INTO pfAnalytics.optPrice DEFAULT VALUES'
my request look like this:我的请求是这样的:
listToWrite = all.to_dict(orient='records')
metadata = sql.schema.MetaData(bind=engine,reflect=True)
table = sql.Table("pfAnalytics.optPrice", metadata)
Session = sessionmaker(bind=engine)
session = Session()
querydel = sql.delete("pfAnalytics.optPrice")
results = consql.execute(querydel)
consql.execute(sql.insert(table), listToWrite)
How to get rid of these brackets?如何摆脱这些括号?
The answer is tricky, it iss du to an error on SQL server.答案很棘手,这是由于 SQL 服务器上的错误造成的。 So I had to specify to to put no bracket at the begining and the end of the table's name with :所以我必须指定在表名的开头和结尾不加括号:
engine.dialect.identifier_preparer.initial_quote = ''
engine.dialect.identifier_preparer.final_quote = ''
I ran into this issue using MSSQL Server and could not seem to find an answer that would work for me.我使用 MSSQL Server 遇到了这个问题,但似乎找不到适合我的答案。 I finally discovered the problem to be in connection string for the db engine.我终于发现问题出在数据库引擎的连接字符串中。 Apparently SQLAlchemy needs the driver included in the string.显然 SQLAlchemy 需要包含在字符串中的驱动程序。 This holds true if you are using a Flask app, for the URI as well as creating an engine.如果您使用 Flask 应用程序来获取 URI 以及创建引擎,则情况如此。
An example in the Flask app: Flask 应用程序中的一个示例:
app.config["SQLALCHEMY_DATABASE_URI"] = "mssql+pyodbc://username:\
password@server/database?driver=SQL+Server"
Similarly, in creating an engine:同样,在创建引擎时:
db_string = "mssql+pyodbc://username:password@server/database?driver=SQL+Server"
engine = create_engine(db_string)
It seems that driver=SQL+Server is the key to making this work for me.似乎 driver=SQL+Server 是使这项工作对我有用的关键。 I didn't see this on the official documentation but noticed it in another post on Stack Overflow.我没有在官方文档中看到这一点,但在 Stack Overflow 上的另一篇文章中注意到了这一点。 So credit to Praveen for this:因此,这要归功于 Praveen:
Connect to MSSQL Database using Flask-SQLAlchemy 使用 Flask-SQLAlchemy 连接到 MSSQL 数据库
The exact error I was getting from SQLAlchemy/pyodbc was:我从 SQLAlchemy/pyodbc 得到的确切错误是:
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name
Hopefully this can save someone the headache it created for me.希望这可以避免它为我带来的头痛。
In my case I had to use Database
instead of Initial Catalog
.就我而言,我不得不使用Database
而不是Initial Catalog
。
Compare these two, to get the difference:比较这两个,以获得差异:
Server=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Server=myServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.