如何将字典插入sqlite数据库?

时间:2020-05-31 14:45:37

标签: python sqlite

我正在尝试将字典写入sql数据库,但是没有给我成功:

Error binding parameter 3 - probably unsupported type.

数据结构:

{
"url": "https://www.wellness.com/dir/6022571/acupuncturist/ri/east-greenwich/hwasook-lee-phoenix- 
fertility-center-dac",
"First_and_Last_Name": "Hwasook Lee, D.Ac.",
"About": "Phoenix Fertility Center specializes in the treatment of infertility, hormonal imbalances, 
women's health and pregnancy related conditions. Our passion revolves around empowering you to 
understand how your body works and how your everyday habits like eating and sleeping have a direct 
impact on your health. ",
"Services": [
"When I entered acupuncture school, I knew that I wanted to specialize in women's health and 
gynecology. ", "One of my mandates as an acupuncturist is to empower my...", "Fertility"
]}

代码:

db = sqlite3.connect('wellness.db')

    db.execute('''CREATE TABLE IF NOT EXISTS wellness(id INTEGER PRIMARY KEY AUTOINCREMENT,
                                                   url TEXT, First_and_Last_Name TEXT, 
                                                   About TEXT, Services TEXT);''') 
    cursor = db.cursor()
    cursor.execute('INSERT INTO wellness(url, First_and_Last_Name, About, Services) VALUES(?, ?, ?, ?)', 
                  (item['url'], item['First_and_Last_Name'], item['About'], item['Services']))

    db.commit()
    db.close()

1 个答案:

答案 0 :(得分:1)

item['Services']

它包含一个列表,而不是字符串

cursor.execute('INSERT INTO wellness(url, First_and_Last_Name, About, Services) VALUES(?, ?, ?, ?)', 
                  (item['url'], item['First_and_Last_Name'], item['About'], '-'.join(item['Services'])  ))

由于item ['Services']是一个列表,请尝试使用join使其完整字符串