简体   繁体   English

如何使用新数据添加更新表

[英]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.

相关问题 如何使用 python 向 yaml 文件添加新行/更新数据? - How to add new lines/update data to the yaml file with python? 如何在不换行的情况下向sqlite 3中的表添加数据 - how to add data to a table in sqlite 3 without going to a new line 如何在不重新运行程序的情况下在 tkinter 表上显示新添加的数据? - How to display new add data on tkinter table without rerun the program? 如何将具有数据类型的新列动态添加到现有的 Delta 表中并使用值更新新列 - How to dynamically add new columns with the datatypes to the existing Delta table and update the new columns with values 如何用另一个数据帧更新一个熊猫数据帧(更新旧数据并添加新数据) - How to update one pandas dataframe with another dataframe (update the old data and add new data) 如何在收到新数据后替换 Dash/Plotly 表/或更新当前表 - How to replace a Dash/Plotly Table after it received new Data / or update the current Table 如何向字典中添加新数据 - How to add new data to the dictionary tkinter中phpmyadmin的行数据如何在表的每一行添加Checkbox来更新/删除行数据 - How to add Checkbox to every row of table to update/delete the row data from phpmyadmin in tkinter 如何通过查询在mysql表中添加新列 - How to add a new column to a table in mysql by query 如何向 pandas pivot 表添加新索引? - How to add a new index to a pandas pivot table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM