SQLite.SQLiteException:本地SQLite数据库文件中没有这样的表:Piece

我正在尝试读取存储在Xamarin android项目资产文件夹中的数据库文件(Lego_Parts.db3)。尝试读取已填充的数据库文件时,出现错误:

  

SQLite.SQLiteException:'没有这样的表:Piece'

(文件中的表称为“片断”)

以下是适用的代码:

Piece.cs

namespace TestApp1
{
    public class Piece
    {
        [PrimaryKey]
        public int ID { get; set; }
        public int PartNum { get; set; }
        public string Category { get; set; }
        public string Url { get; set; }
    }
}

PieceDB.cs

public class PieceDB
{
        readonly SQLiteAsyncConnection _database;

        public PieceDB(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
        }

        public async Task Createtable()
        {
            await _database.CreatetableAsync<Piece>();
            return;
        }

        public Task<List<Piece>> GetallPieces()
        {
            return _database.Table<Piece>().ToListAsync();
        }

        public Task<Piece> GetPiece(int partNum)
        {
            return _database.Table<Piece>().Where(i => i.PartNum == partNum).FirstOrDefaultAsync();
        }

        public Task<Piece> GetPiece(int partNum)
        {
            return _database.Table<Piece>().Where(i => i.PartNum == partNum).FirstOrDefaultAsync();
        }
    }
}

App.xaml.cs

namespace TestApp1
{
    public partial class App : Application
    {
        static PieceDB database;

        public static PieceDB PieceDatabase
        {
            get
            {
                if(database == null)
                {
                    database = new PieceDB(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"Lego_Parts.db3"));
                }
                return database;
            }
        }

        public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new Page1());
        }

        protected override void OnStart()
        {
            // Handle when your app starts
        }

        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

DatabaseTest.xaml.cs

namespace TestApp1
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DatabaseTest : ContentPage
    {

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            await App.PieceDatabase.Createtable()

            listView.ItemsSource = await App.PieceDatabase.GetallPieces();

        }

        public DatabaseTest()
        {
            InitializeComponent();
        }

        async void Handle_ItemTapped(object sender,ItemTappedEventArgs e)
        {
            if (e.Item == null)
                return;

            await DisplayAlert("Item Tapped","An item was tapped.","OK");

            //Deselect Item
            ((ListView)sender).SelectedItem = null;
        }
    }
}

DatabaseTest.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TestApp1.DatabaseTest">
    <StackLayout Margin="20,35,20,20">
        <ListView x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding PartNum}"
                              Detail="{Binding Url}"
                              />
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>
    </StackLayout>
</ContentPage>

在许多SQLite教程中,这一行

_database.CreatetableAsync<Piece>();

将是

_database.CreatetableAsync<Piece>().Wait();

但是这样做时,我却得到了错误:

System.aggregateexception: 'One or more errors occurred. (Cannot create a table without columns (does 'TestApp1.Piece' have public properties?))'

我对使用Xamarin或SQLite还是很陌生,因此非常感谢您的帮助。

waltc 回答:SQLite.SQLiteException:本地SQLite数据库文件中没有这样的表:Piece

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3116495.html

大家都在问