【发布时间】:2021-06-23 16:57:00
【问题描述】:
得到了一个简单的脚本,如下所示。尝试运行脚本时,我得到了我无法弄清楚的回溯错误。错误和脚本如下。我正在根据需要运行 python 3.4。不确定到底发生了什么。任何帮助表示赞赏。它说它在导入时出现语法错误......但它是唯一的导入。
Traceback(最近一次调用最后一次): 文件“script.py”,第 1 行,在 导入 pymysql 文件“C:\Python34\lib\site-packages\pymysql_init_.py”,第 59 行,在 从 。导入连接#noqa:E402 文件“C:\Python34\lib\site-packages\pymysql\connections.py”,第 206 行 ): ^ SyntaxError: 无效语法
import pymysql
myConnection = pymysql.connect(host='localhost', user = 'root', password='******', database='accidents')
cur = myConnection.cursor()
cur.execute('SELECT vtype FROM vehicle_type WHERE vtype LIKE "%otorcycle%";')
cycleList = cur.fetchall()
selectSQL = ('''
SELECT t.vtype, a.accident_severity
FROM accidents_2016 AS a
JOIN vehicles_2016 AS v ON a.accident_index = v.Accident_Index
JOIN vehicle_type AS t ON v.Vehicle_Type = t.vcode
WHERE t.vtype LIKE %s
ORDER BY a.accident_severity;
''')
insertSQL = ('''
INSERT INTO accident_medians VALUES (%s, %s);
''')
for cycle in cycleList:
cur.execute(selectSQL,cycle[0])
accidents = cur.fetchall()
quotient, remainder = divmod(len(accidents),2)
if remainder:
med_sev = accidents[quotient][1]
else:
med_sev = (accidents[quotient][1] + accidents[quotient+2][1])/2
print('Finding median for',cycle[0])
cur.execute(insertSQL,(cycle[0],med_sev))
myConnection.commit()
myConnection.close()
这是来自connections.py。这似乎是标准安装文件。
def __init__(
self,
*,
user=None, # The first four arguments is based on DB-API 2.0 recommendation.
password="",
host=None,
database=None,
unix_socket=None,
port=0,
charset="",
sql_mode=None,
read_default_file=None,
conv=None,
use_unicode=True,
client_flag=0,
cursorclass=Cursor,
init_command=None,
connect_timeout=10,
read_default_group=None,
autocommit=False,
local_infile=False,
max_allowed_packet=16 * 1024 * 1024,
defer_connect=False,
auth_plugin_map=None,
read_timeout=None,
write_timeout=None,
bind_address=None,
binary_prefix=False,
program_name=None,
server_public_key=None,
ssl=None,
ssl_ca=None,
ssl_cert=None,
ssl_disabled=None,
ssl_key=None,
ssl_verify_cert=None,
ssl_verify_identity=None,
compress=None, # not supported
named_pipe=None, # not supported
passwd=None, # deprecated
db=None, # deprecated
): -- line 206
【问题讨论】:
-
你能打开文件
C:\Python34\lib\site-packages\pymysql\connections.py并把第206行贴在这里吗? -
我将它添加到上面的主帖中。它以 ' ): ' 结尾,我用 -- 第 206 行标记了它
-
在无效语法之后还有什么意思吗?我可以在 python 3.9.2 上毫无问题地运行该 def init。没有任何语法错误
-
否定的。我发布的是整个追溯。
-
你会尝试用更新的 python 运行它吗?
标签: python mysql pymysql script