简体   繁体   中英

SQLite is creating a disk file for In-memory DB

I tried to create a shareable in-memory database as per the documentation provided on SQLite Site. But I end up finding the solution to the problem.

 var connectionString = "Data Source=sharedmemdb;Mode=Memory;Cache=Shared";

            using (var connection1 = new SQLiteConnection(connectionString))
            {
                connection1.Open();

                var command1 = connection1.CreateCommand();
                command1.CommandText =
                    "CREATE TABLE Message ( Text TEXT );" +
                    "INSERT INTO Message ( Text ) VALUES ( 'Is there anybody out there?' );";
                command1.ExecuteNonQuery();

                using (var connection2 = new SQLiteConnection(connectionString))
                {
                    connection2.Open();

                    var command2 = connection2.CreateCommand();
                    command2.CommandText = "SELECT Text FROM Message;";


                    var message = command2.ExecuteScalar() as string;
                }
            }

If I execute this code, it will create in-memory DB named as sharedmemdb and shared cache is enabled while making the connection, so this connection accessible to other connections also. If I run this first time this works pretty fine but if I close the application and run again it throws error "Table Message already exists" and this looks very strange as I created the table in-memory and this should not be available if application restarts.

After getting this error, I looked into the application directory and found the file "sharedmemdb" which means SQLite is not creating the shareable in-memory DB.

Any clue why this is happening?

After moving command to using block:

var connectionString = "Data Source =sharedmemdb; Mode = Memory; Cache = Shared";

            using (var connection1 = new SQLiteConnection(connectionString))
            {
                connection1.Open();

                using (var command1 = connection1.CreateCommand())
                {
                    command1.CommandText =
                        "CREATE TABLE Message ( Text TEXT );" +
                        "INSERT INTO Message ( Text ) VALUES ( 'Is there anybody out there?' );";
                    command1.ExecuteNonQuery();
                }
                using (var connection2 = new SQLiteConnection(connectionString))
                {
                    connection2.Open();

                    using (var command2 = connection2.CreateCommand())
                    {
                        command2.CommandText = "SELECT Text FROM Message;";


                        var message = command2.ExecuteScalar() as string;
                    }
                }
            }

System.Data.SQLite doesn't support the "Mode" parameter (that's Microsoft.Data.Sqlite).

However, in System.Data.SQLite you can use the "FullUri" parameter to pass arguments directly to SQLite, so you can achieve what you wanted by changing your connection string to

FullUri=file:mem.db?mode=memory&cache=shared
Protocol^    ^      ^           ^
DB Name -----|      |           |
Use memory mode ----+           |
Use shared cache ---------------+

(The first line being the actual connection string, the next couple of lines breaking it down)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM