简体   繁体   English

同时在表中插入大量值

[英]Insert a lot of values into a table at the same time

So basically, I have a project for school, and it requires me to write a program that will help two compatible persons meet. 所以基本上,我有一个学校项目,它要求我编写一个程序,以帮助两个兼容的人见面。 They have to answer different questions, then the program will compare the results. 他们必须回答不同的问题,然后程序将比较结果。 So I have 21 questions I want to ask and I am trying to insert the 21 answers into a table in my database, but it is not working. 所以我想问21个问题,我试图将21个答案插入我的数据库中的一个表中,但它无法正常工作。 With 9 questions, for example, it worked perfectly, here is the code: 例如,有9个问题,它工作得很好,这里是代码:

import sqlite3


connexion = sqlite3.connect("Test Numero 106")
cur= connexion.cursor()


cur.execute('''CREATE TABLE IF NOT EXISTS aaa(q1 TEXT, q2 TEXT, q3 TEXT, q4 TEXT, q5 TEXT, q6 TEXT, q7 TEXT, q8 TEXT, q9 TEXT, q10 TEXT, q11 TEXT, q12 TEXT, q13 TEXT, q14 TEXT, q15 TEXT, q16 TEXT, q17 TEXT, q18 TEXT, q19 TEXT, q20 TEXT, q21 TEXT);''')

ans=['A', ' A', ' A', ' A', ' A', ' A', ' A', ' A', ' A', ' A ', 'A', ' A', ' A', ' A', ' A', ' A', ' A', ' A', ' A', ' A','A']

if ans[1]=='A' and ans[2]=='A'and ans[3]=='A':
    cur.execute('''INSERT INTO aaa (q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', ans)


connexion.commit()

The list "ans" is the answers inputted by a certain person and I'm trying to insert them in a certain table but they are not going in. The software that I'm using "pyzo" is not telling me that there is an error in the code, but when I want to see the information in a certain table, it is empty. 列表“ans”是由某个人输入的答案,我试图将它们插入某个表中但是它们没有进入。我正在使用的软件“pyzo”并没有告诉我有一个代码中的错误,但是当我想查看某个表中的信息时,它是空的。

The python sqlite docs show that the second argument of the "execute" command needs to be a tuple. python sqlite文档显示“execute”命令的第二个参数需要是一个元组。 Can you try that and see if it works? 你能尝试一下,看看它是否有效?

cur.execute('''INSERT INTO aaa (q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', tuple(ans))

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

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