如何在 UWP 中写入只读 sqlite 文件 [英] How to write to a readonly sqlite file in UWP

查看:36
本文介绍了如何在 UWP 中写入只读 sqlite 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So i'm new to UWP, after finished the connect to sqlite tutorial, I tinkering with the code a bit to try out new things. Instead of create and save sqlite file in local folder, i change the file directory to installed location, i created a folder "data" and add sqlite file in it. At this point you can figure it out what problem i ran in to. I can only read/select table but can't write/insert, delete, update records in database. I kind of expecting this problems and i known reasons for this. What i want to know is:

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

这是我的代码:

对于 mainpage.xaml

For the 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

提前致谢.

推荐答案

应用的安装目录是只读位置(请参阅 文件访问权限).它用于应用程序代码和资产.

The app's install directory is a read-only location (see File access permissions). It is for application code and assets.

如果您需要对数据库进行写访问,则不能将其放置在应用的安装目录中.

If you need write access to your database, you cannot place it in the app's install directory.

这篇关于如何在 UWP 中写入只读 sqlite 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆