我正在尝试选择返回一个对象作为下面的代码。但是这个对象返回空,但是如果我打印记录是否正确打印。我需要返回一个对象,如下所示,但此返回为 null。
我正在使用Python 3.7和PostgreSql数据库11
def selectRowNick(connection, nick):
with connection:
try:
cursor = connection.cursor()
query = """SELECT * FROM app_row where nick = %s"""
cursor.execute(query, (nick, ))
records = cursor.fetchone()
row = Row()
row.setId(records[0])
row.setDescription(records[1])
row.setAddressIP(records[2])
row.setControlBoard(records[3])
row.setNick(records[4])
return row
except (Exception, psycopg2.Error) as error:
if (connection):
print("Failed to select record into table", error)
finally:
# closing database connection.
if (connection):
cursor.close()
connection.close()
from db_condig import connection
from db_store import *
from Row import Row
row = Row()
row = selectRowNick(connection, "row1")
print(row)
null
不是python中的东西。你的意思是我猜None
。
据我了解,row
,selectRowNick()
的返回值,是'null'
或'NULL'
。如果你需要它来None
,你需要做点什么。
或?
return None if row.lower() == 'null' else row