Python: Text file insert to MySQL
Schedule
sschedule at gmail.com
Wed Oct 7 07:32:17 EDT 2009
That was great ! Now I am able to insert the values from the file.
Somehow I am not able to update a specific field with all the vaues in the
file. For eg:
Before the update my table contents are:
+-------+-------+
| first | last |
+-------+-------+
| Sara | Jones |
| Terry | Burns |
| Filiz | Khan |
+-------+-------+
When I do the update using the following code,
-------------------------------------------------------------------------------------------------------------------------------------------
import MySQLdb, csv, sys
conn = MySQLdb.connect (host = "localhost",user = "usr", passwd = "pass",db
= "db")
c = conn.cursor()
csv_data=csv.reader(file("b.txt"))
for row in csv_data:
print row
c.execute("UPDATE a SET last = %s", row)
#c.commit()
c.close()
---------------------------------------------------------------------------------------------------------------------------------------------
The table contents get updated with the last content of the input file . for
eg
+-------+------+
| first | last |
+-------+------+
| Sara | c |
| Terry | c |
| Filiz | c |
+-------+------+
the contents of the b.txt file:
a
b
c
Any kind of help would be greatly appreciated.
James
On Tue, Oct 6, 2009 at 9:33 PM, Gerhard Häring <gh at ghaering.de> wrote:
> Schedule wrote:
> > Hello,
> >
> > I am currenty using MySQL 5.1 community server and trying to import the
> > data of the comma delimited text file into the table using python 2.6
> > scripts. I have installed Mysqldb 1.2.2.
> >
> > follwoing is my script:
> > [...]
> > 7.
> > c.execute("INSERT INTO a (first, last) VALUES (%s, %s), row")
> > [...]
> > When I execute the statement I get the following error:
> >
> ------------------------------------------------------------------------------------
> > [...]
> > _mysql_exceptions.ProgrammingError: (1064, "You have an error in your
> > SQL syntax; check the manual tha
> > t corresponds to your MySQL server version for the right syntax to use
> > near '%s, %s), row' at line 1")
>
> You misplaced the closing quote.
>
> wrong: c.execute("INSERT INTO a (first, last) VALUES (%s, %s), row")
> correct: c.execute("INSERT INTO a (first, last) VALUES (%s, %s)", row)
>
>
> -- Gerhard
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091007/8116d707/attachment-0001.html>
More information about the Python-list
mailing list