irishdave22

Python/MySQL syntax error

Discussion created by irishdave22 on May 11, 2014
Latest reply on May 11, 2014 by irishdave22
Hi All,

First off I'm new to Python coming from a Java/C++ background. I've created a simple Python program that will connect to a database and query it returning the results from the querys. I receive a nasty syntax error message which I cannot spot in my code. Maybe one of ye can? Im running Python 2.7 and MySQL 5.6. Wingware is the IDE I'm using on a Windows 8.1 OS.

import MySQLdb

class Database:
    
    host = "localhost"
    user = "testuser"
    passwd = "password"
    db = "test"
    
    def __init__(self):
        self.connection = MySQLdb.connect(host = self.host,
                                          user = self.user,
                                          passwd = self.passwd,
                                          db = self.db
                                         )
    def query(self, q):
        cursor = self.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(q)
        
        return cursor.fetchall()
    
    def __del__(self):
        self.connection.close()


if __name__ == "__main__":
    db = Database()
    
    q = "use test"
    db.query(q)
    
    q = "DELETE FROM testtable"
    db.query(q)
    
    q = """
    INSERT INTO testtable
    ('name', 'age')
    VALUES
    ('Mike',39),
    ('Michael',21),
    ('Angela', 21)
    """
    
    db.query(q)
    
    q = """
    SELECT * FROM testtable
    WHERE age = 21
    """
    
    people = db.query(q)
    
    for person in people:
        print "Found: {0}".format(person['name'])


This is the error message:
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 ''name', 'age')\n    VALUES\n    ('Mike',39),\n    ('Michael',21),\n    ('Angela', 21' at line 2")

Thanks in advance guys!
Dave.

Outcomes