插入列表作为新列MySQL
我有一个字符串值new_values
的列表,我想将其插入Companies
表中的新列中的新列,我的mysql数据库。由于我有数百行,我无法使用?我遇到的语法。
import MySQLdb
cursor = db.cursor()
cursor.execute("INSERT INTO companies ....")
lst_to_add = ["name1", "name2", "name3"]
db.commit()
db.close()
但是,我不确定应该在列表中传递什么查询,以及在查询中包括新的列名称(例如:“ newcol”)的正确语法。
编辑: 当前表:
id originalName
1 Hannah
2 Joi
3 Kale
预期输出:
id originalName fakeName
1 Hannah name1
2 Joi name2
3 Kale name3
I have a list of string values new_values
and I want to insert it as a new column in the companies
tables my mysql database. Since I have hundreds of rows, I cannot manually type them using the ? syntax that I came across on SO.
import MySQLdb
cursor = db.cursor()
cursor.execute("INSERT INTO companies ....")
lst_to_add = ["name1", "name2", "name3"]
db.commit()
db.close()
However, i am not sure what query I should use to pass in my list and what's the correct syntax to include the new column name (eg: "newCol") into the query.
Edit:
current table:
id originalName
1 Hannah
2 Joi
3 Kale
expected output:
id originalName fakeName
1 Hannah name1
2 Joi name2
3 Kale name3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Alter Table
添加新列。然后循环浏览数据,更新每一行。您需要Python数据,以指示从原始名称到假名的映射。例如,您可以使用字典。
Use
ALTER TABLE
to add the new column.Then loop through the data, updating each row. You need Python data that indicates the mapping from original name to fake name. You can use a dictionary, for example.