简体   繁体   English

使用sqlite3在python中更新具有多个不同值的行

[英]Updating a row with multiple different values in python with sqlite3

I have looked at many similar sources to this question but can't seem to find an answer that solves my problem. 我已经看过许多类似的问题来源,但似乎无法找到解决我问题的答案。

I am using python3.7.1 and sqlite3 to essentially create quizzes. 我使用python3.7.1和sqlite3来创建测验。 I have an array of questions which I have inserted into a table, with a 'Class_name' and 'quizNumber' using this code example: 我有一系列问题已插入到表中,使用此代码示例使用'Class_name'和'quizNumber':

quizQuestion = ['1 + 1 =', '2 + 2 =', '3 + 3 =',...]
quizNumber = (1)
quizClass = ('6D')

for question in quizQuestions:
    cursor.execute( "INSERT INTO set_questionsTbl (QuizNumber, Class_name, Question) VALUES (?,?,?)", (quizNumber,quizClass,question))

This code works fine and the table(set_questionsTbl) looks like this: 这段代码工作正常,表(set_questionsTbl)如下所示:

QuizNumber | Class_name | Question | Answer 
-------------------------------------------
1          | 6D         | 1 + 1 =  | 
1          | 6D         | 2 + 2 =  |
1          | 6D         | 3 + 3 =  |

etc..

I also have an array of answers: 我也有一系列的答案:

quizAnswers = [2,4,6,...]

The problem that occurs is when trying to update the table with the answers so it looks like this: 发生的问题是尝试使用答案更新表时,它看起来像这样:

QuizNumber | Class_name | Question | Answer 
-------------------------------------------
1          | 6D         | 1 + 1 =  | 2
1          | 6D         | 2 + 2 =  | 4
1          | 6D         | 3 + 3 =  | 6
etc...

The code I tried was this: 我试过的代码是这样的:

for answer in quizAnswers:
    cursor.execute("UPDATE set_questionsTbl SET Answer = (?) ", (answer,))

This didn't work as with every loop the previous inputted answer got overwritten leaving me with this: 这不起作用,因为之前输入的答案被覆盖的每个循环留下了这个:

QuizNumber | Class_name | Question | Answer 
-------------------------------------------
1          | 6D         | 1 + 1 =  | 6
1          | 6D         | 2 + 2 =  | 6
1          | 6D         | 3 + 3 =  | 6
etc...

I have also tried joining the loops together but that doesn't work, table looks like: 我也试过加入循环,但这不起作用,表看起来像:

QuizNumber | Class_name | Question | Answer 
-------------------------------------------
1          | 6D         | 1 + 1 =  | 2
1          | 6D         | 2 + 2 =  | 2
1          | 6D         | 3 + 3 =  | 2
1          | 6D         | 1 + 1 =  | 4
1          | 6D         | 2 + 2 =  | 4
1          | 6D         | 3 + 3 =  | 4
1          | 6D         | 1 + 1 =  | 6
1          | 6D         | 2 + 2 =  | 6
1          | 6D         | 3 + 3 =  | 6

I have tried to correct this many times and searched many different examples but couldn't seem to find a solution. 我试图多次纠正这个并搜索了许多不同的例子,但似乎无法找到解决方案。 So how do I loop through each question and update the answer with each answer in quizAnswers? 那么如何循环遍历每个问题并使用quizAnswers中的每个答案更新答案?

I am new to stack overflow so I may have missed a question similar to this, if so please link it. 我是新的堆栈溢出所以我可能错过了类似的问题,如果是这样,请链接它。

You should have a unique ID for at least one of the fields in your table. 您应该为表中的至少一个字段指定唯一ID。 That way when you execute the answer update, you could add a WHERE clause, eg: 这样,当您执行答案更新时,您可以添加WHERE子句,例如:

    cursor.execute( "UPDATE set_questionsTbl SET answer=? WHERE unique_id=?", (answer,uniqueId))

and that way not all of your records will be updated. 这样就不会更新所有记录。

The point you are missing is, that you instruct your database to update all records with the answer in the current iteration of the loop. 您缺少的一点是,您指示数据库使用当前循环迭代中的答案更新所有记录。
Your example: 你的例子:

quizAnswers = [2,4,6]
for answer in quizAnswers:
    cursor.execute("UPDATE set_questionsTbl SET Answer = (?) ", (answer,))

basically does: 基本上做了:

set Answer for *all* records to 2
set Answer for *all* records to 4
set Answer for *all* records to 6

Your database does not understand the meaning of the data you put into the tables. 您的数据库不理解您放入表中的数据的含义。 For the DB, the questions you inserted into the Question column of your set_questionsTbl are just sequences of characters. 对于DB,您插入set_questionsTblQuestion列中的Question只是字符序列。 It has no means of evaluating the equation in Question in order to set a value in Answer only if that value is the result of the equation. 它无法评估Question中的等式,只有当该值是等式的结果时,才能在Answer设置一个值。

You need to tell your DB which answer to set for which question(s). 您需要告诉您的数据库为哪个问题设置了哪个答案。 You need to give your query criteria, that a record must meet for the update operation to be applied to it. 您需要提供查询条件,记录必须满足才能应用更新操作。 That's what WHERE clauses are for. 这就是WHERE条款的用途。

Following your example, you'll want to create a correlation between questions and their respective answers. 按照您的示例,您将需要创建问题与其各自答案之间的关联。 The below is one way to do that. 以下是一种方法。

First, create a list of tuples. 首先,创建一个元组列表。 Each tuple contains an answer in the first element, the second element holds the corresponding question (this needs to be the exact value you inserted into the Question column). 每个元组在第一个元素中包含一个答案,第二个元素包含相应的问题(这需要是您在“ Question列中插入的确切值)。

quiz_question_answers = [(2, '1 + 1 ='), (4, '2 + 2 ='), (6, '3 + 3 =')] 

Then you can iterate over that list of tuples to execute the UPDATE query in your cursor: 然后,您可以迭代该元组列表以在游标中执行UPDATE查询:

for q_a in quiz_question_answers:
    cursor.execute("UPDATE set_questionsTbl SET Answer = ? WHERE Question = ?", (q_a[0], q_a[1]))

This will update the answer in all records that have the specific equation in Question . 这将更新所有具有Question特定等式的记录中的答案。 Records with different Class_name and/or QuizNumber - a record like this for example: 具有不同Class_name和/或QuizNumber的记录 - 例如这样的记录:

QuizNumber | Class_name | Question | Answer 
-------------------------------------------
4          | 5A         | 1 + 1 =  | 

- will be updated as well, because the only criteria, the Question equaling 1 + 1 = , is met for this record also. - 也将更新,因为此记录也符合唯一标准,即等于1 + 1 =Question

If you want the answers to be set only for questions of quiz number 1 for class 6D, you'll have to add more restrictive criteria to your query, eg: 如果您希望仅针对6D类的测验编号1的问题设置答案,则必须为查询添加更多限制性条件,例如:

quiz_question_answers = [(2, '1 + 1 =', 1, '6D'), (4, '2 + 2 =', 1, '6D'), (6, '3 + 3 =', 1, '6D')]
for q_a in quiz_question_answers:
    cursor.execute("UPDATE set_questionsTbl "
                   "SET Answer = ? "
                   "WHERE Question = ? "
                   "AND QuizNumber = ? "
                   "AND Class_name = ?",
                   (q_a[0], q_a[1], q_a[2], q_a[3]))

With this particular solution (using a list of tuples with the query parameters in the correct order) you can also use executemany to let the cursor do the loop over the list of parameter tuples for you: 使用此特定解决方案(使用具有正确顺序的查询参数的元组列表),您还可以使用executemany让光标为您执行参数元组列表上的循环:

quiz_question_answers = [(2, '1 + 1 ='), (4, '2 + 2 ='), (6, '3 + 3 =')]
cursor.executemany("UPDATE set_questionsTbl SET Answer = ? WHERE Question = ?", quiz_question_answers)

Here 's some further reading on WHERE clauses specifically for SQLite. 以下是有关SQLite的WHERE子句的进一步阅读。

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

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