我使用Python进行了搜索,得到了下面的结果,但是在将它保存到SQLite中的表时遇到了麻烦。具有3列(类别,名称,ID)的表
data = [{u'category': u'Local Business',
u'category_list': [{u'id': u'145887745471348', u'name': u'Train Station'}],
u'id': u'129926257075048',
u'name': u'Southampton the Fit Student Central!'},
{u'category': u'Local Business',
u'category_list': [{u'id': u'2500', u'name': u'Local Business'}],
u'id': u'151910741511395',
u'name': u'Student Hell Train'},
{u'category': u'Local Business',
u'category_list': [{u'id': u'192119584190796', u'name': u'Event'}],
u'id': u'159561157587886',
u'name': u'Student Ambassador Training'}]
stmt = "insert into Student_Mentors (category, name, id) values ({category}, {name}, {id})".format(**data)
con.executemany(stmt, data)
con.commit()
我收到以下错误...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-174-58d2f1a67f61> in <module>()
13
14
---> 15 stmt = "insert into Student_Mentors (category, name, id) values ({category}, {name}, {id})".format(**data)
16 con.executemany(stmt, data)
17 con.commit()
TypeError: format() argument after ** must be a mapping, not list
不确定如何使其成为映射
答案 0 :(得分:0)
sqlite3
模块的paramstyle
为qmark
,意味着参数的占位符应为?
s。
你需要使用如下:
stmt = "INSERT INTO Student_Mentors (category, name, id) VALUES (?, ?, ?)"
rows = [[row['category'], row['name'], row['id']] for row in data]
con.executemany(stmt, rows)
con.commit()