Create database

Create database

Hi,
If I have a database open with sqlite3_open() and then want to issue
another sqlite3_open(),
the old database will be closed and the new one will open?
Or I have to explicitly call sqlite3_close()?

Re: Create database

No, a new database will be opened and you will be given an sqlite3* to it.

Just like the fopen() call can be used to open multiple files, sqlite3_open* opens a database. The way to close an sqlite3* (handle to a) database is to use sqlite3_close specifying the database you want to close.

--
˙uʍop-ǝpısdn sı ɹoʇıuoɯ ɹnoʎ 'sıɥʇ pɐǝɹ uɐɔ noʎ ɟı

> -----Original Message-----
> From: sqlite-users [mailto:[hidden email]]
> On Behalf Of Igor Korot
> Sent: Friday, 14 April, 2017 08:26
> To: Discussion of SQLite Database; General Discussion of SQLite Database
> Subject: [sqlite] Create database
>
> Hi,
> If I have a database open with sqlite3_open() and then want to issue
> another sqlite3_open(),
> the old database will be closed and the new one will open?
> Or I have to explicitly call sqlite3_close()?
>
> Thank you.
> _______________________________________________
> sqlite-users mailing list
> [hidden email]> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Re: Create database

Keith,

On Fri, Apr 14, 2017 at 10:32 AM, Keith Medcalf <[hidden email]> wrote:
>
> No, a new database will be opened and you will be given an sqlite3* to it.
>
> Just like the fopen() call can be used to open multiple files, sqlite3_open* opens a database. The way to close an sqlite3* (handle to a) database is to use sqlite3_close specifying the database you want to close.

Re: Create database

To further clarify, the result of doing multiple sqlite3_open calls and saving the results to the save db handle is the same as doing

fh = fopen(...)
fh = fopen(...)
fh = fopen(...)

You have opened multiple files but are only keeping track of 1. So although three files are opened fclose(fh) will only close the last one, and the first two are still open, you just discarded your reference to them and they are inaccessible to you.

If you do this sort of thing a lot then you program will eventually crash when it fills up with unreferenced (leaked) memory objects that you forgot (and have overwritten the handle) to close.

Re: Create database

>
> To further clarify, the result of doing multiple sqlite3_open calls and saving the results to the save db handle is the same as doing
>
> fh = fopen(...)
> fh = fopen(...)
> fh = fopen(...)
>
> You have opened multiple files but are only keeping track of 1. So although three files are opened fclose(fh) will only close the last one, and the first two are still open, you just discarded your reference to them and they are inaccessible to you.
>
> If you do this sort of thing a lot then you program will eventually crash when it fills up with unreferenced (leaked) memory objects that you forgot (and have overwritten the handle) to close.

Thank you for clarifying.
It means that the call to sqlite3_open() does not close previously
opened database and I have to explicitly close it
with sqlite3_close().

Re: Create database

> On Apr 14, 2017, at 7:44 AM, Igor Korot <[hidden email]> wrote:
>
> It means that the call to sqlite3_open() does not close previously
> opened database and I have to explicitly close it
> with sqlite3_close().
>
> I was just couldn't find it anywhere in the docs. Probably missed it.

The handle is documented as an ‘out’ parameter to sqlite3_open, so the function simply ignores any value it has on input (it’s probably uninitialized garbage anyway.) All it does is store the new handle there.