简体   繁体   English

将python列表插入MySQL数据库

[英]INSERT python list into MySQL DB

I got some code but not really getting it working 我有一些代码,但并没有真正起作用

listdata = [1, 2]
listdata1.insert(1, raw_input("Enter first thing into DB: ")
listdata2.insert(2, raw_input("Enter second thing into DB: ")

Then I make the DB, then 然后我制作数据库,然后

cursor.execute("INSERT INTO testdb (TABLE, TABLE) VALUES (%s, %s)", (listdata1, listdata2

Which gives me: NameError: name 'listdata1' is not defined 这给了我:NameError:未定义名称“ listdata1”

Is inserting this even possible this way? 这样是否可以插入? Or how could I get it to work? 还是我如何使它工作?

the problem happens here: 问题发生在这里:

listdata1.insert(1, raw_input("Enter first thing into DB: ")

You did not define the variable listdata1 yet. 您尚未定义变量listdata1 Change it to listdata and you will fix the NameError. 将其更改为listdata,将修复NameError。

Besides, you can append instead of insert eg 此外,您可以追加而不是插入例如

listdata.append( raw_input("Enter first thing into DB: ") )

Then the raw input will then be kept in position 0 in listdata, and you can retrieve it by listdata[0] 然后,原始输入将保留在listdata中的位置0 ,您可以通过listdata[0]检索。

The sql looks wrong too ('TABLE' appear twice) sql也看起来错误(“ TABLE”出现两次)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM