简体   繁体   English

42000您的SQL语法错误; 检查与您的MySQL服务器相对应的手册

[英]42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server

I'm having trouble with my Python odbc code. 我的Python odbc代码遇到问题。 I don't get this following code to work: 我没有下面的代码工作:

temp=process_query("SELECT FName, LName FROM Employee WHERE SSN='%s'" %i)

known_hours=process_query("SELECT DISTINCT Coalesce(Hours,0) 
FROM Works_On WHERE ESSN='%s'" %i)

 temp.append(known_hours)

where process_query takes the form: 其中process_query采用以下形式:

def process_query(query):
    cursor1.execute(str(query))

(process_query continues some more but that is merely for printing purposes, when I've searched the web for my problem it seems that the problem lies within how I call the execute function so I omitted the rest of the function here). (process_query还有更多内容,但这只是出于打印目的,当我在网上搜索问题时,问题似乎出在我如何调用execute函数之内,因此在此省略了其余功能)。

The error I receive when I'm trying to execute this program is: 尝试执行此程序时收到的错误是:

pyodbc.ProgrammingError: ('42000', "[42000] [MySQL][ODBC 5.1 Driver][mysqld-5.1.66-0+squeeze1-log]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 'John', [Decimal('32.5'), Decimal('7.5')], 'Yes']'' at line 1 (1064) (SQLExecDirectW)") pyodbc.ProgrammingError :(“ 42000”,“ [42000] [MySQL] [ODBC 5.1驱动程序] [mysqld-5.1.66-0 + squeeze1-log]您的SQL语法有误;请查看与您的手册相对应的手册MySQL服务器版本,用于在第1行(1064)(SQLExecDirectW)“)附近在'John',[Decimal('32 .5'),Decimal('7.5')],'Yes']''附近使用的正确语法

PS. PS。 If someone knows how to omit the "Decimal" when I'm printing and instead just have for instance 32.5 that would be appreciated as well to get help with. 如果有人在我打印时知道如何省略“十进制”,而仅举32.5,那么在获得帮助时也将不胜感激。

Also I know that it has been several topics regarding this, but I am yet to see and understand the problem I have for a select statement. 我也知道这涉及到多个主题,但是我还没有看到并理解选择语句的问题。

Edit 编辑

Regarding how "i" is implemented it is as following: 关于“ i”的实现方式如下:

I have an initial list called theList which contains all relevant social security numbers, SSN, which I then "loop" through like this: 我有一个名为theList的初始列表,其中包含所有相关的社会保险号SSN,然后我将其像这样“循环”通过:

for i in theList:

    temp=process_query("SELECT FName, LName FROM Employee WHERE SSN='%s'" %i)

    known_hours=process_query("SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN='%s'" %i)

    temp.append(known_hours)
    unknown_hours=process_query("SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN='%s'" %i)

    temp.append(unknown_hours)

Edit 编辑

I've changed it as beargle suggested. 我已按照胡子的建议将其更改。 However I now run into another error namely that since I loop through i (the social security numbers) in theList I have to define these before. 但是,我现在遇到另一个错误,即由于我遍历列表中的i(社会保险号),因此必须在之前定义它们。

Hence I use the line 因此,我用线

theList=process_query('SELECT DISTINCT SSN FROM Employee', None)

Where I've updated my process_query to: 我将process_query更新为的位置:

def process_query(query, parameters):

    if(parameters is None):

        cursor1.execute(query)

    else:

        cursor1.execute(query, parameters)  (*)

    n=0

    lista = []

    while 1:

        row = cursor1.fetchone()        

        if not row:

            break

        lista.append(row[0])

        n = n+1

    if n==0:

        print "No tuples matching the given query were found."

    return lista    

The trouble now is that the program complains at the second cursor1.execute (marked with a asterix, *) that ('The SQL contains 1 parameter markers, but 4 parameters were supplied', 'HY000') which I believe stems from that i is a social secuirity number and thus is not a single digit integer, but I cannot understand how to fix this issue. 现在的麻烦是程序在第二个cursor1.execute(标有星号*)上抱怨(SQL包含1个参数标记,但是提供了4个参数,'HY000'),我相信这是因为是一个社会安全号码,因此不是一个整数,但是我无法理解如何解决此问题。

I now call my as: 我现在称我为:

temp=process_query('SELECT FName, LName FROM Employee WHERE SSN= ?', i)

known_hours=process_query('SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?', i)

the i's in theList is identified before the loop defining temp and such as follows: List中的i是在定义temp的循环之前标识的,如下所示:

theList=process_query('SELECT DISTINCT SSN FROM Employee', None)

which removed all other errors but got me a new one as stated previously. 删除了所有其他错误,但如前所述为我带来了一个新错误。 Also, I tried to convert the "i" to int(i) and map(int,i) without getting a release from my error. 另外,我尝试将“ i”转换为int(i)和map(int,i),而没有从错误中释放出来。

Use query parameters in the cursor.execute() call of your process_query function. process_query函数的cursor.execute()调用中使用查询参数 This will handle any escaping issues (protecting your code against SQL injection) and promotes statement preparation . 这将处理所有转义问题(保护您的代码免遭SQL注入)并促进语句准备

Change the process_query function to accept two parameters, one for the SQL string (containing parameter value placeholder) and one for parameter values: 更改process_query函数以接受两个参数,一个用于SQL字符串(包含参数值占位符),另一个用于参数值:

def process_query(sql, params):
    cursor1.execute(sql, params)

The for loop would then change to: 然后for循环将变为:

for i in theList:
    temp=process_query('SELECT FName, LName FROM Employee WHERE SSN=?', i)
    known_hours=process_query('SELECT DISTINCT SUM(Coalesce(Hours,0)) FROM Works_On WHERE ESSN=?', i)
    temp.append(known_hours)
    unknown_hours=process_query('SELECT Distinct COUNT(*) FROM Works_On WHERE ISNULL(Hours) AND ESSN=?', i)
    temp.append(unknown_hours)

If this doesn't resolve the syntax problem, update your question with the query string causing the problem. 如果这样做不能解决语法问题,请使用引起问题的查询字符串更新您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 1064(42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法使用 - 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use 您的 SQL 语法有错误; 检查与您的 MySQL 服务器 PYTHON 相对应的手册 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server PYTHON 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 ') 附近使用的正确语法 - 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 ') “你的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在''第1行'附近使用正确的语法 - “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 '' at line 1” 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 \'keyword")\' 附近使用的正确语法 - 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 \'keyword")\' 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,以获取在 '*) 附近使用的正确语法 - 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 '*) 警告:您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法 - Warning: 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 您的SQL语法有错误; 检查与您的MariaDB服务器版本对应的手册,以便在第1行附近使用正确的语法, - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near at line 1", MYSQL 数据库错误:“pymysql.err.ProgrammingError: (1064, ”您的 SQL 语法有错误;请查看与您对应的手册-" - MYSQL Database error: “pymysql.err.ProgrammingError: (1064, ”You have an error in your SQL syntax; check the manual that corresponds to your-" sqlalchemy.exc.ProgrammingError:您的SQL语法有错误; 查看与MariaDB服务器版本对应的手册 - sqlalchemy.exc.ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM