【问题标题】:cannot read sqlite file无法读取 sqlite 文件
【发布时间】:2016-08-29 09:34:26
【问题描述】:

我在读取sqlite时出现问题,即无法读取表中的数据(但读取的数据量),如下图:

XAML:

<ListBox x:Name="dictionary" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center" >
                <ListBox.ItemTemplate>

                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontSize="20" Text="{Binding Id}" Visibility="Collapsed"/>
                            <TextBlock FontSize="20" Text="{Binding Word}" FontWeight="SemiBold" Margin="30,0,0,0"/>
                            <TextBlock FontSize="20" Text="{Binding Translation}" FontWeight="SemiBold" Margin="30,0,0,0"/>
                        </StackPanel>
                    </DataTemplate>

                </ListBox.ItemTemplate>
            </ListBox>

注意: 上面的 XAML 我用了 text = "Word" 和翻译 text = "Translation" 来测试 sqlite 上的数据是否清晰。虽然绑定数据的使用仍然不可读

代码:

public ObservableCollection<ind_dict> ReadIndo()
        {
            var sqlpath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, @"Assets\kamus.sqlite");
            using (var dbConn = new SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath))
            {
                List<ind_dict> myCollection = dbConn.Table<ind_dict>().ToList<ind_dict>();
                ObservableCollection<ind_dict> indoList = new ObservableCollection<ind_dict>(myCollection);
                dictionary.ItemsSource = indoList;
                return indoList;
            }

        }

ind_dict 类:

public class ind_dict
        {
            [SQLite.Net.Attributes.PrimaryKey]
            public string Id { get; set; }
            public string Word { get; set; }
            public string Translation { get; set; }

            public ind_dict(string word, string translation)
            {
                Word = word;
                Translation = translation;
            }
        }

如何解决?

【问题讨论】:

  • 使用贴出的代码有没有异常? InstalledLocation是只读位置,处理SQLite文件最好放在LocalFolder这样的文件夹中。
  • 不显示异常,只显示sqlite文件中的数据。结果如上所示。我正在尝试使用 进行测试。如果使用 ,显示的数据是空的。
  • 我用你的代码在我身边进行了测试。但是我遇到了“System.MissingMethodException”,附加信息是Constructor on type '&lt;namespace&gt;.ind_dict' not found.。我在ind_dict 类中添加了一个默认构造函数,例如public ind_dict() { } 来解决这个问题。在此之后,您的代码运行良好并显示数据。
  • 我在 ind_dict 类中添加了一个构造函数,但是如果使用绑定,仍然无法显示数据。我包括项目:onedrive.live.com/…请帮我解决。

标签: c# sqlite xaml uwp win-universal-app


【解决方案1】:

这里的问题是你的ind_dict类中的属性和你的kamus.sqlite数据库中的字段名不一样。

在您的数据库中,字段名称为idwordtranslation

但是,在您的 ind_dict 类中,属性是 IdWordTranslation。使用 SQLite.Net-PCL 时,类中的属性必须与相应表中的字段名称匹配。否则,无法填充属性并将设置为其默认值。在您的情况下,您的属性值将设置为null,因为它们的类型是string。因此,您的页面上不会显示任何数据。

要解决您的问题,您可以更改数据库中的字段名称或 ind_dict 类中的属性。这里以更改ind_dict 类中的属性为例:

在 ind_dict.cs 中,更改如下属性:

public class ind_dict
{
    [SQLite.Net.Attributes.PrimaryKey]
    public string id { get; set; }

    public string word { get; set; }
    public string translation { get; set; }

    public ind_dict()
    {
    }

    public ind_dict(string w, string t)
    {
        word = w;
        translation = t;
    }
}

由于我们更改了 ind_dict 类中的属性,我们还需要更改 XAML 中的 DataTemplate

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <StackPanel Orientation="Horizontal">
                <TextBlock FontSize="20"
                           Text="ID"
                           Visibility="Collapsed" />
                <TextBlock Margin="0,0,0,0"
                           FontSize="20"
                           FontWeight="SemiBold"
                           Text="{Binding word}" />
                <TextBlock Margin="30,0,0,0"
                           FontSize="20"
                           Text="{Binding translation}" />
            </StackPanel>
            <Line Height="5" Stroke="#FFE6E6E6" />
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate>

在此之后,数据将显示在您的 ListBox 中。

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 2020-03-08
    • 2021-09-28
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2012-03-07
    • 2016-06-17
    相关资源
    最近更新 更多