Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Admittedly I a still very new to both Python and Sqlite3, and I am attempting to add the contents of two lists into a database so that one list is in the first column and the second list shows up in the second column. To this point, I have been unsuccessful. I am defenitely making a fundamental error, and the error message that I get is this: "sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type."

my code is this:

import sqlite3
names = ['Tom', 'Dick', 'Harry']

ids = ['A452', 'B698', 'Kd9f']


conn = sqlite3.connect('testforinput.db')
c = conn.cursor()

c.execute("CREATE TABLE thetable(name TEXT, id TEXT)")

index = 0
for link in names:
    idofperson = ids[index]
    c.execute("INSERT INTO thetable(name, id)VALUES(?, ?)", ( [link], idofperson ))
    index+=1
conn.commit()

conn.close()

The error occurs because of the for loop specifically the "idofperson" variable

The desired outcome is that I would like to have two columns created in sql one being name and the other being id.

Any help would be greatly appreciated.

share|improve this question

I think you just change

index =0
for link in names:
idofperson = ids[index]
c.execute("INSERT INTO thetable(name, id)VALUES(?, ?)", ( [link], idofperson ))

to this (use enumrate and change [list] to list, because you pass a list into a column need TEXT type):

for index, link in enumrable(names):
idofperson = ids[index]
c.execute("INSERT INTO thetable(name, id)VALUES(?, ?)", ( link, idofperson ))
share|improve this answer
1  
I'll try that now and let you know if it works. Thanks for the input – Kevin 18 hours ago
1  
Shoot! no luck! but ill do some further research on enumerate and see if that is the key to it all. Thanks again – Kevin 18 hours ago
    
No, I error not there. Your error is type because you pass a 'list' parameter into a column need type TEXT – Artiel 18 hours ago

your variable index is not increasing.try using the enumerate on for loop. or just add index += 1 after execute

share|improve this answer
    
Thanks bro forgot about that, I added input+=1 after the execute statement but I still get the same error :( – Kevin 18 hours ago
    
can you try to remove the extra parenthesis – Randy Arguelles 18 hours ago
    
c.execute("INSERT INTO thetable(name, id)VALUES(?, ?)", [link], idofperson ) – Randy Arguelles 18 hours ago
    
oops sorry my mistake . how to edit this comment:D – Randy Arguelles 18 hours ago

for your problem, try this:

import sqlite3

names = ['Tom', 'Dick', 'Harry']
ids = ['A452', 'B698', 'Kd9f']
data = zip(names, ids)
conn = sqlite3.connect('testforinput.db')
c = conn.cursor()
c.execute("CREATE TABLE thetable(name TEXT, id TEXT)")


for d in data:
    sql = "INSERT INTO thetable (name, id) VALUES ('%s', '%s'); " % d
    c.execute(sql)

conn.commit()
conn.close()

I suggest change data to a list of dict, like this [{'name':'Tom', 'id': 'A452'}, {'name':'dick', 'id':'B698'}..] and you can generate insert sql by data, this make the insert more flexible.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.