简体   繁体   中英

Converting dbf to sqlite using Python is not populating table

I've struggled over this issue for over an hour now. I'm trying to create a Sqlite database using a dbf table. When I create a list of records derived from a dbf to be used as input for the Sqlite executemany statement, the Sqlite table comes out empty. When I try to replicate the issue using Python interactively, the Sqlite execution is successful. The list generated from the dbf is populated when I run it - so the problem lies in the executemany statement.

import sqlite3
from dbfpy import dbf

streets = dbf.Dbf("streets_sample.dbf")

conn = sqlite3.connect('navteq.db')

conn.execute('PRAGMA synchronous = OFF')
conn.execute('PRAGMA journal_mode = MEMORY')

conn.execute('DROP TABLE IF EXISTS STREETS')

conn.execute('''CREATE TABLE STREETS
    (blink_id CHAR(8) PRIMARY KEY,
    bst_name VARCHAR(39),
    bst_nm_pref CHAR(2));''')

alink_id = []
ast_name = []
ast_nm_pref = []

for i in streets:
    alink_id.append(i["LINK_ID"])
    ast_name.append(i["ST_NAME"])
    ast_nm_pref.append(i["ST_NM_PREF"])

streets_table = zip(alink_id, ast_name, ast_nm_pref)

conn.executemany("INSERT OR IGNORE INTO STREETS VALUES(?,?,?)", streets_table)

conn.close()

This may not be the only issue, but you want to call conn.commit() to save the changes to the SQLite database. Reference: http://www.python.org/dev/peps/pep-0249/#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