使用脚本对pymysql进行回溯

2024-03-29 11:46:13 发布

您现在位置:Python中文网/ 问答频道 /正文

下面给出了一个简单的脚本。当试图运行脚本时,我得到了无法完全理解的回溯错误。错误和脚本如下所示。我正在按要求运行Python3.4。不确定到底发生了什么。感谢您的帮助。它说这是导入时的语法错误。。。然而,这是唯一的进口

回溯(最近一次呼叫最后一次): 文件“script.py”,第1行,在 导入pymysql 文件“C:\Python34\lib\site packages\pymysql\uem>init\um.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

Tags: 文件py脚本nonefalsesslinitcur