简体   繁体   English

1064SQL语法有错误。 以便在第1行的'%s'附近使用正确的语法”)

[英]1064, “You have an error in your SQL syntax; for the right syntax to use near '%s' at line 1”)

I have the database which has the fields,id, source_server,source_path,destination_path I am trying to get the data from the mysql database using pymysql based on id: admin.html 我有一个包含字段id,source_server,source_path,destination_path的数据库,我正在尝试使用基于id的pymysql从mysql数据库中获取数据: admin.html

<form action = "admin_1.py" method = "POST">
    <label for="uname"><b>User UUID::</b></label>
    <input type="text" placeholder="Enter Username" name="uuid" required>
    <br>
    <br >
    <br >
    <br >
    <br >
    <button type="submit">click here!!</button>
</form>

Admin.py: Admin.py:

#!C:\Program Files\Python\Python36\python.exe

print ("Content-type: text/html\r\n")
print("")
import cgi,cgitb
import pymysql
form = cgi.FieldStorage() 
cgitb.enable()
UUID = form.getvalue('uuid')

db = pymysql.connect(host="localhost",  user="root", passwd="", db="fullstack")# name of the data base       
cur = db.cursor()

def select_from_sql(uuid):
        #(self.cur).execute('''select (id, source_server,source_path,destination_path) values (%s, %s,%s,%s) from posts''', (self.id,self.source_server,self.source_path,self.destination_path) )
    sql="SELECT * FROM posts WHERE id=%s"
    cur.execute(sql,(uuid))
    row=cur.fetchall()
select_from_sql(UUID)
cur.close()
print("<center>")
print('<html>')
print('<form action="approve_mail.py" method="POST">')
print('<input type="submit" value="Approve" name="approve" />')
print('</form>')
print('</html>')
print("</center>")

cur.close()
db.close()


**Approve_admin.py:**

#!C:\Program Files\Python\Python36\python.exe

from admin import UUID

print("Content-type: text/html")
print("")
import cgi,cgitb
def sending():
      print (UUID)

sending()

I am trying to import the variable UUID to approve_mail.py from admin.py: 我正在尝试从admin.py导入变量UUID到approve_mail.py:

But I am getting this error; 但是我得到这个错误; Content-type: text/html --> --> 内容类型:text / html->->

ProgrammingError    Python 3.6.0: C:\Program Files\Python\Python36\python.exe
Wed May 2 15:55:10 2018
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

 C:\xampp\htdocs\sample\approve_mail.py in ()
     24 import uuid
     25 import pymysql
=>   26 from admin_1 import UUID
     27 from time import sleep
     28 form = cgi.FieldStorage() 
admin_1 undefined, UUID undefined
 C:\xampp\htdocs\sample\admin_1.py in ()
     29                 print("<br>")
     30                 print("</center>")
=>   31 select_from_sql(UUID)
     32 cur.close()
     33 print("<center>")
select_from_sql = <function select_from_sql>, UUID = None
 C:\xampp\htdocs\sample\admin_1.py in select_from_sql(uuid=None)
     15                 #(self.cur).execute('''select (id, source_server,source_path,destination_path) values (%s, %s,%s,%s) from posts''', (self.id,self.source_server,self.source_path,self.destination_path) )
     16         sql="SELECT * FROM posts WHERE id=%s"
=>   17         cur.execute(sql,(uuid))
     18         row=cur.fetchall()
     19         for i in row:
global cur = <pymysql.cursors.Cursor object>, cur.execute = <bound method Cursor.execute of <pymysql.cursors.Cursor object>>, sql = 'SELECT * FROM posts WHERE id=%s', uuid = None
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\cursors.py in execute(self=<pymysql.cursors.Cursor object>, query='SELECT * FROM posts WHERE id=%s', args=None)
    163         query = self.mogrify(query, args)
    164 
=>  165         result = self._query(query)
    166         self._executed = query
    167         return result
result undefined, self = <pymysql.cursors.Cursor object>, self._query = <bound method Cursor._query of <pymysql.cursors.Cursor object>>, query = 'SELECT * FROM posts WHERE id=%s'
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\cursors.py in _query(self=<pymysql.cursors.Cursor object>, q='SELECT * FROM posts WHERE id=%s')
    319         conn = self._get_db()
    320         self._last_executed = q
=>  321         conn.query(q)
    322         self._do_get_result()
    323         return self.rowcount
conn = <pymysql.connections.Connection object>, conn.query = <bound method Connection.query of <pymysql.connections.Connection object>>, q = 'SELECT * FROM posts WHERE id=%s'
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\connections.py in query(self=<pymysql.connections.Connection object>, sql=b'SELECT * FROM posts WHERE id=%s', unbuffered=False)
    858                 sql = sql.encode(self.encoding, 'surrogateescape')
    859         self._execute_command(COMMAND.COM_QUERY, sql)
=>  860         self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    861         return self._affected_rows
    862 
self = <pymysql.connections.Connection object>, self._affected_rows = 0, self._read_query_result = <bound method Connection._read_query_result of <pymysql.connections.Connection object>>, unbuffered = False
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\connections.py in _read_query_result(self=<pymysql.connections.Connection object>, unbuffered=False)
   1059         else:
   1060             result = MySQLResult(self)
=> 1061             result.read()
   1062         self._result = result
   1063         if result.server_status is not None:
result = <pymysql.connections.MySQLResult object>, result.read = <bound method MySQLResult.read of <pymysql.connections.MySQLResult object>>
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\connections.py in read(self=<pymysql.connections.MySQLResult object>)
   1347     def read(self):
   1348         try:
=> 1349             first_packet = self.connection._read_packet()
   1350 
   1351             if first_packet.is_ok_packet():
first_packet undefined, self = <pymysql.connections.MySQLResult object>, self.connection = None, self.connection._read_packet undefined
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\connections.py in _read_packet(self=<pymysql.connections.Connection object>, packet_type=<class 'pymysql.connections.MysqlPacket'>)
   1016 
   1017         packet = packet_type(buff, self.encoding)
=> 1018         packet.check_error()
   1019         return packet
   1020 
packet = <pymysql.connections.MysqlPacket object>, packet.check_error = <bound method MysqlPacket.check_error of <pymysql.connections.MysqlPacket object>>
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\connections.py in check_error(self=<pymysql.connections.MysqlPacket object>)
    382             errno = self.read_uint16()
    383             if DEBUG: print("errno =", errno)
=>  384             err.raise_mysql_exception(self._data)
    385 
    386     def dump(self):
global err = <module 'pymysql.err' from 'C:\\Program Files\\P...\\Python36\\lib\\site-packages\\pymysql\\err.py'>, err.raise_mysql_exception = <function raise_mysql_exception>, self = <pymysql.connections.MysqlPacket object>, self._data = b"\xff(\x04#42000You have an error in your SQL s... for the right syntax to use near '%s' at line 1"
 C:\Program Files\Python\Python36\lib\site-packages\pymysql\err.py in raise_mysql_exception(data=b"\xff(\x04#42000You have an error in your SQL s... for the right syntax to use near '%s' at line 1")
    103         errval = data[9:].decode('utf-8', 'replace')
    104     else:
    105         errval = data[3:].decode('utf-8', 'replace')
    106     errorclass = error_map.get(errno, InternalError)
=>  107     raise errorclass(errno, errval)
errorclass = <class 'pymysql.err.ProgrammingError'>, errno = 1064, errval = "You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1"
ProgrammingError: (1064, "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 '%s' at line 1") 
      args = (1064, "You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1") 
      with_traceback = <built-in method with_traceback of ProgrammingError object>

Please help me to understand about this error: 请帮助我了解此错误:

You have two problems here (well actually there are more than two problems with your code but that's another question): the first one is that cursor.execute() second argument should be a list or tuple - here: 您在这里有两个问题(实际上您的代码有两个以上的问题,但这是另一个问题):第一个问题是cursor.execute()第二个参数应该是列表或元组-此处:

 cur.execute(sql,(uuid))

You actually want: 您实际上想要:

 cur.execute(sql,(uuid,))

Notice the comma after uuid - it's the comma that defines a tuple, not the parens. 注意uuid之后的逗号-它是定义元组而不是括号的逗号。

The second issue is that you are passing None as value for your uuid , cf your traceback: 第二个问题是您将None作为uuid值传递给了您,回溯到:

select_from_sql = <function select_from_sql>, UUID = None
C:\xampp\htdocs\sample\admin_1.py in select_from_sql(uuid=None)

暂无
暂无

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

相关问题 mysql.connector.errors.ProgrammingError: 1064 (42000): 你的 SQL 语法有错误; 在第 1 行的“)”附近使用正确的语法 - mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; for the right syntax to use near ')' at line 1 错误 1064 (42000):您的 SQL 语法中存在错误,接近使用 %s - error 1064 (42000) : You have an error in your SQL syntax near to use %s _mysql_exceptions.ProgrammingError:(1064,“您的SQ L语法有错误;在第1行的&#39;)&#39;附近使用了正确的语法”) - _mysql_exceptions.ProgrammingError: (1064, “You have an error in your SQ L syntax; right syntax to use near ')' at line 1”) 您的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", “你的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在&#39;&#39;第1行&#39;附近使用正确的语法 - “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” 1064, &quot;你的 SQL 语法有错误 - 1064, "You have an error in your SQL syntax 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 服务器版本相对应的手册,以获取在 \'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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM