This topic created in 4645 days ago, the information mentioned may be changed or developed.
新人,想利用python抓取一些数据,存到mysql里。
但是我通过以下这个模式无法创建table,报的错误是You have an error in your SQL syntax
I=[A,B,C]
for i in I:
cur.execute("CREATE TABLE IF NOT EXISTS %s (num VARCHAR(25))",i)
求懂的人告知怎么能成功创建一个表名为变量名的table~
Supplement 1 · Sep 18, 2013
采用了2楼和7楼的方法,把i前面的逗号改成%就可以了。
其实之前自己采用过这种方法,但是因为自己实际用的list 里面有中文字符,所以也报错了……
13 replies • 1970-01-01 08:00:00 +08:00
 |
|
1
est Sep 17, 2013
试试吧%s改成 ?
|
 |
|
2
delo Sep 17, 2013 1
把 execute参数里 i 前面的逗号改成%(不知道我理解对不对… )
|
 |
|
3
roricon Sep 17, 2013
cur.execute("""CREATE TABLE IF NOT EXISTS {0} (num VARCHAR(25))""".format(i))
|
 |
|
4
cloverstd Sep 17, 2013
%s 改为 ? 或者 ,i 改为 % (i)
|
 |
|
6
delo Sep 18, 2013
@ msg7086 好吧,如果需要'A', 'B'这样的字符串的话,可以把I弄成dict吧。用key来标识…
|
 |
|
7
randal Sep 18, 2013 1
mysqldb表名不能参数绑定, 需要改成cur.execute("CREATE TABLE IF NOT EXISTS %s (num VARCHAR(25))" % i)
|
 |
|
10
lambdaT Sep 18, 2013
for i in l : sqlStr='CREATE TABLE IF NOT EXISTS' +x ;; cur.execute(sqlStr)
|
 |
|
12
likuku Sep 19, 2013
嗯,字符串替代就是这样子的:
print "this is test str: %s" % (test_str)
|
 |
|
13
est Sep 23, 2013
日。7楼说对了。
SQL真是一坨屎。说到底还是拼字符串。万一表名里包含 ` ; ' 等特殊字符就happy了。
|