user10649535
user10649535

Reputation:

How to insert values from input list into table in database?

Write a function named "add_list" that takes a list as a parameter where each value is itself a list containing 3 strings. This function doesn't return a value. There is a database saved in a file named "rarely.db" containing a table named "big" with columns "ought", "spray", and "courtroom". Insert each value from the input list into this table using the 3 strings in each list as the values for the three columns.

import sqlite3
    def add_list(lista):
        conn=sqlite3.connect('rarely.db')
        c=conn.cursor()
        ought1 = c.execute('SELECT ought FROM big').fetchall() 
        spray1 = c.execute('SELECT spray FROM big').fetchall() 
        courtroom1 = c.execute('SELECT courtroom FROM big').fetchall()
        insert = c.execute('INSERT INTO big VALUES(?)', (lista))
        conn.commit()
        conn.close()

Here is my attempt at it. What am I doing wrong?

Upvotes: 1

Views: 732

Answers (1)

Ajax1234
Ajax1234

Reputation: 71471

You can use executemany:

import sqlite3
def add_list(lista):
   conn=sqlite3.connect('rarely.db')
   c=conn.cursor()
   insert = c.executemany('INSERT INTO big VALUES (?, ?, ?)', lista)
   conn.commit()
   conn.close()

Upvotes: 1

Related Questions