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

标签 python mysql

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

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/

相关文章:

mysql - 指令删除的 hibernate 问题

python - 在python 3中将 map 对象转换为numpy数组

python - 为什么添加面部颜色时 sharey = True 被忽略?

Mysql在多个数据库中运行一个脚本

javascript - 在 Sequelize Attribute 中编写自定义函数

java - Hibernate 查询以选择 Datetime 列的时间范围内的行

python - 将 datetime64[ns, UTC] Pandas 列转换为日期时间

python - 动态类变量 - locals() 与 __metaclass__

python - 在 Flask 中通过 SQL 数据库格式化表

mysql - 检测MySQL中的值是否为数字