1064,“您的SQL语法有错误;要在第1行'%s'附近使用正确的语法”

时间:2018-05-02 10:32:47

标签: python pymysql

我的数据库包含字段,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:

#!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()

我正在尝试从admin.py导入变量UUID到approve_mail.py:

但是我收到了这个错误; 内容类型:text / html - &gt; - &GT;

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>

请帮我理解这个错误:

1 个答案:

答案 0 :(得分:0)

这里有两个问题(实际上你的代码有两个以上的问题,但这是另一个问题):第一个问题是cursor.execute()第二个参数应该是一个列表或元组 - 这里:

 cur.execute(sql,(uuid))

你真的想要:

 cur.execute(sql,(uuid,))

注意uuid之后的逗号 - 它是定义元组的逗号,而不是parens。

第二个问题是,您将None作为uuid的值传递,参见您的追溯:

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