I am trying to use a local SQLite database in my Xamarin application
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 )'
Here is the applicable code:
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());
}
}
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
{
Catagory = "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;
}
}
}