所以我是UWP的新手,在完成连接到sqlite tutorial之后,我修改了一些代码来尝试新的东西。我没有在本地文件夹中创建和保存sqlite文件,而是将文件目录更改为已安装的位置,我创建了一个文件夹“data”并在其中添加了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
提前致谢。
答案 0 :(得分:1)
应用的安装目录是只读位置(请参阅File access permissions)。它适用于应用程序代码和资产。
如果您需要对数据库的写访问权限,则无法将其放在应用程序的安装目录中。