无法创建没有列错误的表Xamarin SQLite [英] Cannot create a table without columns error Xamarin SQLite

查看:195
本文介绍了无法创建没有列错误的表Xamarin SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Xamarin应用程序中使用本地SQLite数据库,我一直在关注本教程:

I am trying to use a local SQLite database in my Xamarin application, I've been following this tutorial: https://docs.microsoft.com/en-us/xamarin/get-started/tutorials/local-database/?tabs=vswin

在加载要对其进行测试的ListView页面时,总是会收到错误:System.AggregateException:'发生了一个或多个错误. (无法创建没有列的表('TestApp1.Piece'具有公共属性吗?))'

When loading the ListView page that I'm testing it on I always get the error: System.AggregateException: 'One or more errors occurred. (Cannot create a table without columns (does 'TestApp1.Piece' have public properties?))'

以下是适用的代码:

Piece.cs

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

PieceDatabase.cs

PieceDatabase.cs

namespace TestApp1
{
    public class PieceDatabase
    {
        readonly SQLiteAsyncConnection _database;

        public PieceDatabase(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<Piece>().Wait();
        }

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

        public Task<int> SavePieceAsync(Piece temp)
        {
            return _database.InsertAsync(temp);
        }
    }
}

App.xaml.cs

App.xaml.cs

namespace TestApp1
{
    public partial class App : Application
    {

        static PieceDatabase database;

        public static PieceDatabase PieceDatabase
        {
            get
            {
                if (database == null)
                {
                    database = new PieceDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pieces.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

DatabaseTest.xaml.cs

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

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

            /** The error does not occur if I add a piece here, but I'm trying to figure out why that is
            await App.PieceDatabase.SavePieceAsync(new Piece
            {
                Category = "Beams",
                PartNum = 1,
                Url = "whatever.com"
            }); */

            List<Piece> test = await App.PieceDatabase.GetPieceAsync();
            listView.ItemsSource = test;
        }

        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

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>

这是我第一次与Xamarin合作,感谢您的帮助或解释.

This is the first time I've worked with Xamarin so any help or explanation is appreciated.

推荐答案

我做了一个有关从sqlite数据库加载数据的示例,您可以从github下载它,我没有问题,我使用sqlite-net-pcl 1.6.258- Beta版本和xamarin.forms 3.4版本.

I do one sample about loading data from sqlite database, you download it from github, I have no issue, I use sqlite-net-pcl 1.6.258-beta version and xamarin.forms 3.4 version.

https://github.com/CherryBu/Sqlite-Listview

您还可以浏览以下有关详细信息的文章:

You cam also take a look the following article about detailed info:

https://www.c- sharpcorner.com/article/xamarin-forms-sqlite-database-crud-operations2/

这篇关于无法创建没有列错误的表Xamarin SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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