importing csv file into sqlite

Chris Rebert clp at rebertia.com
Thu Dec 18 04:04:03 EST 2008


On Wed, Dec 17, 2008 at 11:20 PM, klia <alwaseem307ster at yahoo.com> wrote:
> klia wrote:
>>
>> hey guys, i have a hug .csv file which i need to insert it into sqlite
>> database using python.
>> my csv data looks like this
>> Birthday2,12/5/2008,HTC,this is my birthday
>> Sea,12/3/2008,kodak,sea
>> birthday4,14/3/2009,samsung,birthday
>> love,17/4/2009,SONY,view of island
>>
>> can any one give me a head start codes.
>>
>> thanks in advance
>>
> guys so far i came out with this but i get this error
> waseem at Linux:~/Project2$ python experment.py
> Traceback (most recent call last):
>  File "experment.py", line 13, in <module>
>    curse.execute('INSERT INTO photos VALUES (?,?,?,?)',item)
> sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current
> statement uses 4, and there are 1 supplied.
>
> here's the codes
>
> import sqlite3
> import csv
>
> f = open('/home/waseem/Project2/photos.txt')
> csv.field_size_limit(100000) #see below!
> input = csv.reader(f, delimiter='\t')
> conn = sqlite3.connect('/home/waseem/Project2/picutres.db')
> curse = conn.cursor()
>
> curse.execute('CREATE TABLE photos (Name VARCHAR(100) PRIMARY KEY, Date
> INTEGER, Make VARCHAR(50), Tag VARCHAR(100))')
>
> for item in input:
>        curse.execute('INSERT INTO photos VALUES (?,?,?,?)',item)

I believe you need to change 'item' to '*item' to expand the list in
the call so that the function gets 4 additional args rather than 1
additional arg that happens to be a list. But as I've never used the
DB-API before, this is just a guess.

Also, you should rename 'input' so that you don't shadow a built-in
function, and you might want to rename 'item' to something like 'row'
to emphasize that it is itself a list of items.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com

> curse.commit()



More information about the Python-list mailing list