[英]How do I compare values in a database with variables in SQLite3 with python
Hello there's two things I need help with: Firstly, how would I go about comparing variables with data within a database using SQSLite3?您好,有两件事我需要帮助:首先,我 go 如何使用 SQSLite3 将变量与数据库中的数据进行比较? (Line 9) Secondly, how would I combine lines 9 and 11 so when it inserts the data it inserts it on the same line? (第 9 行)其次,我将如何组合第 9 行和第 11 行,以便在插入数据时将其插入同一行?
import sqlite3
con = sqlite3.connect('Coursework.db')
NumberofS = 59
NumberofMS = 62
X = 'Stationary Waves on a String'
c = con.cursor()
c.execute("INSERT INTO Request (Equipment, Practical_Name) SELECT Equipment, Practical_Name FROM Practicals WHERE Practical_Name = (X) ")
c.execute("INSERT INTO Request (Number_of_Students, Method_Sheets) VALUES(?, ?)", (NumberofS, NumberofMS))
con.commit()
con.close()
Any help would be greatly appreciated任何帮助将不胜感激
Simply combine both queries in an insert-select with three parameters where numbers parameters run as constants:只需将插入选择中的两个查询与三个参数组合在一起,其中数字参数作为常量运行:
sql = (
"INSERT INTO Request (Equipment, Practical_Name, Number_of_Students, Method_Sheets) "
"SELECT Equipment, Practical_Name, ? AS Number_of_Students, ? AS Method_Sheets "
"FROM Practicals "
"WHERE Practical_Name = ?"
)
c.execute(sql, [NumberofS, NumberofMS, X])
con.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.