1
votes

I am trying to query MS SQL Server for a table.column, then insert this output into a sqlite table.

This example has one numeric column in the SQL Server source table. I think I've almost got it by scouring the other answers.

Please let me know what I am missing.

import sqlite3
import pyodbc


#def connect_msss():
ODBC_Prod = ODBC_Prod 
SQLSN = SQLSN 
SQLpass = SQLpass 
conn_str = ('DSN='+ODBC_Prod+';UID='+SQLSN+';PWD='+SQLpass)
conn = pyodbc.connect(conn_str)


#def connect_sqlite():
sl3Conn = sqlite3.connect('server_test.db')
c = sl3Conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS mrn_test (PTMRN NUMERIC)')


#def query_msss():

cur = conn.cursor()
cur.execute("SELECT TOP 50 PTMRN FROM dbo.atl_1234_mrntest")
rows = cur.fetchall()
for row in rows:
    c.execute("INSERT INTO mrn_test VALUES (?)", row)
conn.commit()


#connect_msss()
#connect_sqlite()
#query_msss()

Error 1:

c.execute('CREATE TABLE IF NOT EXISTS mrn_test (PTMRN NUMERIC)')

 Out[117]: <sqlite3.Cursor at 0x2d1a742fc70>

Error 2:

cur = conn.cursor() cur.execute("SELECT TOP 50 PTMRN FROM dbo.atl_1234_mrntest")

Out[118]: <pyodbc.Cursor at 0x2d1a731b990>
1
Those functions could not possibly run without error. For example, in query_msss neither c nor conn are defined.Daniel Roseman
You're right, thanks. I edited the question with a simplified code and the errors.Samsonite Manly
if there are errors, what are the error messages?user1443098

1 Answers

1
votes

You're not committing the executed changes on the sqlite connection, after the c.execute step you're committing the MySQL DB connection. I think you need to replace conn.commit() at the end with sl3Conn.commit().