我试图在数据库中插入3个单词但我只插入最后一个单词,我想在不同的原始单词中插入3个单词。
import MySQLdb
bd = MySQLdb.connect("localhost","root","welcome","mydb")
cursor = bd.cursor()
palabras = ["hola", "python", "pythondiario"]
for i in palabras:
sql = "insert into empresa (codigo_unico) values ('%s')" % (i)
#cursor.execute(sql)
bd.commit()
bd.close()
它只在原始中插入pythondiario,我做错了什么?
答案 0 :(得分:0)
仅仅因为你没有在循环内执行查询,只会执行列表最后一个元素的查询,将cursor.execute(sql)
放在循环中:
cursor = bd.cursor()
palabras = ["hola", "python", "pythondiario"]
for i in palabras:
sql = "insert into empresa (codigo_unico) values ('%s')" % (i)
cursor.execute(sql)