0

I have two or more lists, for example:

    name = ['Chris', 'John', 'Louis']
    surname = ['Brown', 'Green', 'Red']
    car = ['Audi', 'Mercedes', 'Ferrari']

What I'd like to achieve is creating an sql database where every list is a column so that it would be something like: "Chris" in the first line, "Brown" in the second line and "Audi" in the third one - and so on. My actual python script is the following one but I can't figure out how I can save the list:

    connection = sqlite3.connect("name.db")                       
    cursor = connection.cursor()                                  
    cursor.execute("DROP TABLE IF EXISTS peopleList")           
    cursor.execute("CREATE TABLE peopleList(Name TEXT, Surname TEXT, Car TEXT)")

1 Answer 1

7

You can use zip() and executemany() like so:

cursor.executemany("""INSERT INTO peopleList (Name, Surname, Car) VALUES (?,?,?)""", 
                   zip(name,surname,car))
Sign up to request clarification or add additional context in comments.

4 Comments

I got a traceback error: sqlite3.OperationalError: near "%": syntax error
@antonioag: oh I see now you're using SQLite3. The question was titled and tagged with MySQL so that confused me. Please see edited answer which should work.
Sorry for the wrong tag and thank you for your time. Anyway, no more errors but the file created has 0 rows
Make sure you do connection.commit() after the INSERT statement.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.