1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] how to insert an array into database in python?

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 9, 2024 às 07:42.

  1. Stack

    Stack Membro Participativo

    I have a database called my_python and I have a table called my_transport. There are 3 columns in the table: id, transport, and fee. for the first columns "id", I make it auto increment so that I don't have to insert any value into it. My question is how to insert the value of my_trans and my_fee into the database table?

    import mysql.connector

    my_trans=['car','train','ship','train']
    my_fee=[200,300,150,200]

    try:
    connection = mysql.connector.connect(host='localhost',
    database='my-python',
    user='root',
    password='')
    sql_insert_query = """INSERT INTO my_transport
    (`transport`, `fee`) VALUES (my_trans, my_fee)"""
    cursor = connection.cursor()
    result = cursor.execute(sql_insert_query)
    connection.commit()
    print ("Record inserted successfully into table") except mysql.connector.Error as error :
    connection.rollback() #rollback if any exception occured
    print("Failed inserting record into table {}".format(error)) finally:
    #closing database connection.
    if(connection.is_connected()):
    cursor.close()
    connection.close()
    print("MySQL connection is closed")


    I have try below code but it said:

    "Failed inserting record into table 1054 (42S22): Unknown column 'my_trans' in 'field list'"

    Continue reading...

Compartilhe esta Página