且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Python 中使用 SQLite 将 .csv 文件导入 SQL 数据库

更新时间: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:

  1. person.csv 的最后两行格式不正确,但这并不妨碍程序运行.您可以使用文本编辑器解决此问题.
  2. person.csv 使用制表符作为分隔符而不是逗号.
  3. 在以to_db ="开头的行中有一个错字(拼写)
  4. 要导入的列数不匹配(2 而不是 11)
  5. executemany 上的表名错误.

此外,我在文件中而不是在内存中创建数据库.它足够小,性能应该不是问题,而且您所做的任何更改都将被保存.

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