插入到表中的Python SQLite数据正在消失?
所以这让我很困惑,标题并不能很好地解释它。我有一个网页,当用户从下拉列表中选择时,它会对flask处理的url进行AJAX调用。它首先调用/teamSelected,在这里,我的python获取关于这两支球队之间足球比赛的一些信息,将其放入相应命名的表中并返回。 在此之后,对/requestcommentation进行第二个AJAX调用,在python中,我从表中检索该数据并返回它 问题: 当我第一次调用/teamSelected时,我有一个代码,如果表存在,就会删除它。在这之后,我检查表是否存在似乎很奇怪,但每次调用表时都会删除它,这只是我确保输入了程序的if部分,以便我可以测试其中的所有内容。之后,如果表不存在,它将进入if部分,在该部分中创建表,刮取数据并将其存储在表中。如果我尝试打印表中的内容,它会完美地将它们打印出来,尽管它告诉我只有一行? 但是,如果我在开始时删除掉表的代码,然后尝试打印内容,它将不打印任何内容 对我来说,这毫无意义。如果表不存在,我创建它,用数据填充它,然后我可以访问它。但是,如果我注释掉drop table语句,那么下一次调用该表是存在的,并且不会被删除,因此我应该能够访问它,但其中不再有数据。如果我猜的话,几乎就好像这个表是为那个调用创建的,之后它会被销毁,所以后续调用不能访问它 /teamSelected-收集数据,添加到数据库插入到表中的Python SQLite数据正在消失?,python,sqlite,flask,Python,Sqlite,Flask,所以这让我很困惑,标题并不能很好地解释它。我有一个网页,当用户从下拉列表中选择时,它会对flask处理的url进行AJAX调用。它首先调用/teamSelected,在这里,我的python获取关于这两支球队之间足球比赛的一些信息,将其放入相应命名的表中并返回。 在此之后,对/requestcommentation进行第二个AJAX调用,在python中,我从表中检索该数据并返回它 问题: 当我第一次调用/teamSelected时,我有一个代码,如果表存在,就会删除它。在这之后,我检查表是否存
@app.route("/teamSelected", methods=['POST', 'GET'])
def new():
try:
connection = sqlite3.connect("commentary.db")
cursor = connection.cursor()
except:
print("COULD NOT CONNECT TO DATABASE")
data = request.get_data() #gets data passed via AJAX call
splitData = data.decode().replace("\"", "").split("__") #data contains different elements split up by "__"
homeTeam = splitData[0]
awayTeam = splitData[1]
tableName = homeTeam + awayTeam + splitData[2] #unique table name
cursor.execute("DROP TABLE if exists "+tableName) #drops table to ensure enters if statement
cursor.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='"+tableName+"';")
result = cursor.fetchone()
number_of_rows = result[0]
print("R O W S " + str(number_of_rows)) #Always prints 0
if(number_of_rows == 0):
create_table_string = "create table if not exists '"+ tableName + "' (id INTEGER PRIMARY KEY, commentary TEXT, time TEXT)"
cursor.execute(create_table_string)
def scrapeInfo():
...
#scraping stuff
...
maxUpdate = 5
updateNumber = 0
while updateNumber < maxUpdate:
cursor.execute("INSERT INTO "+tableName+"(commentary, time) VALUES(?,?)", (commentaryUpdates[updateNumber], times[updateNumber]))
#inserts scraped data into table
updateNumber += 1
cursor.execute("select * from " + tableName)
rows = cursor.fetchall()
for row in rows:
print(row)
#THIS ^ works
cursor.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" + tableName + "';")
result = cursor.fetchone()
number_of_rows = result[0]
print(number_of_rows)
#This ^ prints 1 despite the above for loop printing 5 rows
return jsonify("CLEAN")
return scrapeInfo()
#This is only hit if the table exists, meaning it doesn't enter
#the if statement, so this section is only hit when the drop table
#statement above is commented out. Here, this prints nothing, no idea why.
cursor.execute("select * from " + tableName)
rows = cursor.fetchall()
for row in rows:
print(row)
概括地说,意外行为:
只有先删除数据,然后添加数据,才能从表中检索数据
若添加数据的上一次调用中已经存在表,则新调用不会检索任何数据
插入数据打印1后的\u行数\u可能与打印5相关
不管怎样,单独的路由/请求注释都无法访问表
没有抛出异常
我真的可以帮上忙,因为我完全不明白这里的问题是什么,而且已经为此争论了好几个小时
经过更多的测试,我确信这与所创建的表的范围有关。我不知道如何或为什么,但我似乎只能访问我在该调用中添加到表中的数据,以前调用中添加的任何数据都不存在,这让我觉得表数据只是访问它的调用的本地数据,而不是全局数据?只是设法弄明白了这一点。我知道这是因为地方性的变化,而不是全球性的变化。环顾四周后,我意识到如果我不使用connection.commit保存所做的更改,这正是我会遇到的问题。我现在已经添加了它,现在所有调用都可以看到所做的更改,并且工作正常。如果代码显示正确,那么问题在于返回scrapeInfo,应该在:ifnumberofrows==0:block内调用该行。。。除此之外-SQL注入呢?啊,是的,抱歉,我现在就编辑它。在我的代码中,scrapeInfo是从if语句中调用的。至于SQL注入,在我担心安全性之前,我只是想让代码实际工作——没有冒犯/只是发现;除此之外,我认为表的数量和表中的行的数量是混乱的。请使用两个单独的变量。。。也许只是为了澄清。另外,将scrapInfo作为单独的函数从外部调用也很好,这样很容易看到传递到那里的内容。@MichałZaborowski没问题,他设法解决了这个问题,但向帮助人员表示感谢!
@app.route("/requestCommentary", methods=['POST', 'GET'])
def getCommentary():
data = request.get_data()
splitData = data.decode().replace("\"", "").split("__")
homeTeam = splitData[0]
awayTeam = splitData[1]
tableName = homeTeam + awayTeam + splitData[2]
#Here I'm trying to retrieve the data from the table, but nothing is printed
cursor.execute("select * from " + tableName)
rows = cursor.fetchall()
for row in rows:
print(row)
return jsonify("CLEAN")