【问题标题】:How to write to a readonly sqlite file in UWP如何在 UWP 中写入只读 sqlite 文件
【发布时间】:2017-05-16 18:26:33
【问题描述】:

所以我是 UWP 的新手,在完成与 sqlite tutorial 的连接后,我稍微修改了代码以尝试新事物。我没有在本地文件夹中创建和保存 sqlite 文件,而是将文件目录更改为安装位置,我创建了一个文件夹“data”并在其中添加 sqlite 文件。至此,您可以弄清楚我遇到了什么问题。我只能读取/选择表,但不能写入/插入、删除、更新数据库中的记录。我有点期待这个问题,我知道这个问题的原因。我想知道的是:

  • 我可以将安装位置的只读 sqlite 文件更改为读写吗?如果可以,请给我指路。
  • 将数据库文件保存在安装位置是否实用或好主意?或者最好将其保存在本地文件夹中。

这是我的代码:

对于 mainpage.xaml

<GridView x:Name="gridView" BorderBrush="Blue" BorderThickness="2" HorizontalAlignment="Left" Margin="34,401,0,0" VerticalAlignment="Top" Height="287" Width="1219">
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Test" x:Name="templateGrid">
                <StackPanel x:Name="stackPanel"  Orientation="Vertical" HorizontalAlignment="Center">
                    <StackPanel x:Name="stack2" Margin="20,20,0,0">
                        <TextBlock FontSize="18" Text="{x:Bind ID}" HorizontalAlignment="Center"></TextBlock>
                        <TextBlock FontSize="10" Text="{x:Bind Name}" HorizontalAlignment="Center"></TextBlock>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
    <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="10,112,0,0" TextWrapping="Wrap" Text="Name" FontSize="25" VerticalAlignment="Top"/>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="132,112,0,0" FontSize="25" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="328"/>
    <Button x:Name="button2" Content="Add" Click="button2_Click" HorizontalAlignment="Left" Margin="516,125,0,0" VerticalAlignment="Top"/>

后面的代码:

SQLite.Net.SQLiteConnection conn;
    public MainPage()
    {
        this.InitializeComponent();
        string path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "data", "librarydatatbase.db");
        conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path);
        List<Test> Records = conn.Query<Test>(@"select * from Test");
        gridView.ItemsSource = Records;
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        conn.Execute("insert into Test (Name) values (?)", textBox.Text.ToString());
        List<Test> Records = conn.Query<Test>(@"select * from Test");
        gridView.ItemsSource = Records;
    }

对于 sqlite:

create table Test
(
ID integer primary key autoincrement,
Name nvarchar(25)
);

insert into Test (Name) values ("Test value 1")

我收到的错误:

An exception of type 'SQLite.Net.SQLiteException' occurred in SQLite.Net.dll but was not handled in user code

Additional information: ReadOnly

提前致谢。

【问题讨论】:

    标签: sqlite uwp


    【解决方案1】:

    应用程序的安装目录是只读位置(请参阅File access permissions)。它用于应用程序代码和资产。

    如果您需要对数据库的写入权限,则不能将其放在应用的安装目录中。

    【讨论】:

    • 感谢您的回答。但是我已经知道了,我只是想知道数据库文件是否有解决此问题的技巧或方法。
    • @Dant:安全不是通过锁门来实现的,而是在门垫下留下钥匙。如果您需要读/写数据库,请将其存储在您具有读/写访问权限的位置。我不知道这算不算骗局。
    猜你喜欢
    • 2014-03-14
    • 2014-12-06
    • 2021-05-01
    • 2021-09-01
    • 2020-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多