繁体   English   中英

Python SQLite3 UPDATE WHERE无法更新

[英]Python SQLite3 UPDATE WHERE not updating

问题:调用我的方法,值将被执行并且应该被提交,但是不执行。 下面的方法,但是它不会产生任何错误...什么也不会发生,但是会打印出值:

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

我隔离的数据库很少,我可以隔离的很好。 在这里隔离 )。 我知道此代码应运行。 另外,我是一个初学者,这是我的第一个项目。

我应该寻找什么具体的东西? 有什么我可能没有做的事情吗?

编辑:在此处生成数据的完整代码,具有用于测试运行的静态示例html: https : //github.com/SimonWoodburyForget/Experiments/tree/master/WarThunder

下面是示例代码,其中以时间为文本。 它既快速又脏,因此您可以使用它进行测试。 我将致力于将其转换为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')

如果要将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])

暂无
暂无

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

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