The mysql insert statement in flask reported an error: 1241, 'Operand should contain 1 column (s)

The code in

view.py is as follows:

.
c_location = request.form.getlist ("centering location")

    connection = db.engine.connect()
    mysql = text(
        "INSERT INTO livingexpenses_coal (c_location) values (:m);"
    )
    result = connection.execute(mysql,m=c_location)

.

now an error has been reported:
sqlalchemy.exc.OperationalError: (_ mysql_exceptions.OperationalError) (1241, "Operand should contain 1 column (s)") [SQL: "INSERT INTO livingexpenses_coal (c_location) values (% s);"] [parameters: (["1asdfeld," 5"])] (Background on this error at: http://sqlalche.me/e/e3q8)

that is, c_location is now an array and cannot input values, but the normal sql statement can use:
INSERT INTO table name (column name, column name) VALUES (column value, column value), (column value); to insert multiple data at one time. Please tell me how to modify my code. Thank you very much.


c_location since it is an array, you should take a single element as the SQL parameter, such as

    connection = db.engine.connect()
    mysql = text(
        "INSERT INTO livingexpenses_coal (c_location) values (:m);"
    )
    result = connection.execute(mysql,m=c_location[0])

or format the entire array (if possible), such as

    connection = db.engine.connect()
    mysql = text(
        "INSERT INTO livingexpenses_coal (c_location) values (:m);"
    )
    result = connection.execute(mysql,m=';'.join(c_location))
Menu