Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

why doesn't my SQlite database work?

$
0
0

Hi. I am new using SQLite and with my first project using SQLite i get a lot of errors . I searched a lot and couldn't find an answer. I am using Xamrin Forms and I have installed sql-net-pcl already but anywhere I want to use a function in SQLite or use an attribute i get an error for it in the Error list although the squiggly line doesn't underline these function and the color of the function becomes blue as if it was already functional.

here is a list of the errors i am getting:

Error CS0246 The type or namespace name 'SQLite' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'PrimaryKeyAttribute' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'MaxLengthAttribute' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'AutoIncrementAttribute' could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name 'MainPage' could not be found (are you missing a using directive or an assembly reference?)

I have already cleaned and rebuilt the solution and it doesn't work.

this is the Xaml code I am using

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="HelloWorld.MainPage">
    <StackLayout>
        <StackLayout Orientation="Horizontal">
            <Button Text="Add" Clicked="OnAdd" />
            <Button Text="Update" Clicked="OnUpdate" HorizontalOptions="CenterAndExpand" />
            <Button Text="Delete" Clicked="OnDelete" />
        </StackLayout>
        <ListView x:Name="recipesListView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Name}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

and this is the .cs file

using App40;
using SQLite;
using Xamarin.Forms;

namespace HelloWorld
{

    public class Recipe 
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [MaxLength (255)]
        public string Name  { get; set; }
    }

    public partial class MainPage : ContentPage
    {
        private SQLiteAsyncConnection _connection;
            public MainPage()
        {
            InitializeComponent();

            _connection =  DependencyService.Get<ISQLiteDb>().getConnection();

        }

        protected async override void OnAppearing()
        {
            await _connection.CreateTableAsync<Recipe>();
           var recipes = await _connection.Table<Recipe>().ToListAsync();

            base.OnAppearing();
        }

        void OnAdd(object sender, System.EventArgs e)
        {
        }

        void OnUpdate(object sender, System.EventArgs e)
        {
        }

        void OnDelete(object sender, System.EventArgs e)
        {
        }
    }
}

I use this interface for the getting connection to the database

using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
using App40;

namespace App40
{
   public  interface ISQLiteDb
    {
        SQLiteAsyncConnection getConnection();
    }
}

and this is it's implementation in android

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;

namespace App40.Droid
{
    public class SQLiteDb : ISQLiteDb
    {
        public SQLiteAsyncConnection getConnection()
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            var path = Path.Combine(documentsPath, "MySQLite.db3");

            return new SQLiteAsyncConnection(path);
        }
    }
}

What could be the problem ?
Thanks a lot.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>