[python] sqlite3 newb question

Questions about programming languages and debugging
Post Reply
User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

[python] sqlite3 newb question

Post by moudy »

hello every one
I was trying few examples related to sqlite3, making a database, table, inserting data.
But from the very beginning, I came over this problem, and Im not what is exactly happening:

Code: Select all

>>> for r in ['email0','email1','email2']:
	cursor.executemany('INSERT INTO mytable VALUES(?)', r)

	
<sqlite3.Cursor object at 0x02C0CF20>
<sqlite3.Cursor object at 0x02C0CF20>
<sqlite3.Cursor object at 0x02C0CF20>
>>> connection.commit()
>>> cursor.execute('SELECT * FROM mytable')
<sqlite3.Cursor object at 0x02C0CF20>
>>> allentries=cursor.fetchall()
>>> allentries
[(u'e',), (u'm',), (u'a',), (u'i',), (u'l',), (u'0',), (u'e',), (u'm',), (u'a',), (u'i',), (u'l',), (u'1',), (u'e',), (u'm',), (u'a',), (u'i',), (u'l',), (u'2',)]

as you can see, I was trying to enter a list of emails, ended up inserting every letter of the string from the list into a row !
what do you think ?
mahmoud_shihab@hotmail.com

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: [python] sqlite3 newb question

Post by maboroshi »

Try this

Code: Select all

rows = [('email0'),
     ('email1'),
     ('email2')
     ]:
cursor.executemany('INSERT INTO mytable VALUES(?)', rows)
Let me know how it turns out :-)

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: [python] sqlite3 newb question

Post by moudy »

thanks for the reply mabs,
the code didn't work. I got this:

Code: Select all

	]:  
	
SyntaxError: invalid syntax
so then i did this:

Code: Select all

>>> rows = [('email0'),('email1'),('email2')]
>>> cursor.executemany('INSERT INTO mytable VALUES(?)', rows)

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    cursor.executemany('INSERT INTO mytable VALUES(?)', rows)
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 6 supplied.
so there must be some sort of external iteration over the list or some thing, i don't know really whats going wrong here, it should be simple and straight forward :/
mahmoud_shihab@hotmail.com

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Re: [python] sqlite3 newb question

Post by maboroshi »

take a look here for more detailed info ;)

Code: Select all

http://stackoverflow.com/questions/2092757/python-and-sqlite-insert-into-table

User avatar
moudy
Technology Enthusiast
Technology Enthusiast
Posts: 688
Joined: 10 Feb 2009, 17:00
15
Location: Beirut, Lebanon

Re: [python] sqlite3 newb question (solved)

Post by moudy »

i kept on trying alot, and the answer was very silly, it was right under my nose and i kept overlooking the answer !

Code: Select all

for i in l:
        cursor.execute('INSERT INTO mytable VALUES(?)', (i,))
Any way, ill keep on reading about the topic.
mahmoud_shihab@hotmail.com

Post Reply