I am trying to test a script that will push a dataframe to SQL Server.
Here is what I have so far:
import pandas as pd
import pyodbc
conn_str = (
r'Driver={SQL Server};'
r'SERVER=wouldntuliketoknow\SQLEXPRESS;'
r'Database=test;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
globalID = [9581091, 9581091, 79735371, 79735371, 79735371]
ID = [53506312, 961273620, 53506312, 79735371, 135962137]
names = ['meh',
'cool',
'dude whatever',
'foo',
'bar']
df = pd.DataFrame({'globalID': globalID, 'ID': ID, 'name': names})
df.to_sql("pandas", cnxn)
print(conn_str)
It is throwing an error:
pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': ('42S02', "[42S02] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'sqlite_master'. (208) (SQLExecDirectW)")
What am I doing wrong?
I'm not sure where the sqllite_master is coming from in the error?
Is the cursor necessary?