问题:调用我的方法,值会被执行并且应该被提交,但不要。下面的方法,但它不会产生任何错误......没有任何反应,但值会打印出来:
def updateNameWhereTime(self, time, name): # time is a floating point from time.time(),
# name is a simple string like 'this'
print(time, name) # This prints right values.
self.db.execute("UPDATE Flights SET name=(?) WHERE startTime=(?);", (name, time))
self.db.commit() # Does not save anything to the .db
我隔离的数据库很少,我可以,它完全正常。 (quarantine here)。我知道这段代码应该运行。另外,我是初学者,这是我的第一个项目。
我应该寻找什么具体的东西?有什么我可能不会做的吗?
编辑:在此处生成数据的完整代码,包含用于测试运行的静态样本html:https://github.com/SimonWoodburyForget/Experiments/tree/master/WarThunder
答案 0 :(得分:0)
下面是时间为文本的示例代码。它很快,很脏,所以你可以用它来测试。我将努力将其转换为time.time()。
如果startType函数参数的数据类型与数据库startTime数据类型不匹配,那么当然也会出现问题,并且找不到匹配项进行更新。
def updateNameWhereTime(time, name): # time is a floating point from time.time(),
print(time, name)
c.execute("UPDATE Flights SET name=? WHERE startTime=?;", (name, time))
conn.commit()
def insertRows(table, startTime, name):
c.execute("INSERT INTO Flights VALUES ('"+startTime+"', '"+name+"')")
def printTableRows(table):
for row in c.execute("SELECT * FROM "+table):
print row
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute('''DROP TABLE IF EXISTS Flights''')
c.execute('''CREATE TABLE Flights
(startTime text, name text)''')
print "Inserting rows into table"
insertRows('Flights','2015-04-11 10:00','Test 1')
insertRows('Flights','2015-04-11 10:05','Test 2')
insertRows('Flights','2015-04-11 10:10','Test 3')
insertRows('Flights','2015-04-11 10:15','Test 4')
print "Original data in table"
printTableRows('Flights')
print "Updating rows in table"
updateNameWhereTime('2015-04-11 10:05','Test 2 Updated')
updateNameWhereTime('2015-04-11 10:10','Test 3 Updated')
print "Updated rows in table"
printTableRows('Flights')
以下是生成的输出:
Inserting rows into table
Original data in table
(u'2015-04-11 10:00', u'Test 1')
(u'2015-04-11 10:05', u'Test 2')
(u'2015-04-11 10:10', u'Test 3')
(u'2015-04-11 10:15', u'Test 4')
Updating rows in table
('2015-04-11 10:05', 'Test 2 Updated')
('2015-04-11 10:10', 'Test 3 Updated')
Updated rows in table
(u'2015-04-11 10:00', u'Test 1')
(u'2015-04-11 10:05', u'Test 2 Updated')
(u'2015-04-11 10:10', u'Test 3 Updated')
(u'2015-04-11 10:15', u'Test 4')
答案 1 :(得分:0)
如果要将startTime存储为日期或时间戳,则需要相应地声明创建列的数据类型。
c.execute("create table Flights (..., startTime date or startTime timestamp, ...)")
请注意,内部sqlite不会将数据存储为日期或时间戳。数据将是数字。
您面临的部分挑战是确保update语句中的数据类型与将数据插入表中时使用的数据类型相匹配。
如果您决定以这种方式创建startTime列,那么默认适配器可能足以满足您的需要。以下是文档的链接,紧接在看似最相关的示例代码之后。
https://docs.python.org/2/library/sqlite3.html#sqlite3.PARSE_COLNAMES
import sqlite3
import datetime
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = con.cursor()
cur.execute("create table test(d date, ts timestamp)")
today = datetime.date.today()
now = datetime.datetime.now()
cur.execute("insert into test(d, ts) values (?, ?)", (today, now))
cur.execute("select d, ts from test")
row = cur.fetchone()
print today, "=>", row[0], type(row[0])
print now, "=>", row[1], type(row[1])
cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"')
row = cur.fetchone()
print "current_date", row[0], type(row[0])
print "current_timestamp", row[1], type(row[1])