2017-01-22 45 views
0

我最近把我的兩個項目都移到了SQLite.net-pcl。到目前爲止,我遇到了很多問題,這是我的最新成果。每次我在iOS上運行我的應用程序時,都會崩潰,並說SQLite.SQLiteException:約束。 Android在同一個PCL上運行,並且會在第一時間工作,但之後會給我SQLite.SQLiteException:約束,直到我刪除我的應用程序存儲。不知道還有什麼要嘗試在這裏。Xamarin - SQLite.SQLiteException:使用SQLite-net-pcl的約束

代碼示例: -

對象

using System; 

namespace App.LocalObjects 
{ 
[Preserve(AllMembers = true)] 
public class LocalArticle 
{ 

    [SQLite.PrimaryKey, SQLite.Column("articleObjectId")] 
    public string objectId { get; set; } 
    public DateTime createdAt { get; set; } 
    public DateTime updatedAt { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public string Url { get; set; } 
    public int Status { get; set; } 

    public LocalArticle() 
    { 
     this.objectId = " "; 
     this.Title = " "; 
     this.Description = " "; 
     this.Url = " "; 
    } 
} 
} 

數據庫

using System; 
using System.Linq; 
using System.Collections.Generic; 
using SQLite; 
using App.LocalObjects; 

namespace App.DataLayer 
{ 

public class Database 
{ 
    static object locker = new object(); 
    public SQLiteConnection database; 
    public string path; 

    public Database(SQLiteConnection conn) 
    { 
     database = conn; 
     database.CreateTable<LocalArticle>(); 
    } 

public int SaveLocalArticleItem(LocalArticle item) 
    { 
     lock (locker) 
     { 

      LocalArticle article = database.Table<LocalArticle>().FirstOrDefault(x => x.objectId == item.objectId); 

      if (article != null && article.objectId.Equals(item.objectId) && !article.updatedAt.Equals(item.updatedAt)) 
      { 
       database.Update(item); 

       return item.ID; 
      } else { 
       return database.Insert(item); 
      } 
     } 
    } 

代碼來初始化數據庫:

using System; 
using System.IO; 
using Android.App; 
using App.BusinessLayer.Managers; 
using App.BusinessLayer.ParseObjects; 
using App.Utils; 
using Android.Content; 
using Com.Nostra13.Universalimageloader.Core; 
using Com.Nostra13.Universalimageloader.Core.Assist; 
using Android.Graphics; 
using Com.Nostra13.Universalimageloader.Cache.Memory.Impl; 
using Com.OneSignal; 
using Parse; 
using SQLite; 

namespace App.Droid 
{ 
[Application(LargeHeap = true)] 
public class App : Application 
{ 

    public static App Current { get; private set; } 
    public ModelManager modelManager { get; set; } 
    private SQLiteConnection conn; 
    private ImageLoader imageLoader; 

public App(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer) 
     : base(handle, transfer) 
    { 
     Current = this; 
    } 

public override void OnCreate() 
    { 
     base.OnCreate(); 

     SetupParse(); 
     SetupDB(); 
} 

private void SetupParse() 
    { 
     ParseObject.RegisterSubclass<ParseArticle>(); 

     ParseClient.Initialize(new ParseClient.Configuration 
     { 
      ApplicationId = Constants.ParseApplicationID, 
      Server = Constants.ParseServer 
     }); 


    } 

    private void SetupDB() 
    { 

     var sqliteFilename = Constants.DBName; 
     string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
     var path = System.IO.Path.Combine(libraryPath, sqliteFilename); 
     conn = new SQLiteConnection(path); 

     modelManager = new ModelManager(conn); 
} 

這裏是我的項目目前引用: -

enter image description here

回答

1

好了,所以我已經發現爲什麼這發生了一些資料...

所以SQLiteException:約束有關,如果你有UNIQUE,NOT NULLCHECK約束在一張桌子上,你試圖做一個UPDATEINSERT。在這裏閱讀更多SQLite Contraints

所以我的問題是我有我的主鍵(這將永遠是唯一的),並檢查數據庫中的項目的不正確的代碼,而不是更新它,它試圖插入它。由於主鍵相同,所以拋出錯誤。這現在已經解決了。