简体   繁体   English

Python SQLite3 UPDATE WHERE无法更新

[英]Python SQLite3 UPDATE WHERE not updating

Problem: Call my method, values get executed and should get committed, but don't. 问题:调用我的方法,值将被执行并且应该被提交,但是不执行。 Method below, but it does not generate any error... nothing happens but values do get printed: 下面的方法,但是它不会产生任何错误...什么也不会发生,但是会打印出值:

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

I quarantined has little of the database has I could, and it works perfectly fine. 我隔离的数据库很少,我可以隔离的很好。 ( quarantine here ). 在这里隔离 )。 I know this code should run. 我知道此代码应运行。 Also, I'm a beginner and this is my very first project. 另外,我是一个初学者,这是我的第一个项目。

Anything specific that I should be looking for? 我应该寻找什么具体的东西? Is there something I might not be doing? 有什么我可能没有做的事情吗?

EDIT: Full code that generates the data here, has static sample html for test runs: https://github.com/SimonWoodburyForget/Experiments/tree/master/WarThunder 编辑:在此处生成数据的完整代码,具有用于测试运行的静态示例html: https : //github.com/SimonWoodburyForget/Experiments/tree/master/WarThunder

Below is the sample code with time as text for now. 下面是示例代码,其中以时间为文本。 It is quick and dirty so you can use it to test. 它既快速又脏,因此您可以使用它进行测试。 I will work on converting it to time.time(). 我将致力于将其转换为time.time()。

If the datatype of startType function parameter does not match the database startTime datatype that will also be a problem of course and no match will be found to update. 如果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')

Here is the output generated: 这是生成的输出:

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')

If you want to store startTime as a date or timestamp then you want to declare the datatype of the column on creation accordingly. 如果要将startTime存储为日期或时间戳,则需要在创建时相应地声明列的数据类型。

c.execute("create table Flights (..., startTime date or startTime timestamp, ...)")

Note that internally sqlite will not store the data as date or timestamp. 请注意,内部sqlite不会将数据存储为日期或时间戳。 The data will be numeric. 数据将是数字。

Part of your challenge is to make sure the datatype in the update statement matches the datatype that is used when inserting the data into the table. 您面临的挑战之一是确保update语句中的数据类型与将数据插入表中时使用的数据类型匹配。

If you decide to create the startTime column in this manner then the default adapters may be enough for what you need. 如果决定以这种方式创建startTime列,则默认适配器可能足以满足您的需求。 Here is the link to the documentation and immediately after the sample code that seems most relevant. 这里是文档的链接,紧接在看起来最相关的示例代码之后。

https://docs.python.org/2/library/sqlite3.html#sqlite3.PARSE_COLNAMES 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