lundi 23 février 2015

SQLite insert issue with WP8

I'm developing an app for Windows Phone 8 and I'm using SQLite to store data (I'm new to both of these areas), and I've ran into a problem when trying to insert data. For my solution I'm using the MVVM pattern, so I'm thinking maybe my insert method was done wrong because I tried to code my CRUD operations using generic types which would work with all the Model classes rather than having 6-7 insert methods for each Model class, or maybe it's the way I'm using my insert method in my save button that uses an ActionCommand. I'm completely lost with it all. I've put in debugs and from what I can see my connection, etc. is fine.


Here's the message I get when I try and insert;



A first chance exception of type 'SQLite.SQLiteException' occurred in SleepTracker.DLL
at SQLite.SQLite3.Prepare2(Database db, String query)
at SQLite.PreparedSqlLiteInsertCommand.Prepare()
at SQLite.PreparedSqlLiteInsertCommand.ExecuteNonQuery(Object[] source)
at SQLite.SQLiteConnection.Insert(Object obj, String extra, Type objType)
at SQLite.SQLiteConnection.Insert(Object obj)
at SleepTracker.DatabaseResources.DBHelper.<>c__DisplayClass4`1.<Insert>b__0()
at SQLite.SQLiteConnection.RunInTransaction(Action action)
at SleepTracker.DatabaseResources.DBHelper.Insert[T](T obj)


And here's the code for my insert and how I'm using it;



public void Insert<T>(T obj) where T : new()
{
using (var conn = new SQLiteConnection(App.ConnectionString))
{
conn.RunInTransaction(() =>
{
conn.Insert(obj);
});
}
}


And how I'm using it in my button;



public ICommand SaveButtonCommand
{
get
{
return _saveButtonCommand
?? (_saveButtonCommand = new Resources.ActionCommand(() =>
{
Models.SleepTrackerModel insertSleepData = new Models.SleepTrackerModel
{
Date = SleepDate,
SleepTime = SleepTime,
WakeTime = WakeTime,
Duration = SleepDuration,
Rating = SleepRating
};
_dbHelper.Insert<Models.SleepTrackerModel>(insertSleepData);
}));
}
}


I'm completely lost on how to fix this, so any help would be great.


Also, I've tried following this tutorial; http://ift.tt/Te3CeK but when I try ExecuteNonQuery(obj); in the insert method, I get an error saying that ExecuteNonQuery doesn't take 1 argument. Would I need to update the SQLite.cs class with this or something?


Thanks.


Aucun commentaire:

Enregistrer un commentaire