【问题标题】:SQLite python - parameters are of unsupported type [closed]SQLite python - 参数的类型不受支持[关闭]
【发布时间】:2019-08-30 07:10:07
【问题描述】:

我想使用 Python sqlite 模块将记录插入到 sqlite 数据库中。

使用 executemany() 执行 INSERT 语句会导致“参数类型不受支持”错误。

import  sqlite3
conn = sqlite3.connect('food.db')
c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS nutritional_values
                (item, calories, total fat, protein)''')
items = [       ('Broccoli Chinese',    22,     0.7,    1.1),
                ('chia seeds',          490,    30.8,   15.6),
                ('blueberries',         57,     0.3,    0.7),]
c.executemany('INSERT INTO nutritional_values VALUES (?,?,?,?)',items)


c.execute('''CREATE TABLE IF NOT EXISTS food_consumption
                (date, item, amount)''')
c.execute('DELETE FROM food_consumption')
consumed = [    ('24/8/2019', 'Broccoli Chinese',    1.5),
                ('24/8/2019', 'chia seeds',          0.35),
                ('24/8/2019', 'blueberries',         0.4),]
c.executemany('INSERT INTO food_consumption VALUES (?,?,?)',consumed)

sql = """SELECT nv.item, nv.calories
         FROM nutritional_values nv
         INNER JOIN food_consumption fc
             ON nv.item = fc.item"""

for row in c.execute(sql):
    print(row)

c.execute('''CREATE TABLE IF NOT EXISTS nutrition_consumed
                (var1)''')

for row in c.execute(sql):
    var1 = row[1]*2
    item = [(var1),]
    c.executemany('INSERT INTO nutrition_consumed VALUES (?)', (item),)

for row in c.execute('SELECT * FROM nutrition_consumed'):
    print(row)

conn.commit()
conn.close()

得到

    c.executemany('INSERT INTO nutrition_consumed VALUES (?)', (item),)
ValueError: parameters are of unsupported type

如何将var解析到表中?

【问题讨论】:

标签: python sqlite


【解决方案1】:

(item), 替换为(item, )。带输出的工作代码如下。

import  sqlite3
conn = sqlite3.connect('food.db')
c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS nutritional_values
                (item, calories, total fat, protein)''')
items = [       ('Broccoli Chinese',    22,     0.7,    1.1),
                ('chia seeds',          490,    30.8,   15.6),
                ('blueberries',         57,     0.3,    0.7),]
c.executemany('INSERT INTO nutritional_values VALUES (?,?,?,?)',items)


c.execute('''CREATE TABLE IF NOT EXISTS food_consumption
                (date, item, amount)''')
c.execute('DELETE FROM food_consumption')
consumed = [    ('24/8/2019', 'Broccoli Chinese',    1.5),
                ('24/8/2019', 'chia seeds',          0.35),
                ('24/8/2019', 'blueberries',         0.4),]
c.executemany('INSERT INTO food_consumption VALUES (?,?,?)',consumed)

sql = """SELECT nv.item, nv.calories
         FROM nutritional_values nv
         INNER JOIN food_consumption fc
             ON nv.item = fc.item"""

for row in c.execute(sql):
    print(row)

c.execute('''CREATE TABLE IF NOT EXISTS nutrition_consumed
                (var1)''')

for row in c.execute(sql):
    var1 = row[1]*2
    item = [(var1),]
    c.executemany('INSERT INTO nutrition_consumed VALUES (?)', (item,))

for row in c.execute('SELECT * FROM nutrition_consumed'):
    print(row)

conn.commit()
conn.close()

输出:

abhay@abhay-Lenovo-Z51-70:~$ python test.py

('西兰花中式', 22) (“奇亚籽”,490) (“蓝莓”,57) (44,)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多