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.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

You were correct. The system was defaulting to a different schema. When the table was recreated with the "dbo" schema my python script worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.