【问题标题】:Cannot create a table without columns error Xamarin SQLite无法创建没有列错误 Xamarin SQLite 的表
【发布时间】:2019-11-10 15:37:06
【问题描述】:

我正在尝试在我的 Xamarin 应用程序中使用本地 SQLite 数据库,我一直在关注本教程:https://docs.microsoft.com/en-us/xamarin/get-started/tutorials/local-database/?tabs=vswin

加载我正在测试它的 ListView 页面时,我总是收到错误:System.AggregateException: '一个或多个错误发生。 (无法创建没有列的表('TestApp1.Piece' 有公共属性吗?))'

这是适用的代码:

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

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

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

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

<?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,因此非常感谢任何帮助或解释。

【问题讨论】:

  • 您的应用正在设置MainPage = new NavigationPage(new Page1()); 此代码路径似乎完全绕过任何数据库创建或初始化
  • 即使在 App.xaml.cs 构造函数中初始化数据库时,在初始化时仍然会发生错误。
  • 在调用CreateTableAsync时去掉Wait
  • 现在我收到错误:SQLite.SQLiteException: 'near ")": syntax error' 在尝试添加到数据库时出现此错误:SQLite.SQLiteException : 'no such table: Piece' 当试图从中读取时。

标签: c# sqlite xamarin xamarin.forms


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2014-03-21
    • 2018-12-30
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多