ios - FMDB ResultSet always returns only one row -


i trying use sqlite database in 1 of projects.

it working fine; reason, happened , couldn't find bug.

the resultset object quit after first record. array has 1 record in it. (probably left while because of error)

i created dbmanager class, , dbmanager class contains different inner classes. have private global fmdatabase instance (and initialise somewhere before using it)

as see, there 2 different print error line

when run, second print line gives error:

error calling sqlite3_step (21: out of memory) rs error domain=fmdatabase code=7 "out of memory" userinfo=0x790308d0 {nslocalizeddescription=out of memory}

and array should contain on 300 records, has 1 record in it. (last print line 1)

this part looking simple. (i have totally similar code somewhere else, works fine)

private var database : fmdatabase!  class dbmanager{      class element{          class func get()->[dataelement]{             database.open()             println( database.lasterror() )              var result = [dataelement]()              var resultset: fmresultset! = database!.executequery("select * element working = 1", withargumentsinarray: nil)              while resultset.next(  ) {                 let data = dataelement(                      id : int(resultset.intforcolumn("id")),                     name: resultset.stringforcolumn("name"),                      server: resultset.stringforcolumn("server"),                      working: resultset.boolforcolumn("working") )                 result.append( data )             }              println( database.lasterror() )             database.close()              println( result.count )             return result         }     } } 

ps: difference between tables (as realize ) "id" column. element table has "id" integer primary key autoincrement not null, other 1 not have id column. both of them worked long time.

the "out of memory" error misleading sqlite error means sqlite function called null value sqlite3* pointer. in fmdb means closed database tried continue using same fmdatabase instance (without calling open again).

now, don't see doing in code sample, code sample employing practices make sort of error possible. namely, rather instantiating fmdatabase object locally, using database property , run risk other function may have used (maybe init method dataelement? maybe other function removed sake of brevity? maybe other thread?).

let's imagine function called other function opened database again (which fmdb silently let do, returning if database open), perform sql, , closed database, routine, when went retrieve second row of information, of have found database closed, resulting in error describe. opening , closing of database object not recursive. once subroutine closed it, it's closed.

now of hypothetical, because without seeing rest of code does, it's impossible me confirm. scenario fit code have shared combined the symptoms you've described.

assuming case, there 2 possible solutions here:

  1. you open database once , leave open until app terminated. easiest approach , more efficient have here. sqlite pretty robust in terms of committing individual sql statements (or transactions), people leave database open.

    i might go step further, , suggest if might have different threads interacting database, instantiate single fmdatabasequeue object, , use throughout app, again not opening , closing time. note, if use fmdatabasequeue, you'll want more judicious making sure 1 function in middle of indatabase or intransaction block doesn't call function tries indatabase or intransaction block (or else you'll deadlock). shared resource, want sensitive resource locked , when it's released, , databases no exception rule.

  2. if you're determined open , close database in every function code sample suggests (again, practice wouldn't advise), not use class property keep track of database. sure, have function opens database, return fmdatabase object, , each function have it's own local instance , close instance when it's done it.

    but want avoid unintended consequences of 1 function's closing database affecting behavior of other function.


Popular posts from this blog