priitj / whitedb

WhiteDB memory database
http://whitedb.org/
GNU General Public License v3.0
608 stars 78 forks source link

Passing list to arglist returns 0 results in Python 3.4 #26

Closed Ironlenny closed 8 years ago

Ironlenny commented 8 years ago

Here's the funtion:

class DB():
...
    def search_notes(self, query):
        query = query.split(' ')
        print(query) # debug
        args = [(0, wgdb.COND_EQUAL, 'tag')]

        for i in query:
            args.append((1, wgdb.COND_EQUAL, i))

        self.cursor.execute(arglist=[(0, wgdb.COND_EQUAL, 'tag'), (1, wgdb.COND_EQUAL, 'note')]) # debug
        print((tuple(self.cursor.fetchone()))) # debug
        print(args) # debug
        self.cursor.execute(arglist=args)
        self.cursor.fetchall()
        print((tuple(self.cursor.fetchall()))) # debug

Here's my test:

import db

mainDB = db.DB()
mainDB.create_note('This is a note', ['note', 'programming', 'python', 'whitedb'])
notes = mainDB.search_notes('note')

This is the output:

['note'] # Value of 'query'.
('tag', 'note', <whitedb.Record object at 0x7f36e12b9208>) # Manual search result
[(0, 1, 'tag'), (1, 1, 'note')] # Value of 'args'
() # 'args' search result
priitj commented 8 years ago
self.cursor.fetchall()
print((tuple(self.cursor.fetchall()))) # debug

The first fetchall() consumes all records, the cursor will be positioned at the end of result set, so there are no more records to fetch. Can't repeat fetchall().

for i in query:
    args.append((1, wgdb.COND_EQUAL, i))

This is not wrong, but it could easily generate a query like "where x = 1 and x = 2" which will always have an empty set as a result. Just a heads up.

Ironlenny commented 8 years ago

Thank you. I wasn't aware fetchall() placed the cursor at the end of the set, but that does make sense in hindsight. And thanks for the heads up about my query builder. I'm going to have to fix that.