1

我试图将查询传递给mysql数据库这里是代码

#!/usr/bin/python
import sys

sql = "select count(distinct offering),count(learner),count(distinct learner),sum(transaction ) from EMS_data where offering like 'engineering'  and manager='patel' and fiscal like %s" % sys.argv[1]

在财政栏中,我输入了 2010 2011 2012 2013

在命令行中,我输入了文件名和 2013。但它向我展示了这个结果

mysql_exceptions.OperationalError: (1054, "Unknown column '2013' in 'where clause'")

我不是在搜索列,而是在列内包含。

4

1 回答 1

0

我建议使用MySQLdb 的预处理语句,如下所示:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb as mdb

con = mdb.connect('localhost', 'testuser', 'test623', 'testdb')

with con:    

    cur = con.cursor()

    cur.execute("UPDATE Writers SET Name = %s WHERE Id = %s", 
        ("Guy de Maupasant", "4"))        

    print "Number of rows updated:",  cur.rowcount

但是,您的直接问题可以通过以下方式解决:

sql = "select count(distinct offering),count(learner),count(distinct learner),sum(transaction ) from EMS_data where offering like 'engineering'  and manager='patel' and fiscal regexp '%s'" % sys.argv[1]
于 2013-06-15T15:57:51.567 回答