Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何正确填写表格_Python_Sqlite - Fatal编程技术网

Python 如何正确填写表格

Python 如何正确填写表格,python,sqlite,Python,Sqlite,您能告诉我如何从.CSV文件中正确填写数据库中的表格吗? 我有一个数据库,包括: “ID”整数不为空, 主键(“ID”自动递增) 有一个.CSV文件: 我连接到数据库并读取给定文件: con = sqlite3.connect(databasename) table_count_before = con.execute("select * from test").fetchall() cur = con.cursor() with open(filename_csv, '

您能告诉我如何从
.CSV
文件中正确填写数据库中的表格吗? 我有一个数据库,包括:

“ID”整数不为空, 主键(“ID”自动递增)

有一个
.CSV
文件:

我连接到数据库并读取给定文件:

con = sqlite3.connect(databasename)
table_count_before = con.execute("select * from test").fetchall()
cur = con.cursor()
with open(filename_csv, 'r') as f:
    dr = csv.DictReader(f, delimiter=';')
    test_csv = [(i['Model'], i['Price'], i['Format']) for i in dr]

cur.executemany("INSERT INTO test (ID, Vendor, Model, Type, Standart, Format, " 
                "VALUES (?, ?, ?, ?, ?, ?) "
                "ON CONFLICT(Model) DO UPDATE SET "
                "ID = ?, Vendor = ?, Type = ?, Standart = ?, Format = ?;", test_csv)
con.commit()
con.close()
事实证明,我需要从文件中添加一行,重点放在数据库中没有的模型上,如果有,则替换这一行,简而言之,如果没有这样的模型,则插入这一行,如果有更新/替换的内容。 我根本不需要
“Price”
列,但我无法将其从
.CSV中删除。

在Pycharm中,我得到一个错误:

sqlite3.OperationalError:ON CONFLICT子句不匹配任何主键或唯一约束


请帮我解决这个问题,我办不到。非常感谢。

模型
列施加
唯一
约束

CREATE TABLE test (
    ID       INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    Vendor   TEXT,
    Model    TEXT NOT NULL UNIQUE,
    Type     TEXT,
    Standart TEXT,
    Format   TEXT
)
没有任何约束,就不会有冲突

然后:

它应该会起作用。(测试)


别忘了
executemany
需要一个iterable的iterable,您应该为每个
提供一个值。

我们可以看看您用来创建表的脚本吗?因为我可以发现两个潜在的问题:

  • 您的CSV包含3行,而您的表包含5,除非您 为缺少的两个设置默认值脚本将无法工作
  • CNFLICT上的部分查询将无法工作,除非您已经 为您的字段设置一些约束

  • 错误:
    sqlite3.OperationalError:near“VALUES”:语法错误
    更新了请求中的代码,现在我正在使用it@AlexRebell有不同的错误吗?没有,只是这个错误,我在你的要求下创建了一个表,我正试图用我的代码填充这个表,我更新了数据库中的代码question@AlexRebell我复制并粘贴了我的完整代码片段。:)我重新创建了这个表,正如他在回答@GyuHyeon choi中指出的那样,克服@GyuHyeon列的基数的想法是先编辑csv,然后再将其上传到数据表,对吗?它肯定会工作,但如果你要对其他文件重复同样的过程,它不是很干净,也不容易移植。我建议你把熊猫作为一种选择,在你的情况下,这可能会有帮助,谢谢你,我一定会考虑到这一点!
        conn = sqlite3.connect('main.db')
    
        test_csv = []
    
        with open(filename_csv, 'r') as f:
            dr = csv.DictReader(f, delimiter=';')
            for i in dr: 
                test_csv.append(
                    (
                        '', i['Model'], '', i['Price'], i['Format'],
                        '', '', i['Price'], i['Format']
                    )
                )
        
        conn.executemany(
            "INSERT INTO test (Vendor, Model, Type, Standart, Format) " 
            "VALUES (?, ?, ?, ?, ?) "
            "ON CONFLICT(Model) DO UPDATE SET "
            "Vendor = ?, Type = ?, Standart = ?, Format = ?;", 
            test_csv
        )   
    
        conn.commit()
        conn.close()