Connection open to nonexistent database succeeds! WTF?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Connection open to nonexistent database succeeds! WTF?

Mike Clark
I've written the following code for the ADO.NET client for SQLite, and the
odd thing I've noticed is that even when the connection string passed to it
does not refer to a file that exists, the code returns true!  Does SQLite
not actually check for a valid connection until you try to perform a data
operation?  The result I get when trying to insert a row to a table in a
nonexistent database is "Sql logic error" "table doesn't exist" -- no
kidding, because there's no such database file!

Is this expected behavior?

    public bool ConnectionOK(string ConnString)
    {
        bool result = true;
        SQLiteConnection sqlConnection;
        if (DbPath.Length > 0)
        {
            try
            {
                sqlConnection = new SQLiteConnection(ConnString);
                sqlConnection.Open();
                if (sqlConnection.State == ConnectionState.Open)
                {
                    sqlConnection.Close();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
        }
        return result;
    }


--
Mike Clark <http://www.mikeclark.co>
Twitter: @Cyberherbalist
Blog: Cyberherbalist's Blog <http://www.cyberherbalist.co>
---------
"Free will, though it makes evil possible, is also the only thing that
makes possible any love or goodness or joy worth having."
*- C. S. Lewis*
_______________________________________________
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: Connection open to nonexistent database succeeds! WTF?

Jens Alfke-2

> On Mar 31, 2018, at 8:17 AM, Mike Clark <[hidden email]> wrote:
>
> Is this expected behavior?

Yes. If the database file doesn’t exist, opening it will create it. (That’s how you create new databases.) There is a flag to sqlite3_open (in the C API) that prevents creating a file.

—Jens
_______________________________________________
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: Connection open to nonexistent database succeeds! WTF?

Barry Smith
It looks like you're using C# and System.Data.SQLite.

If that's the case, use the connection string parameter FailIfMissing=True
to throw an exception if the file does not exist.

On 1 April 2018 at 02:34, Jens Alfke <[hidden email]> wrote:

>
> > On Mar 31, 2018, at 8:17 AM, Mike Clark <[hidden email]>
> wrote:
> >
> > Is this expected behavior?
>
> Yes. If the database file doesn’t exist, opening it will create it.
> (That’s how you create new databases.) There is a flag to sqlite3_open (in
> the C API) that prevents creating a file.
>
> —Jens
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
>
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users