Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: importing csv file into sqlite

Reply
Thread Tools

Re: importing csv file into sqlite

 
 
Chris Rebert
Guest
Posts: n/a
 
      12-18-2008
On Wed, Dec 17, 2008 at 11:20 PM, klia <(E-Mail Removed)> 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@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()

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-18-2008
Chris Rebert wrote:

>> klia wrote:


>> 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.


No, the execute() method call is correct. There must be a malformed row in
the csv with only one field.

Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: importing csv file into sqlite James Mills Python 3 12-19-2008 02:08 PM
Re: importing csv file into sqlite klia Python 4 12-19-2008 01:58 PM
importing csv file into sqlite klia Python 0 12-18-2008 05:58 AM
importing .txt data into sqlite using python klia Python 0 12-17-2008 07:03 AM
Installing sqlite-ruby when sqlite is in non-standard location Carl Youngblood Ruby 1 04-09-2005 03:32 AM



Advertisments