我有一个非常大的csv文件,带有分隔符" |"在db.First中创建一个表。下面给出了5行csv
"Tha Twilight New Moon DVDrip 2009 XviD-AMiABLE"|694554360|2cae2fc76d110f35917d5d069282afd8335bc306|0|movies|0|1
"[RiP] Burn Up!"|734636626|f80762eb595c286b765848d13074247ee6a90d8c|2|other|0|0
"%5BA-Flux%26Lunar%5D Hunter x Hunter Episodes 1-5 %5BDVD%5D"|1094189056|b639c563d665c1f2827a815267033724abfe4f4f|5|anime|0|0
"Hack SIGN 1-25 extra eps -aak-"|6258485500|8eb275e544913bd3255fd4781f058c3f2f98f77e|27|other|0|0
"Hunter X Hunter - 11-15 [A-K]"|971266048|ce07cc4a4da8e00e1c4972278d6e14b3049bb5cf|5|anime|0|0
我的必填字段也在下面
CREATE TABLE torrents (
name TEXT,
size DATE,
hash TEXT UNIQUE,
downloads_count TEXT,
category TEXT,
seeders INTEGER,
leechers INTEGER
);
.mode csv
.separator "|"
.import torrents_mini.csv torrents
请建议更好更快地将此表创建到数据库中?
答案 0 :(得分:0)
import csv
import sqlite3
conn = sqlite3.connect('navjod.db')
c = conn.cursor()
# Create table name | size | hash | downloads_count | category_id* | seeders | leechers
c.execute("DROP TABLE IF EXISTS torrents")
c.execute('''CREATE TABLE torrents
(name TEXT, size TEXT, hash TEXT UNIQUE, downloads_count TEXT,
category TEXT, seeders INTEGER, leechers INTEGER)''')
with open('sample.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter='|')
for i in spamreader:
c.execute("INSERT INTO torrents VALUES (?,?,?,?,?,?,?)", i)
conn.commit()
希望它会对你有所帮助:)。