Python 使用值数组存储到数据库中
我的目标是:将分数数组存储到表中 为什么是数组?之所以使用这个数组,是因为我使用pygame收集了分数,并保存了数组中每次尝试获得的分数,直到达到10次尝试并将数组传递到此处 我希望从游戏中存储的数组中获得这些分数,并将其存储到数据库中。如您所见,列score1到score10和=?因为我不知道在问号里填什么 我尝试使用INSERT into将其插入到数据库中,但我无法,因为我需要指定需要它的ID。我只剩下使用更新了 有没有一种方法可以使用这个分数数组将其存储到数据库中 注意:我不能把球员的得分写在“?”中,因为得分会有所不同。选择按钮就在那里,看看它是否有效 非常感谢Python 使用值数组存储到数据库中,python,arrays,python-3.x,sqlite,Python,Arrays,Python 3.x,Sqlite,我的目标是:将分数数组存储到表中 为什么是数组?之所以使用这个数组,是因为我使用pygame收集了分数,并保存了数组中每次尝试获得的分数,直到达到10次尝试并将数组传递到此处 我希望从游戏中存储的数组中获得这些分数,并将其存储到数据库中。如您所见,列score1到score10和=?因为我不知道在问号里填什么 我尝试使用INSERT into将其插入到数据库中,但我无法,因为我需要指定需要它的ID。我只剩下使用更新了 有没有一种方法可以使用这个分数数组将其存储到数据库中 注意:我不能把球员的得分
import sqlite3
conn = sqlite3.connect(':memory:')
conn.cursor()
score = [100, 20, 1000, 1002, 129, 1039, 400, 30, 300, 30]
conn.execute("""UPDATE Defender
SET score1=?, score2=?,
score3=?, score4=?,
score5=?, score6=?,
score7=?, score8=?,
score9=?, score10=?
WHERE defender_id=1""")
conn.commit()
conn.execute("SELECT score1 FROM Defender WHERE defender_id=1")
print(conn.fetchall())
conn.close()
通常,
execute()
函数可以接受两个参数:查询本身和作为元组的数据(如果需要):
conn.execute(query, data)
所以你可以说:
query = """UPDATE Defender
SET score1=?, score2=?,
score3=?, score4=?,
score5=?, score6=?,
score7=?, score8=?,
score9=?, score10=?
WHERE defender_id=1""" #your query
score = [100, 20, 1000, 1002, 129, 1039, 400, 30, 300, 30] #your data
conn.execute(query, tuple(score))