1

我已经尝试过,但我无法让它工作。我最初对文档的 utf 解码有问题,所以我添加了 errors='ignore 部分。现在我在一个出现 sqlite3 语法错误的地方。我的占位符数量与我的列相同(我知道它有很多列。我试图将它们全部放入数据库中,以便更容易以有意义的方式将其中一些列放在一起)

这是我的代码。

import csv, sqlite3

con = sqlite3.connect("4x4orce.db")
cur = con.cursor()
cur.execute("""CREATE TABLE t (Premier Part Number, Long Description, External Long Description, Upc, USA, Item, 
Availability, Canada Item Availability, Core Charge, Customer Price, Jobber, MAP, Retail, Inventory Status, Brand, 
Mfg Part Number, UT whse, KY whse, TX whse, CA whse, WA whse, ImageURL, ShipsLTL, ItemWithCores, Cust CAD Price, 
Proposition 65 Carcinogen, Proposition 65 Reproductive Harm)""")  # use your column names here

with open('python/4x4orce/premier_data_feed_4x4orce.csv', 'r', errors='ignore') as fin:
    # `with` statement available in 2.5+
    # csv.DictReader uses first line in file for column headings by default
    dr = csv.DictReader(fin)  # comma is default delimiter
    to_db = [(i['Premier Part Number'], i['Long Description'], i['External Long Description'], i['Upc'],
            i['USA Item Availability'], i['Canada Item Availability'], i['Core Charge'], i['Customer Price'],
            i['Jobber'], i['MAP'], i['Retail'], i['Inventory Status'], i['Brand'], i['Mfg Part Number'], i['UT whse'],
            i['KY whse'], i['TX whse'], i['CA whse'], i['WA whse'], i['ImageURL'], i['ShipsLTL'], i['ItemWithCores'],
            i['Customer CAD Price'], i['Proposition 65 Carcinogen'], i['Proposition 65 Reproductive Harm']) for i in dr]

cur.executemany("""INSERT INTO t (Premier Part Number, Long Description, External Long Description, Upc, USA, Item, 
                Availability, Canada Item Availability, Core Charge, Customer Price, Jobber, MAP, Retail, 
                Inventory Status, Brand, Mfg Part Number, UT whse, KY whse, TX whse, CA whse, WA whse, ImageURL, 
                ShipsLTL, ItemWithCores, Cust CAD Price, Proposition 65 Carcinogen, Proposition 65 Reproductive Harm) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", to_db)
con.commit()
con.close()

这是我得到的错误:

Traceback (most recent call last):
  File "/Users/mwhites/programming/python/4x4orce/ImportCSV.py", line 8, in <module>
    Proposition 65 Carcinogen, Proposition 65 Reproductive Harm)""")  # use your column names here
sqlite3.OperationalError: near "65": syntax error

Process finished with exit code 1

我在这里做错了什么?

4

0 回答 0