On 12 January 2016 at 13:26, Scott Hess <shess at google.com> wrote: > On Mon, Jan 11, 2016 at 9:12 PM, Felipe Gasper <felipe at felipegasper.com> > wrote: > > > On 11 Jan 2016 9:06 PM, Rowan Worth wrote: > > > >> * if it returns SQLITE_OK and zero rows, the schema hasn't been created > >> yet > >> > > > > Sure; however, by the time you do the next action it?s possible that > > something else will be creating the schema. This is the condition that > I?m > > trying to avoid. > > > If something else created the schema between check and use, what's the > problem? > > You can avoid this by doing: > - query sqlite_master. > - if the query returns rows, the schema exists, exit; else > - open a transaction. > - query sqlite_master. > - if the query returns rows, the schema exists, end transaction and exit; > else > - create the schema. > - commit transaction. > > Or, just use CREATE IF NOT EXISTS style when creating the schema, and you > will successfully not create it the second time. Or just pay attention to > your error codes in the create code. > > -scott >
You can get away with querying sqlite_master only once, I think? - open a transaciton - query sqlite_master - if the query returns rows, end transaction and exit - create the schema - on SQLITE_BUSY, retry transaction[1] - commit transaction [1] most likely this means another process is also creating the schema, and the next transaction attempt will exit early after noticing sqlite_master is populated This way doesn't require any EXCLUSIVE transactions either. -Rowan