2016-12-03 52 views
0

我使用Python3.4和SQlite3。lastrowid設置的外鍵問題。 FK值更改爲'無'

我有2個表,其中一個表2包含一個外鍵是在表1

主鍵我檢索使用在DEF createLog的方法中的最後一排ID在表1的插入件後:

NEWID = c.lastrowid

newID包含正確的值。接下來,我將newID傳遞給一個新的方法(def enterLog),該方法使用外鍵將數據插入到table2中。

我已經設置了一些測試代碼,並且每次插入table1之後newID的結果在下面的結果中可以看作12和13。

當我調用下一個方法時,要插入這些值(12和13),該值將變爲'無'。

我懷疑我可能沒有得到一個整數值作爲回報,但與某個對象有關。

任何人都可以看到這裏的問題在哪裏?

謝謝

亞當

此代碼調用下面

`class ProcessingLog(): 

def newLog(self, fk_user_preferences, recipe_name='not defined', user_name='not defined', cycles_to_be_completed=0, message='not defined'): 
    message = 'Cycles to be completed: ' + str(cycles_to_be_completed) + '/n' + message 
    query.createLog(fk_user_preferences,recipe_name, user_name, message) 

def newEntry(self, id, user_name, cycles_completed, status, message): 
    query.enterLog(id, user_name, cycles_completed, status, message) 

def viewLog(self, id): 
    db_records = query.viewLog(id) 
    for item in db_records: 
     print(item)` 

數據庫代碼這是代碼與數據庫

class DataBase(): 

db_file_path = '...hidden...' 

def __init__(self, db_file=db_file_path): 
    self.db_connection = sqlite3.connect(db_file) # Connection will create db but not tables 

def createLog(self, recipe_id=0, recipe_name='test log', user_name='no user name', message='no message'): 
    time_stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
    newID = 0 
    c = self.db_connection.cursor() 
    c.execute('INSERT INTO processing_log (fk_user_preferences, recipe_name, message, timestamp) VALUES(?,?,?,?);', (recipe_id, recipe_name, message, time_stamp)) 
    newID = c.lastrowid 
    self.db_connection.commit() 
    self.enterLog(newID, user_name, 0, 'READY', 'Log initiated.', time_stamp) 
    print("ewID....", NewID) 
    return newID 

def enterLog(self, processing_log_id=0, user_name='test log', cycle_number=0, status='no status', message='no message', time_stamp=''): 
    if time_stamp == '': # allow time_stamp to be passed in from above 
     time_stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
    c = self.db_connection.cursor() 
    c.execute('INSERT INTO log_entry (fk_processing_log, user, cycle_number, status, message, timestamp) VALUES(?,?,?,?,?,?);', (processing_log_id, user_name, cycle_number, status, message, time_stamp)) 
    self.db_connection.commit() 

def viewLog(self, record_id): 
    c = self.db_connection.cursor() 
    c.execute('SELECT * FROM processing_log WHERE primary_key = ?;', (record_id,)) 
    records = c.fetchall() 
    return records 

def viewLogEntries(self, record_id): 
    c = self.db_connection.cursor() 
    c.execute('SELECT * FROM log_entry WHERE fk_processing_log = ?;', (record_id,)) 
    records = c.fetchall() 
    return records 

def viewAllLogEntries(self): 
    c = self.db_connection.cursor() 
    c.execute('SELECT * FROM log_entry ;') 
    records = c.fetchall() 
    return records 
互動

這是一些測試代碼

Log = ProcessingLog() 
last_id = Log.newLog(1, 'test recipe name', 'test user_name', 10, 'test log entry message') 
test_last_id = last_id 
print(" newEntry =========================") 
Log.newEntry(last_id, 'WAIT', 'f-name, last-name', 0, 'Waiting for start... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'RUN', 'f-name, last-name' 1, 'Processing running... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'PAUSE', 'f-name, last-name', 2, 'Processing Paused... ') 
print("last_id.....",test_last_id) 
sleep(2) 
Log.newEntry(last_id, 'RESUME', 'f-name, last-name', 2, 'Processing Resumed... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'RUN', 'f-name, last-name', 3, 'Processing running... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'RUN', 'f-name, last-name'', 4, 'Processing running... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'RUN', 'f-name, last-name', 5, 'Processing running... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'RUN', ''f-name, last-name'', 6, 'Processing running... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'RUN', ''f-name, last-name'', 7, 'Processing running... ') 
print("last_id.....",test_last_id) 
Log.newEntry(last_id, 'COMPLETE', ''f-name, last-name'', 7, 'Processing complete... ') 
print("last_id.....",test_last_id) 
last_id = Log.newLog(2, 'test2 recipe name', 'test user_name2', 20, 'test2 log entry message') 
print("last_id.....",last_id) 
Log.newEntry(last_id, 'WAIT', 'f-name, last-name', 0, 'Waiting for start... ') 
print("last_id.....",last_id) 
Log.newEntry(last_id, 'RUN', 'f-name, last-name', 1, 'Processing running... ') 
print("last_id.....",last_id) 
Log.newEntry(last_id, 'RUN', 'f-name, last-name', 2, 'Processing Paused... ') 
print("last_id.....",last_id) 
Log.newEntry(last_id, 'PAUSE', 'f-name, last-name', 2, 'Processing Resumed... ') 
print("last_id.....",last_id) 
sleep(2) 

這是從測試碼

newID.... 12 
newEntry ========================= 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
newID.... 13 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 
last_id..... None 

回答

1

的newLog函數不返回值的輸出。將函數的最後一行更改爲下面的內容。

return query.createLog(fk_user_preferences, recipe_name, user_name, message) 
+0

感謝您的幫助! –