-1

I have a list like this:

result1 = ['"United States"', '"China"', '"Sweden"', '"Europe"', '"Russian Federation"', '"China"']

And I want to insert it into a table:

con.execute("INSERT INTO TableName(contry) VALUES(?)", result1)

But I get an error:

Incorrect number of bindings supplied. The current statement uses 1, and there are 74 supplied.

Any help would be much appreciated. Or if you need more code, please let me know.

1 Answer 1

0

Your code as written is trying to insert a single row, and in that row you're specifying 74 values instead of the 1 that your query wants.

Additionally, the beauty of parameterized queries is you do not need to (and should not) quote strings or do anything else in particular to avoid SQL injection attacks. Thus, the strings in your country list probably should not be quoted (unless you want them to actually contain quotation marks in the database for some reason).

You're probably looking for con.executemany instead, which takes a list of lists. Thus you want something like:

result1 = [['United States'], ['China'], ['Sweden'], ...]
con.executemany("INSERT INTO TableName(contry) VALUES(?)", result1)

Note that this (probably) runs 74 separate INSERT statements instead of 1 multi-row INSERT, depending on the database you're using. Certain higher-level SQL frameworks (like SQLAlchemy) provide tools to handle this better, and your specific flavor of database API may provide similar tools.

Lacking either of those, your other option to do a multiple-row insert is something like this: (Written much more verbosely than it needs to be)

import itertools
def insert_many(con, table, data):
    """
    Inserts multiple rows into a table.

    This will fail horribly if data is empty or the number of 
    parameters (len(data) * len(data[0])) exceeds the limits of your
    particular DBAPI.

    :param con: Database connection or cursor
    :param table: Name of table, optionally including columns.
        e.g. 'TableName' or 'TableName(country)'
    :param data: List of lists of data elements.  All inner lists must be the same length.
    """

    # Represents enough parameters for one set of values.  (1, in your case)
    one_value = "({qmarks})".format(qmarks=", ".join("?" for _ in rows[0]))
    # Represents all sets of values  (74 copies of the above, in your case)
    all_values = ", ".join(one_value for _ in data)

    # Flattened version of your 2-dimensional array.
    # The 'list' may not be required; I'm not certain offhand if execute() will accept an iterator.
    data = list(itertools.chain(*data))

    con.execute(
        "INSERT INTO {table} VALUES {values}".format(table=table, values=values),
        data
    )

insert_many(con, "TableName(country)", [['United States'], ['China'], ['Sweden'], ...])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.