Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Invalid object name" error on INSERT to SQL Server from pyodbc

I am trying to insert some data into a SQL Server database using the pyodbc module.

I have the connection string working.

The following code lists the two columns in the table.

 for row in cursor.columns(table='InvUtil'):
     print (row.column_name)

I see the following two columns that are in the InvUtil table:

server

termss

I then try to insert data with the following command:

 cursor.execute("INSERT INTO InvUtil(server, termss) values (?, ?)", 'ten', 'eleven')

I get the following error. Seems like it's having issues with the InvUtil table name.

Traceback (most recent call last):
  File "SQLpydobc.py", line 61, in <module>
    cursor.execute("INSERT INTO InvUtil(server, termss) values (?, ?)", 'ten', 'eleven')
pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]**Invalid object name 'InvUtil'**. (208) (SQLExecDirectW)")

I expect the script to run without errors. I have a commit command later in the script so should see data in the columns.

like image 493
Az Cactus Jack Avatar asked Oct 25 '25 14:10

Az Cactus Jack


1 Answers

Perhaps specify the full table name with schema/db as in dbo.InvUtil?

Or perhaps specify the default database name in the connection string (Database=dbo or InitialCatalog=dbo).

like image 196
jspcal Avatar answered Oct 28 '25 05:10

jspcal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!