importing csv file into sqlite

John Machin sjmachin at lexicon.net
Fri Dec 19 08:58:57 EST 2008


On Dec 19, 11:17 pm, klia <alwaseem307s... at yahoo.com> wrote:

[ancient screed snipped]

>
> hey guys
> i took all of your suggestion but my goal ain't yet achieved :-((
> these are the codes after changes, john i couldn't really catch what do you
> mean by renaming input, is it just normal renaming.

Somebody else told you not to use "input", choose another name

> i am testing the code on
> just simple .csv file with few data in as follows before trying on my hug
> csv file but still no joy
>
> "Bithday",12-05-08,"HTC","this is my birthday"
> "Sea",12-03-08,"kodak","sea"
> "girl","14-03-2009","samsung","birthday"
> "love","17-04-2009","SONY","view of island"
>
> import sqlite3
> import csv
>
> f = open('/home/waseem/Project2/photos.csv')
> input = csv.reader(f, delimiter=',')
> conn = sqlite3.connect('/home/waseem/Project2/pictures.db')
> curse = conn.cursor()
>
> curse.execute('CREATE TABLE photos (Name VARCHAR(100) PRIMARY KEY, Date
> INTEGER, Make VARCHAR(50), Tag VARCHAR(100))')
> for row in input:
>         curse.execute('INSERT INTO photos VALUES (?,?,?,?)', '*row')
> curse.commit()
>
> this time i got this error
>
> waseem at Linux:~/Project2$ python experment.py
> Traceback (most recent call last):
>   File "experment.py", line 12, in <module>
>     curse.execute('INSERT INTO photos VALUES (?,?,?,?)', '*row')
> sqlite3.IntegrityError: column Name is not unique
>
> i removed the primary key and single quotation mark for '*row' to just *row
> but i got the old error which is
>
> waseem at Linux:~/Project2$ python experment.py
> Traceback (most recent call last):
>   File "experment.py", line 11, in <module>
>     curse.execute('INSERT INTO photos VALUES (?,?,?,?)', *row)
> TypeError: function takes at most 2 arguments (5 given)
>

No, the old error was """sqlite3.ProgrammingError: Incorrect number of
bindings supplied. The current statement uses 4, and there are 1
supplied.""" That's quite different.

Secondly you have already been told about the difference between row
and *row ... as the message says, it wants 2 arguments, (1) the SQL
(2) the sequence of values that will be used to replace the ?s in the
SQL. So just lose the *, OK?






More information about the Python-list mailing list