Search code examples
pythondatabaselistinsertraw-input

INSERT python list into MySQL DB


I got some code but not really getting it working

listdata = [1, 2]
listdata1.insert(1, raw_input("Enter first thing into DB: ")
listdata2.insert(2, raw_input("Enter second thing into DB: ")

Then I make the DB, then

cursor.execute("INSERT INTO testdb (TABLE, TABLE) VALUES (%s, %s)", (listdata1, listdata2

Which gives me: NameError: name 'listdata1' is not defined

Is inserting this even possible this way? Or how could I get it to work?


Solution

  • the problem happens here:

    listdata1.insert(1, raw_input("Enter first thing into DB: ")
    

    You did not define the variable listdata1 yet. Change it to listdata and you will fix the NameError.

    Besides, you can append instead of insert e.g.

    listdata.append( raw_input("Enter first thing into DB: ") )

    Then the raw input will then be kept in position 0 in listdata, and you can retrieve it by listdata[0]

    The sql looks wrong too ('TABLE' appear twice)