0
votes

I am trying to learn xaml and C# used in Xamarin Forms, and now I want to implement SQLite functionality. I am simply trying to add a value into a SQL table but get the following error:

Unhandled Exception:

SQLite.SQLiteException: near ")": syntax error occurred

My Database connection interface is as follows:

using SQLite;
namespace TestSQLite
{
    public interface IDatabaseConnection
    {
        SQLiteAsyncConnection GetConnection();
    }
}

My Android specific Database Connection (iOS is identical) is as follows:

using SQLite;
using System.IO;
using TestSQLite; 
using Xamarin.Forms;

[assembly: Dependency(typeof(DatabaseConnection))]

namespace TestSQLite
{
    public class DatabaseConnection : IDatabaseConnection
    {
        public SQLiteAsyncConnection GetConnection() 
        {
            var dbName = "TestDb.db3";
            var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), dbName);

            return new SQLiteAsyncConnection(path);
        }
    }
}

My MainPage (testpage) is as follows:

using SQLite;
using Xamarin.Forms;

namespace TestSQLite
{
    public class ControlledDrugs
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Drug { get; set; }
        public double Volume { get; set; }
    }
    public class Users
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public partial class MainPage : ContentPage
    {
        private SQLiteAsyncConnection _connection;

        public MainPage()
        {
            InitializeComponent();
            _connection = DependencyService.Get<IDatabaseConnection>().GetConnection();
        }

        protected override async void OnAppearing()
        {
            await _connection.CreateTableAsync<ControlledDrugs>();
            await _connection.CreateTableAsync<Users>();

            var drugs = await _connection.Table<ControlledDrugs>().ToListAsync();
            Drugslistview.ItemsSource = drugs;

            var user = await _connection.Table<Users>().ToListAsync();
            Userlistview.ItemsSource = user;

            base.OnAppearing();
        }
        async void OnAdd(object sender, System.EventArgs e)
        {
            var user = UserInput.Text;
//The next step generates the error
            await _connection.InsertAsync(user);
        }

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

        }

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

        }

    }
}

As you can see, I have yet to progress to update or delete. Learning by Youtube and Stackoverflow snippets is painfully slow. But this error got me stumped. Also, have NuGet package sqlite-net-pcl v1.5.166-beta installed Xamarin Visual Studio.

1

1 Answers

0
votes

you are attempting to insert a string into the User table instead of a User object

var user = UserInput.Text;
// user is just a string
await _connection.InsertAsync(user);

instead you need to create a User object

var user = new Users { Name = UserInput.Text };
await _connection.InsertAsync(user);