Mono can't open sqlite database

2019-02-26 01:06发布

问题:

I'm attempting to do a very basic connection to a sqlite v3 database and I'm using monodevelop 3.0 and Mono 2.10 and am unable to get connected to the database. I can make the app create the database, but then it immediately fails attempting to connect to it. Any suggestions? I had started with a different database, but then decided to have my app attempt to create a database empty and then connect to it. This still seems to fail.

SqliteConnection.CreateFile("db\\DataWorksProg.s3db");
SqliteConnection conn = new SqliteConnection("Data Source=file:db\\DataWorksProg.s3db");
conn.Open();

This small piece of code fails with an error about not being able to open the database file.

Mono.Data.Sqlite.SqliteException: Unable to open the database file

Permissions look OK and I have the Sqlite3.dll in the project, and it seems to be working OK. Have I missed anything obvious? I'm pretty good on the Visual Studio side, but still fairly fresh working in a Mono/Monodevelop environment.

回答1:

What platform?

I don't believe you need to create a file. If it's not found, iirc, it'll make the database file.

Fwiw, on a Mac, I'm doing (note URI to a pretty standard path; I haven't used Data Source)...

using System;
using System.Data;
using Mono.Data.Sqlite;

namespace test
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            IDbConnection conTemp = null;
            IDbCommand cmdTemp = null;

            conTemp = (IDbConnection)new SqliteConnection ("URI=file:/Users/userName/mnmh.db");
            conTemp.Open ();
            cmdTemp = conTemp.CreateCommand ();         
            cmdTemp.CommandText = "SELECT * FROM employee";
            IDataReader drTemp = cmdTemp.ExecuteReader ();
            while (drTemp.Read()) {
                Console.WriteLine (drTemp.GetString (0));
            }


        }
    }
}

etc etc

Check the obvious -- you've referenced all the stuff you're using, etc.



回答2:

Figured out my problem here. Apparently instead of using

"Data Source=file:db\\DataWorksProg.s3db"

I should have been using

"URI=file:db\\DataWorksProg.s3db"

Switched to the URI and it works as expected. I had thought from reading the docs that under the 2.0 profile, the DataSource part was needed instead of the URI, but I got the results I'm looking for.



标签: c# sqlite mono