无法将记录插入表中提供的绑定数不正确

时间:2019-02-12 16:25:31

标签: python sql sqlite

无法插入ShopifyMonitor表记录(有2个字段:ID,名称) 错误的完整回溯:

File "D:\Related To Python (Tutorials)\Python-Test\Working With Database\goo.py", line 174, in <module>
    c.execute(make_shopify_name, (shopify_name))
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 10 supplied.

get_site = str(input('Enter site here: '))
url = fix_url(get_site)
shopify_name = fix_url(get_site, True)
basepath = os.path.dirname(__file__)
db_name = '{}/shopify.sqlite3'.format(basepath)
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS ShopifyMonitor (
    id integer PRIMARY KEY AUTOINCREMENT,
    name text UNIQUE NOT NULL

);"""
sql_create_tasks_table = """ CREATE TABLE IF NOT EXISTS Product (
    id integer PRIMARY KEY AUTOINCREMENT,
    product_id text NOT NULL,
    updated_at text NOT NULL,
    title text NOT NULL,
    link_to_product text UNIQUE NOT NULL,
    vendor text NOT NULL,
    sku text NOT NULL,
    quantity text NOT NULL,
    options text,
    price text NOT NULL,
    collection_id text,
    collection_updated text,
    shopify_name text NOT NULL,
    FOREIGN KEY(shopify_name) REFERENCES ShopifyMonitor(name)
);"""
make_shopify_name = '''INSERT INTO ShopifyMonitor(name) VALUES (?) '''

conn = create_connection(db_name)

if conn is not None:

    # create projects table
    create_table(conn, sql_create_projects_table)
    # create tasks table
    create_table(conn, sql_create_tasks_table)
else:
    print("Error! cannot create the database connection.")
c = conn.cursor()
c.execute(make_shopify_name, (shopify_name))
conn.commit()

您的帖子似乎主要是代码;请添加更多详细信息。添加更多详细信息。

1 个答案:

答案 0 :(得分:1)

问题很微妙:

c.execute(make_shopify_name, (shopify_name))

应该是:

c.execute(make_shopify_name, (shopify_name,))  # note comma after shopify_name

传递给execute的第二个参数应该是查询的参数元组-即使只有一个参数,它仍必须是一个元组。

目前,您所拥有的只是一个围绕变量名的括号-括号基本上将被Python忽略,因为它们没有任何意义。

一个常见的误解是,括号是组成元组的-不是,而是逗号:

x = (1)  # x is 1
x = 1,   # x is a tuple containing a single value, the integer 1
x = (1,) # as above - but the parentheses aren't actually required syntactically here