[Tutor] Multi-Line SQL Statement in Python

Tom Tucker tktucker at gmail.com
Wed Nov 24 04:55:20 CET 2004


John,
Here is a quickie that includes the multi-lines and the "LIKE%" statement.

#!/usr/bin/python
import MySQLdb, sys

sqlquery = """SELECT Host, User \
              FROM user \
              WHERE host LIKE \'localhost%\'"""

try:
        db = MySQLdb.connect(host="localhost", user="root", db="mysql")

        cursor = db.cursor()
        cursor.execute(sqlquery)
        result = cursor.fetchall()
        for record in result:
                print record[0] , record[1]

except MySQLdb.Error, e:
        print "Error %d: %s,\n" % (e.args[0], e.args[1])

cursor.close()
db.close()


Tom


More information about the Tutor mailing list