gpt4 book ai didi

python - 从动态列表python创建mysql表

转载 作者:行者123 更新时间:2023-11-29 21:32:29 24 4
gpt4 key购买 nike

使用动态列表创建表时出现问题。我在“退出”旁边不断收到错误,如下所示:

Traceback (most recent call last): File "pp.py", line 54, in c.execute(createsqltable) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.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 'Exit VARCHAR(250),caseid VARCHAR(250))' at line 1") here is the code:

lst =['Offset', 'Name', 'PID', 'PPID', 'Thds', 'Hnds', 'Sess', 'Wow64', 'Start', 'Exit', 'caseid']
table_name = "test"
createsqltable = """CREATE TABLE IF NOT EXISTS """ + table_name + " (" + " VARCHAR(250),".join(lst) + " VARCHAR(250))"
#print createsqltable
c.execute(createsqltable)
conn.commit()

最佳答案

由于Exit是sql的关键字,所以必须先使用``进行转义。

lst =['Offset', 'Name', 'PID', 'PPID', 'Thds', 'Hnds', 'Sess', 'Wow64', 'Start', '`Exit`', 'caseid']
table_name = "test"
createsqltable = """CREATE TABLE IF NOT EXISTS """ + table_name + " (" + " VARCHAR(250),".join(lst) + " VARCHAR(250))"
#print createsqltable
c.execute(createsqltable)
conn.commit()

关于python - 从动态列表python创建mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35148964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com