Python Forum
twisted: catch return from sql
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
twisted: catch return from sql
#1
hi all,

I got this function inside twisted...

@inlineCallbacks
    def get_or_create_node_id(self,node_name):
        try:
            record_exists = yield self.db.runQuery("SELECT EXISTS(SELECT 1 FROM nodes WHERE name = %s)", (node_name))
            print(record_exists[0])
        except ConnectionError:
            log.error("db interaction failed due to connection error")
        if record_exists[0] == '1':
            log.info("creating new nodes entry {node_name}", node_name=node_name)
            try:
                yield self.db.runQuery("INSERT INTO nodes (name) VALUES ('%s')", (node_name))
                node_id = yield self.db.runQuery("select LAST_INSERT_ID()")
            except ConnectionError:
                log.error("db interaction failed due to connection error")
        returnValue(int(node_id))
record_exists[0] returns this...

2021-09-01T17:46:39+0200 [stdout#info] (1,)
how can i use this in an if statement to check if the record already exists...i dont get it, not even sure if this a tuple...

  if record_exists[0] == '1':
Update: solved this by switching to pymysql's dict cursor...much cleaner output
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex to catch what user has put in text box robertkwild 26 8,220 Jun-26-2024, 01:06 PM
Last Post: AdamHensley
  try catch not working? korenron 2 2,158 Jan-15-2023, 01:54 PM
Last Post: korenron
  Multiprocessing queue catch get timeout Pythocodras 1 6,110 Apr-22-2022, 06:01 PM
Last Post: Pythocodras
  how to catch schema error? maiya 0 3,209 Jul-16-2021, 08:37 AM
Last Post: maiya
  is this a good way to catch exceptions? korenron 14 8,643 Jul-05-2021, 06:20 PM
Last Post: hussaind
  pool mysql error - not catch by try\except? korenron 1 3,559 Jul-05-2021, 11:26 AM
Last Post: ibreeden
  try catch question ,get data from main code korenron 7 5,725 Nov-03-2020, 09:28 AM
Last Post: korenron
  catch input type error mcmxl22 5 5,163 Aug-11-2019, 07:33 AM
Last Post: wavic
  How to catch string between two strings SriMekala 3 4,177 May-24-2019, 03:05 AM
Last Post: heiner55
  mac os and twisted? MuntyScruntfundle 0 2,713 Sep-22-2018, 12:20 PM
Last Post: MuntyScruntfundle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020