更新时间:2023-01-21 15:34:36
这对我在 Windows 10 上有效,但在 Linux/Unix 下也应该有效.有几个问题:
This works for me on Windows 10, but should work under Linux/Unix too. There are several problems:
此外,我在文件中而不是在内存中创建数据库.它足够小,性能应该不是问题,而且您所做的任何更改都将被保存.
In addition, I create the database in a file rather than in memory. It is small enough that performance should not be a problem and also any changes you make will be saved.
这是我更正后的文件(你可以自己做另一个表):
Here is my corrected file (you can do the other table yourself):
import sqlite3, csv
# con = sqlite3.connect(":memory:")
con = sqlite3.connect("person.db")
cur = con.cursor()
cur.execute("CREATE TABLE person (personid STR,age STR,sex STR,primary_voting_address_id STR,state_code STR,state_fips STR,county_name STR,county_fips STR,city STR,zipcode STR, zip4 STR, PRIMARY KEY(personid))")
with open('person.csv','r') as person_table:
dr = csv.DictReader(person_table, delimiter='\t') # comma is default delimiter
to_db = [(i['personid'], i['age'], i['sex'], i['primary_voting_address_id'], i['state_code'], i['state_fips'], i['county_name'], i['county_fips'], i['city'], i['zipcode'], i['zip4']) for i in dr]
cur.executemany("INSERT INTO person VALUES (?,?,?,?,?,?,?,?,?,?,?);", to_db)
con.commit()