Correct way to handle 0-rows results from N1QL?

Hello everyone, sorry if this is a silly question, or if this is the wrong place to ask, but I’m quite new to Couchbase AND Python, and I couldn’t find the answer anywhere else.

I have a bucket with various data, and I’m performing a couple of queries: the first one returns data, the second one finds no matching documents.
If I go to the console and I perform the queries, I get something on the line of:

query 1:

SELECT keyName FROM bucket

result:

[
  {
    "keyName": "value1"
  },
  {
    "keyName": "value2"
  }
]

query 2:

SELECT keyName FROM bucket WHERE unsatisfiable_condition

result:

{
  "results": [],
  "metrics": {
    "elapsedTime": "182.0104ms",
    "executionTime": "182.0104ms",
    "resultCount": 0,
    "resultSize": 0
  }
}

Now, the question is: how do I handle the second case, with the Python SDK? I’m performing the query in this way (simplifying my code):

buckets = {}

def __getBucket(self, bucketName):
    if self.buckets.has_key(bucketName):
        return self.buckets[bucketName]
    else:
        bucket = Bucket(HOST+bucketName)
        self.buckets[bucketName] = bucket
        return bucket

def getRows(self, bucketName):
    bucket = self.__getBucket(bucketName)
    result = bucket.n1ql_query(N1QLQuery(query))   
    return result 

while True:
    try:
        rows = getRows(bucketName)
        for row in rows:
            do_stuff(row)
    except KeyboardInterrupt as e:
        print str(e)
        break
    except Exception as e:
        print str(e)
        break

If I press Ctrl+C to exit, I get the following output, generated from the second “except”:

ValueFormatError: <Failed to decode bytes, Results=1, inner_cause=, C Source=(src\convert.c,222), OBJ='{\n    "requestID": "1c7b22a7-9034-4e47-8a47-27c9329ad862",\n    "signature": {\n        "keyName": "json"\n    },\n    "results": [\n    ],\n    "status": "success",\n    "metrics": {\n        "elapsedTime": "10.9967ms",\n        "executionTime": "10.9967ms",\n        "resultCount": 0,\n      "resultSize": 0\n    }\n}\n'>

Why am I getting this error, and why am I getting it only when I try to exit (and not during execution time)? Am I handling things correctly?

Your script basically loops and creates a new connection each time - that’s not good practice.

As for your error, I’m not sure why that’s happening. It would seem as if it doesn’t even want to decode the payload as UTF8. What version of Python are you using?

As far as receiving the error when exiting - the error terminates your program - because of how you’ve strucutred your loop.
Does the error happen in your simplified version? :slight_smile:

Hello, sorry if I didn’t reply earlier - yes, the code I posted was oversimplified, check my updated version.

I’m using python 2.7.13

What puzzles me is that I keep getting no output during execution (despite the while true loop that keeps processing 0-rows results), and then it gives me this error in output when I close the program.