簡體   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