0

How can I attach a password protected sqlite database to a non password protected database?

I have a user sqlite database that is not password protected. I'm trying to attach a read-only resource database that is password protected.

I could reverse their roles and open the password protected first and attach the user database, but I think it should work this way?

I haven't tried too many things so there is no code to share. but I have googled and can't seem to find any mention of this in the documentation or any examples. It's all about opening a password protected database directly.

Edit: - heres what i've tried... using both https://www.sqlite.org/lang_attach.html https://www.sqlite.org/c3ref/open.html

private void btnPasswordAttach_Click(object sender, EventArgs e)
    {
        string pathToUnProtected = @"C:\Users\BoB\Downloads\DBs\Test Users #1.astc";
        string connectionstring = string.Format("Data Source={0};Version=3;BinaryGUID=false", pathToUnProtected);
        SQLiteConnection connection = new SQLiteConnection(connectionstring);
        connection.Open();
        TestQueryMain(connection); **//WORKS**

        pathToUnProtected = @"C:\Users\BoB\Downloads\DBs\Test Mods #1.aste";
        string commandTextUnProtected = string.Format("ATTACH DATABASE '{0}' AS mods", pathToUnProtected);
        SQLiteCommand attachCommandUnProtected = new SQLiteCommand(commandTextUnProtected, connection);
        attachCommandUnProtected.ExecuteNonQuery();
        TestQueryUnProtected(connection); **//WORKS**

        //string pathToProtected = @"C:\Users\BoB\Downloads\DBs\VanillaResources.sqlite";
        //string pathToProtected = @"C:\Users\BoB\Downloads\DBs\VanillaResources.sqlite?password=VanillaIceCream";
        //string pathToProtected = @"file:C:\Users\BoB\Downloads\DBs\VanillaResources.sqlite?password=VanillaIceCream";
        string pathToProtected = @"C:\Users\BoB\Downloads\DBs\VanillaResources.sqlite;password=VanillaIceCream;";
        string password = "VanillaIceCream";

        string commandText = string.Format("ATTACH DATABASE '{0}' AS vanilla", pathToProtected);
        SQLiteCommand attachCommandProtected = new SQLiteCommand(commandText, connection);
        attachCommandProtected.ExecuteNonQuery();
        TestQueryProtected(connection); **//NO SUCH TABLE...**
    }


private void TestQueryMain(SQLiteConnection connection)
    {
        string sql = "Select * FROM Notes";
        SQLiteCommand command = new SQLiteCommand(sql, connection);
        command.ExecuteReader();
    }

    private void TestQueryProtected(SQLiteConnection connection)
    {
        try
        {
            string sql = "SELECT Version from vanilla.DatabaseVersion";
            SQLiteCommand command = new SQLiteCommand(sql, connection);

            using (SQLiteDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    string i = reader["Version"].ToString();
                    Debug.Assert(true);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Assert(true);
        }
    }

    private void TestQueryUnProtected(SQLiteConnection connection)
    {
        try
        {
            string sql = "SELECT Version from mods.DatabaseVersion";
            SQLiteCommand command = new SQLiteCommand(sql, connection);

            using (SQLiteDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    string i = reader["Version"].ToString();
                    Debug.Assert(true);
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Assert(true);
        }
    }
Bob
  • 11
  • 3
  • What have you tried? The naive attempt would be to open the password protected DB and then attach it to the other. You should say what research you _have_ done. –  Jul 18 '18 at 15:28
  • added some code that i tried... i'd rather not do the password protected first, feels like this should work... but i may not have any choice. thx – Bob Jul 18 '18 at 20:11

1 Answers1

1

found the answer in another post: https://stackoverflow.com/a/1385690/10099912

to attach a password protected sqlite database you need to use the 'key'word:

"ATTACH DATABASE 'C:\test.sqlite' AS attachedDb KEY 'password'"

Bob
  • 11
  • 3