1

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?

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.