sqlite附加密码保护的数据库

时间:2018-07-18 13:37:24

标签: database sqlite

如何将受密码保护的sqlite数据库附加到不受密码保护的数据库?

我有一个没有密码保护的用户sqlite数据库。 我正在尝试附加一个受密码保护的只读资源数据库。

我可以撤销他们的角色,并先打开受保护的密码并附加用户数据库,但我认为这种方式应该起作用?

我没有尝试太多事情,所以没有代码可共享。但我已经在Google上进行了搜索,似乎在文档或任何示例中都找不到对此的任何提及。 都是直接打开受密码保护的数据库。

编辑:-这是我尝试过的... 同时使用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);
        }
    }

1 个答案:

答案 0 :(得分:1)

在另一篇文章中找到了答案: https://stackoverflow.com/a/1385690/10099912

要附加受密码保护的sqlite数据库,您需要使用“关键字”:

“ ATTACH DATABASE'C:\ test.sqlite'AS AttachedDb KEY'password'”