Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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 sqlite插入命名参数或null_Python_Sqlite_Named Parameters - Fatal编程技术网

python sqlite插入命名参数或null

python sqlite插入命名参数或null,python,sqlite,named-parameters,Python,Sqlite,Named Parameters,我试图使用命名参数将字典中的数据插入数据库。我有一个简单的SQL语句,例如 SQL = "INSERT INTO status (location, arrival, departure) VALUES (:location, :arrival,:departure)" dict = {'location': 'somewhere', 'arrival': '1000', 'departure': '1001'} c.execute(SQL,dict) 将某处插入位置,将1000插入到达列,将

我试图使用命名参数将字典中的数据插入数据库。我有一个简单的SQL语句,例如

SQL = "INSERT INTO status (location, arrival, departure) VALUES (:location, :arrival,:departure)"
dict = {'location': 'somewhere', 'arrival': '1000', 'departure': '1001'}
c.execute(SQL,dict)
将某处插入位置,将1000插入到达列,将1001插入离开列

我实际拥有的数据将包含位置,但可能包含到达或离开,但可能不同时包含两者(在这种情况下,表中可能没有任何内容或空值)。在本例中,我得到sqlite3.ProgrammingError:您没有为绑定2提供值

我可以使用defaultdict修复此问题:

c.execute(SQL,defaultdict(str,dict))
为了让事情稍微复杂一点,我实际上会有一个字典列表,其中包含多个地点,有到达地点,也有离开地点

    ({'location': 'place1', 'departure': '1000'},
    {'location': 'palce2', 'arrival': '1010'},
    {'location': 'place2', 'departure': '1001'})
我希望能够用c.ExecuteMy运行它,但是我现在不能使用defaultdict

我可以遍历列表中的每个字典并运行许多c.execute语句,但executemany似乎是一种更整洁的方法

为了方便起见,我简化了这个示例,实际数据在字典中有更多的条目,我从JSON文本文件构建它


有人对我如何做有什么建议吗?

使用
None
插入
NULL

dict = {'location': 'somewhere', 'arrival': '1000', 'departure': None}
您可以使用默认字典和生成器将其与
executemany()
一起使用:


对于这个问题有一个更简单的解决方案,在大多数情况下应该是可行的只需将默认dict的列表传递给
即可,而不是
dict的列表

换句话说,如果您从头开始将行构建为
defaultdict
,则可以将
defaultdict
行列表直接传递给命令
executemany
,而不是将它们构建为字典,然后在使用
executemany
之前修补情况

以下工作示例(Python 3.4.3)说明了这一点:

import sqlite3
from collections import defaultdict
# initialization
db = sqlite3.connect(':memory:')
c = db.cursor()
c.execute("CREATE TABLE status(location TEXT, arrival TEXT, departure TEXT)")
SQL = "INSERT INTO status VALUES (:location, :arrival, :departure)"
# build each row as a defaultdict
f = lambda:None # use str if you prefer
row1 = defaultdict(f,{'location':'place1', 'departure':'1000'})
row2 = defaultdict(f,{'location':'place2', 'arrival':'1010'})
rows = (row1, row2)
# insert rows, executemany can be safely used without additional code
c.executemany(SQL, rows)
db.commit()
# print result
c.execute("SELECT * FROM status")
print(list(zip(*c.description))[0])
for r in c.fetchall():
    print(r)
db.close()
如果运行它,它将打印:

('location', 'arrival', 'departure')
('place1', None, '1000') # None in Python maps to NULL in sqlite3
('place2', '1010', None)

谢谢你,马丁,行得通。对于存在此问题的任何其他人,默认词典需要包含每个可能项的条目,即使实际词典始终包含该条目。另外,executemany行上缺少最后一个右括号。
('location', 'arrival', 'departure')
('place1', None, '1000') # None in Python maps to NULL in sqlite3
('place2', '1010', None)