con = pymysql.connect(user="root",
password=pw,
host="LocalHost",
database=None,
port=3306,
charset="utf8")
# 创建游标
cur = con.cursor()
cur.execute("drop database %s;", "douban") # 这样写会出错
# cur.execute("drop database douban;") #这样写不会出错
为什么倒数第二行会报错?把倒数第二行换成倒数第一行就不会报错?
报错如下
File "D:/PyQt5/滤波/爬虫/豆瓣电影.py", line 34, in mysql_data
cur.execute("drop database %s;", 'douban')
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
result.read()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
packet.raise_for_error()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "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 ''douban'' at line 1")
Process finished with exit code 1
cur.execute("drop database %s;" % "douban")