lastrowid设置的外键问题。 FK值更改为'无'

问题描述:

我使用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 

的newLog函数不返回值的输出。将函数的最后一行更改为下面的内容。

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

感谢您的帮助! –