简体   繁体   中英

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://dotnetslackers.com/articles/silverlight/Windows-Phone-7-Native-Database-Programming-via-Sqlite-Client-for-Windows-Phone.aspx 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.

Ok, I think I've solved it. I put in a few more TRY-CATCHES and noticed that it wasn't actually creating my tables, so it had nowhere to insert the data. What I think was causing this was when I was passing my model class as the type by doing; it was messing it up somehow, so I just added a using for the Models folder, then just passed in SleepTrackerModel as the type and it seems to have fixed the problem (I hope) and I'm getting no more exceptions.

试试这个也许可以帮助您(SQLite通用应用程序): http : //depblog.weblogs.us/2015/01/06/sqlite-net-and-sqlite-net-extensions-with-universal-apps/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM