简体   繁体   中英

Python - INSERT table INTO SQLite from MS SQL Server

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM