简体   繁体   English

使用Python将dbf转换为sqlite不会填充表

[英]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. 我正在尝试使用dbf表创建Sqlite数据库。 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. 当我创建一个从dbf派生的记录列表以用作Sqlite executemany语句的输入时,Sqlite表为空。 When I try to replicate the issue using Python interactively, the Sqlite execution is successful. 当我尝试使用Python交互式复制问题时,Sqlite执行成功。 The list generated from the dbf is populated when I run it - so the problem lies in the executemany statement. 从dbf生成的列表在运行时会被填充-因此问题出在executemany语句中。

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. 这可能不是唯一的问题,但是您想调用conn.commit()将更改保存到SQLite数据库。 Reference: http://www.python.org/dev/peps/pep-0249/#commit 参考: http : //www.python.org/dev/peps/pep-0249/#commit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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