簡體   English   中英

1064SQL語法有錯誤。 以便在第1行的'%s'附近使用正確的語法”)

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

我有一個包含字段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->->

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>

請幫助我了解此錯誤:

您在這里有兩個問題(實際上您的代碼有兩個以上的問題,但這是另一個問題):第一個問題是cursor.execute()第二個參數應該是列表或元組-此處:

 cur.execute(sql,(uuid))

您實際上想要:

 cur.execute(sql,(uuid,))

注意uuid之后的逗號-它是定義元組而不是括號的逗號。

第二個問題是您將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM