由lastrowid设置的外键问题。 FK值变为“无”#39;

时间:2016-12-03 03:02:30

标签: python sqlite foreign-keys

我正在使用Python3.4和SQlite3。

我有2个表,其中一个table2包含一个外键,它是table1中的主键

在Table1中插入后,我使用:

检索def createLog方法中的最后一行ID

newID = c.lastrowid

newID包含正确的值。接下来,我将newID传递给一个新方法(def enterLog),该方法使用外键将数据插入table2。

我已经设置了一些测试代码,每次插入table1后,newID的结果在下面的结果中可以看作12和13。

当我调用下一个方法时,为了插入这些值(12和13),该值变为“无”#39;。

我怀疑我可能没有得到一个整数值作为回报但是与某个对象相关的东西。

有谁能看到这里的问题在哪里?

谢谢

亚当

此代码调用下面的数据库代码

`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 个答案:

答案 0 :(得分:1)

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

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