4

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.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418

1 Answers1

3

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).

jspcal
  • 50,847
  • 7
  • 72
  • 76
  • You were correct. The system was defaulting to a different schema. When the table was recreated with the "dbo" schema my python script worked. – Az Cactus Jack Jun 06 '19 at 00:14