[英]How to add update a table with new data
I have written query to add data to a DB but would like to add more data without dropping the DB every time.我已经编写了将数据添加到数据库的查询,但希望添加更多数据而不每次都删除数据库。 How do I go about writing such query?我该如何编写这样的查询?
what I have:我拥有的:
category = (
(1, 'Sports'),
(2, 'Entertainment'),
(3, 'Politics'),
(4, 'Technology'),
(5, 'Health'),
(6, 'Business'),
(7, 'Travel Competition'),)
query2 = "INSERT OR REPLACE INTO users_category (Id, Name) VALUES (%s, %s)
cursor.executemany(query2, category)
what I've tried:我试过的:
category = (
(1, 'Sports'),
(2, 'Entertainment'),
(3, 'Politics'),
(4, 'Technology'),
(5, 'Health'),
(6, 'Business'),
(7, 'Travel'),
(8, 'Other'),) #new data
query2 = "INSERT INTO users_category (Id, Name) VALUES (%s, %s) ON CONFLICT (id) DO UPDATE SET Name = excluded.users_category.Name"
Error Traceback:错误追溯:
Error invalid reference to FROM-clause entry for table
"users_category"
LINE 1: ... 'Sports') ON CONFLICT (id) DO UPDATE SET Name =
excluded.u...
^
HINT: There is an entry for table "users_category", but it cannot be
referenced from this part of the query.
Desired Result:预期结果:
A script to add more data to an existing table without the need for flushing the previous data manually.向现有表添加更多数据的脚本,无需手动刷新以前的数据。
这有效:
query2 = "INSERT INTO users_category (Id, Name) VALUES (%s, %s) ON CONFLICT DO NOTHING"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.