Is the file I'm about to open an SQLite Database

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Is the file I'm about to open an SQLite Database

Phoenix
Is there anyway to confirm the file I am about to open, via
sqlite3_open, is in fact an SQLite Database?

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is the file I'm about to open an SQLite Database

Simon Slavin-3


On 4 Sep 2017, at 3:46pm, Phoenix <[hidden email]> wrote:

> Is there anyway to confirm the file I am about to open, via
> sqlite3_open, is in fact an SQLite Database?

Read the file header from it using fopen()/fread()/fclose() (i.e. not the SQLite library).  The file will start with "SQLite format 3" followed by a x00 byte.

Simon.
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is the file I'm about to open an SQLite Database

Gwendal Roué-2
In reply to this post by Phoenix

> Le 4 sept. 2017 à 16:46, Phoenix <[hidden email]> a écrit :
>
> Is there anyway to confirm the file I am about to open, via
> sqlite3_open, is in fact an SQLite Database?

You may have noticed that sqlite3_open doesn't fail if the file is not a database.

To check if the file is a valid database, read something. For example: "SELECT * FROM sqlite_master LIMIT 1" (I'm sure there are shorter/smarter test access, but this one does the job). SQLite will then fail unless the file is actually a database.

Gwendal Roué

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Is the file I'm about to open an SQLite Database

papa
Yes, what I do to check for the existance of a database is:
//This method will returns true if the table does not exit, false otherwise.
bool SQLite3_RDB::notExist(const std::wstring& table) {
     // SQL statement
     sql_statement_request = "SELECT *  FROM " + table + " LIMIT 0";

     // Feed the statement to the SQL3
     rc = sqlite3_prepare_v2(this->db, sql_statement_request.c_str(),
-1, &binary_sql_statement, NULL);

     // If the Statement processor returns !=1 table does not exist
     if (rc != SQLITE_OK) {
         // Terminate the connection to the database and
         this->finalize();
         // Respond affirmative
         return true;
     }
     // If the table exit terminate the connection to the database
     this->finalize();
     // Respond negative
     return false;
     // Finished
}

I just started using SQLite3, so I am sure there are better ways to do
this, but this seems to work for me.

On 2017-09-04 10:55 AM, Gwendal Roué wrote:

>> Le 4 sept. 2017 à 16:46, Phoenix <[hidden email]> a écrit :
>>
>> Is there anyway to confirm the file I am about to open, via
>> sqlite3_open, is in fact an SQLite Database?
> You may have noticed that sqlite3_open doesn't fail if the file is not a database.
>
> To check if the file is a valid database, read something. For example: "SELECT * FROM sqlite_master LIMIT 1" (I'm sure there are shorter/smarter test access, but this one does the job). SQLite will then fail unless the file is actually a database.
>
> Gwendal Roué
>
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

--
ArbolOne.ca
Using Fire Fox and Thunderbird.
ArbolOne is composed of students and volunteers dedicated to providing free services to charitable organizations.
ArbolOne on Java Development in progress [ í ]

_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users