简体   繁体   English

使用 sqlalchemy 执行查询时出现“无效对象名称”错误

[英]“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:比较这两个,以获得差异:

  • not working one:不工作:
Server=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
  • working one:工作一:
Server=myServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SQLAlchemy执行查询时避免参数绑定 - Avoid Parameter Binding When Executing Query with SQLAlchemy 执行sqlalchemy存在查询 - Executing a sqlalchemy exists query 使用 AsyncEngine 执行任何 SQL 查询时的 SQLAlchemy v1.4 ObjectNotExecutableError - SQLAlchemy v1.4 ObjectNotExecutableError when executing any SQL query using AsyncEngine TypeError:必须是字符串或缓冲区,而不是int:在执行sqlAlchemy查询时 - TypeError: must be string or buffer, not int: when executing sqlAlchemy query 使用具有相同 model 和 class 名称的 SQLAlchemy 查询 - Using SQLAlchemy query with same model and class name 使用来自URL的变量进行查询无法执行-Flask-SQLAlchemy - Query using variable from url not executing - Flask-SQLAlchemy 使用 SqlAlchemy 执行原始查询(在 SQL-Server 数据库和 Pymssql 上)时,传递参数未被识别并引发 SQL 错误 - Passing parameters not being recognized and throws SQL error when executing raw query (on SQL-Server database and Pymssql) with SqlAlchemy 使用 OOP 方法在 tkinter python 中执行(“after”脚本)时如何处理无效命令名称错误 - How to handle Invalid command name error, while executing (“after” script) in tkinter python using OOP Approach python sqlalchemy不执行sql查询 - python sqlalchemy not executing sql query 使用对象属性的SQLAlchemy动态查询 - SQLAlchemy dynamic query using object attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM