Re: importing csv file into sqlite

Discussion in 'Python' started by Chris Rebert, Dec 18, 2008.

  1. Chris Rebert

    Chris Rebert Guest

    On Wed, Dec 17, 2008 at 11:20 PM, klia <> 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()
     
    Chris Rebert, Dec 18, 2008
    #1
    1. Advertising

  2. Chris Rebert

    Peter Otten Guest

    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
     
    Peter Otten, Dec 18, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. klia
    Replies:
    0
    Views:
    1,019
  2. klia
    Replies:
    0
    Views:
    302
  3. James Mills

    Re: importing csv file into sqlite

    James Mills, Dec 18, 2008, in forum: Python
    Replies:
    3
    Views:
    1,619
    John Machin
    Dec 19, 2008
  4. klia
    Replies:
    4
    Views:
    1,653
    John Machin
    Dec 19, 2008
  5. Carl Youngblood
    Replies:
    1
    Views:
    232
    Carl Youngblood
    Apr 9, 2005
Loading...

Share This Page