Python: Text file insert to MySQL
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: 1. import MySQLdb, csv, sys 2. conn = MySQLdb.connect (host = "localhost",user = "usr", passwd = "pass",db = "databasename") 3. c = conn.cursor() 4. csv_data=csv.reader(file("a.txt")) 5. for row in csv_data: 6. print row 7. c.execute("INSERT INTO a (first, last) VALUES (%s, %s), row") 8. c.commit() 9. c.close() import MySQLdb, csv, sys conn = MySQLdb.connect (host = "localhost",user = "usr", passwd = "pass",db = "databasename") c = conn.cursor() csv_data=csv.reader(file("a.txt")) for row in csv_data: print row c.execute("INSERT INTO a (first, last) VALUES (%s, %s), row") c.commit() c.close() the contents of the text file eg. : - John,Smith Danie,Thomas Ronald,Rey When I execute the statement I get the following error: C:\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecate d from sets import ImmutableSet ['John', 'Smith'] Traceback (most recent call last): File "e:\Scripts\test.py", line 10, in c.execute("INSERT INTO a (first, last) VALUES (%s, %s), row") File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 166, in execute self.errorhandler(self, exc, value) File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler raise errorclass, errorvalue _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") Any kind of help to get me going will be greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python: Text file insert to MySQL
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 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 > -- http://mail.python.org/mailman/listinfo/python-list
Submit HTTP GET data from XP PC to PHP on web server.
Hello everyone I haeve tried to understand the capabilities of python in web development. Have gone throuhg forums and online tutorials. Nevertheless I am not able to find any topic which can give me some insite along with basic examples of how to send http get data from an xp mashine using python code to php on web server. I have some parameters to collect from windows mashine via http get and collect them on web server using php. Any hint how to begin would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list