Python Spanner 客户端 API INSERT DML 不插入数据或 return 错误
Python Spanner Client API INSERT DML Doesn't Insert data or return error
我有一个 class 允许我重用 spanner 客户端连接。我正在尝试使用 DML 进行基本插入,但无法在我的 class 中完成此操作。我可以使用命令行插入数据:
cloud spanner databases execute-sql queue --instance=sandbox --sql="INSERT MESSAGE_STORE (MessageId,Message,MessageRecipient,MessageSender) VALUES ( 'id','hello spanner','fred','bob')"
但是,当我尝试使用 python 客户端库执行等效操作时,它不会插入一行,甚至不会引发错误。我已将调试设置为 true,所以这应该不是问题。我来自 C/C++ 背景并且是 python 的新手所以错误可能就在那里,我不确定。
这是我的 class 代码:
class DataStore():
def __init__(self):
self.logger = logging.getLogger('manager.sub')
loghandler = logging.StreamHandler(sys.stdout)
loghandler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
self.logger.addHandler(loghandler)
self.logger.setLevel(logging.DEBUG)
client = spanner.Client()
self.instance = client.instance(INSTANCE_ID)
database = self.instance.database(DATABASE_ID)
self.client = database
def insertmessage(self, newmsg):
messageid = uuid.uuid4()
sender = newmsg['Sender']
recipient = newmsg['Recipient']
message = newmsg['Message']
# Values are hardcoded for now until I can get it to work
def insert_message(transaction):
row_ct = transaction.execute_update(
"INSERT MESSAGE_STORE (MessageId, Message, MessageRecipient, MessageSender) "
" VALUES ('id','hello spanner' , 'fred' , 'bob') "
)
print("{} record(s) inserted.".format(row_ct))
try:
self.client.run_in_transaction(insert_message)
except Exception as e:
self.logger.debug(e)
# Hardcoded for now until I can actually get the data inserted
output = "{ 'Message Id':" + str(messageid) +", 'Result Code': '1' }"
return output
您的问题似乎是由于 Python 对适当间距和缩进的严格要求所致。将 insert_message 函数更新为如下结构:
def insert_message(transaction):
row_ct = transaction.execute_update(
"INSERT MESSAGE_STORE (MessageId, Message, MessageRecipient, MessageSender) "
" VALUES ('id', 'hello spanner', 'fred', 'bob')"
)
print("{} record(s) inserted.".format(row_ct))
try:
self.client.run_in_transaction(insert_message)
except Exception as e:
self.logger.debug(e)
我有一个 class 允许我重用 spanner 客户端连接。我正在尝试使用 DML 进行基本插入,但无法在我的 class 中完成此操作。我可以使用命令行插入数据:
cloud spanner databases execute-sql queue --instance=sandbox --sql="INSERT MESSAGE_STORE (MessageId,Message,MessageRecipient,MessageSender) VALUES ( 'id','hello spanner','fred','bob')"
但是,当我尝试使用 python 客户端库执行等效操作时,它不会插入一行,甚至不会引发错误。我已将调试设置为 true,所以这应该不是问题。我来自 C/C++ 背景并且是 python 的新手所以错误可能就在那里,我不确定。
这是我的 class 代码:
class DataStore():
def __init__(self):
self.logger = logging.getLogger('manager.sub')
loghandler = logging.StreamHandler(sys.stdout)
loghandler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
self.logger.addHandler(loghandler)
self.logger.setLevel(logging.DEBUG)
client = spanner.Client()
self.instance = client.instance(INSTANCE_ID)
database = self.instance.database(DATABASE_ID)
self.client = database
def insertmessage(self, newmsg):
messageid = uuid.uuid4()
sender = newmsg['Sender']
recipient = newmsg['Recipient']
message = newmsg['Message']
# Values are hardcoded for now until I can get it to work
def insert_message(transaction):
row_ct = transaction.execute_update(
"INSERT MESSAGE_STORE (MessageId, Message, MessageRecipient, MessageSender) "
" VALUES ('id','hello spanner' , 'fred' , 'bob') "
)
print("{} record(s) inserted.".format(row_ct))
try:
self.client.run_in_transaction(insert_message)
except Exception as e:
self.logger.debug(e)
# Hardcoded for now until I can actually get the data inserted
output = "{ 'Message Id':" + str(messageid) +", 'Result Code': '1' }"
return output
您的问题似乎是由于 Python 对适当间距和缩进的严格要求所致。将 insert_message 函数更新为如下结构:
def insert_message(transaction):
row_ct = transaction.execute_update(
"INSERT MESSAGE_STORE (MessageId, Message, MessageRecipient, MessageSender) "
" VALUES ('id', 'hello spanner', 'fred', 'bob')"
)
print("{} record(s) inserted.".format(row_ct))
try:
self.client.run_in_transaction(insert_message)
except Exception as e:
self.logger.debug(e)