6

When should you close the connection to an SQLite database (using [db close] in FMDB)?

Right now I am closing it after running every batch of related queries, but should I rather close when my app closes? What are the pros/cons of doing either way?

Thanks

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Souleiman
  • 3,360
  • 4
  • 23
  • 21
  • http://stackoverflow.com/questions/6061036/cocoa-sqlite-when-to-close-database refer to this question – Sumodh S Mar 30 '13 at 15:40
  • Okay, thanks, didn't see that. But those 2 answers have no votes/comments and don't really answer my question (what are the pros/cons) – Souleiman Mar 30 '13 at 15:45

1 Answers1

36

I'm the guy who wrote FMDB.

Keep it open unless you change your schema. That's the only reason to close it, and constantly re-opening it is a little hit on performance / battery life.

ccgus
  • 2,906
  • 23
  • 18
  • 1
    @ccgus Thanks.. FMDB is really good. but may time app is crash due to this. Error : "database is currently in use". On each operation first i open db. than some place i close it also.. so is it good idea to close each time ? its good to close when app is gone in backgroud mode ? Please give me suggestion... Its really appricited.. – Hitarth Aug 01 '14 at 19:02
  • @ccgus Should we close the database when the app goes into the background? – Ben Clayton Feb 17 '15 at 17:25
  • I don't do much iOS programming- so take my answer with a grain of salt. But no, I don't think you need to close it. If the fd is still around in SQLite, then it should all be good. I guess try it, and let us know what you find out :) – ccgus Feb 18 '15 at 17:03