Python MySQL statement error, please help!

Asked By 360 points N/A Posted on -
qa-featured

Hello everyone!

I always believed i am good in Python until this morning; a course mate came to my room with his laptop and presented me a code. It is simple Python MySQL statement returning an error.

I have tried to debug it, made a few changes but still it is not running.

Here is my final code after editing.

import os
import MySQLdb
import time

db = MySQLdb.connect(host="localhost", user="root", passwd="********", db="workspace")
cursor = db.cursor()

tailoutputfile = os.popen('tail -f syslog.log')
while 1:
        x = tailoutputfile.readline()  
        if len(x)==0:
                break
        y = x.split()
        if y[2] == 'BAD':
                timestring = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7]")
        if y[2] == 'GOOD':
                print y[4] + 't' + y[7]

This was the error message i got after trying to run the code

user@machine:~/$ python reader.py
Traceback (most recent call last):
  File "reader.py", line 17, in ?
    cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7]")
  File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/python2.4/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 that corresponds to                                                              your MySQL server version for the right syntax to use near '[4], y[7]' at line 1")
user@machine:~/$

 

I am going nuts.

I can't find a solution to this. Please help me.

SHARE
Best Answer by JohnyGarcia
Best Answer
Best Answer
Answered By 0 points N/A #107766

Python MySQL statement error, please help!

qa-featured

Looking at your code, there seems to be one tiny problem with your SQL command, as prompted by your error message.

On the eleventh line, in the cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7]"), your code seem to be missing an extra ')' right after the 'y[7]' part of the SQL command, this is important because it encloses the values part of your command, and this missing character gives the syntax error.

The command should look something like this:

cursor.execute("INSERT INTO releases (date, cat, name) values (timestring, y[4], y[7])")

Adding the missing character that gives the SQL syntax error message.

Answered By 15 points N/A #107767

Python MySQL statement error, please help!

qa-featured

From the error message, it seems that the problem s being caused by how your friend is storing the files.

He may have changed the location to which the files are supposed to be stored, and that is a folder on the primary hard drive that is named 'localhost'.

All the files that he is used to design the program in the PHP application should reside in there for the program to be able to execute properly on the local machine.

In case he downloaded the code from the internet, then you will need to customize the code, declare the directory paths to the folder location to which the files are supposed are supposed to be stored on your computer, and then try running the code again and see if everything works fine.

Clair Charles

Related Questions