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

Having an issue creating table using dynamic list. I keep getting error next to Exit as shown:

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()

解决方案

Since Exit is the keyword of sql, you must use `` to escape first.

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值