Xamarin - SQLite.SQLiteException:使用 SQLite-net-pcl 的约束 [英] Xamarin - SQLite.SQLiteException: Constraint using SQLite-net-pcl

查看:34
本文介绍了Xamarin - SQLite.SQLiteException:使用 SQLite-net-pcl 的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的两个项目都转移到了 SQLite.net-pcl.到目前为止,我遇到了许多问题,这是我的最新问题.每次我在 iOS 上运行我的应用程序时,它都会在某个时候崩溃并显示 SQLite.SQLiteException: Constraint.Android 运行在同一个 PCL 上,并且将第一次运行,但之后给我 SQLite.SQLiteException: Constraint,直到我删除我的应用程序存储.不知道这里还有什么可以尝试的.

I have recently moved both of my projects over to SQLite.net-pcl. I have ran into a number of problems so far and this is my latest. Everytime I run my app on iOS it crashes at some point and says SQLite.SQLiteException: Constraint. Android is running on the same PCL and will work first time around but afterwards gives me SQLite.SQLiteException: Constraint until I delete my application storage. Not sure what else to try here.

代码示例:-

对象

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);
}

这是我的项目当前引用的内容:-

Here's what my project is currently referencing:-

推荐答案

好的,我已经找到了一些关于为什么会发生这种情况的信息......

Okay so I have found some information on why this was happening...

所以 SQLiteException: Constraint 与您在表上是否有 UNIQUENOT NULLCHECK 约束有关,并且您尝试执行更新插入.在此处阅读更多SQLite 约束

So SQLiteException: Constraint is related to if you have a UNIQUE, NOT NULL, or CHECK constraint on a table and you try to do an UPDATE or INSERT. Read more here SQLite Contraints

所以我的问题是我有我的主键(它总是唯一的),以及检查数据库中项目的错误代码,而不是更新它,它试图插入它.由于主键相同,因此抛出错误.现在已经解决了.

So my problem was that I have my Primary Key (Which will always be unique), and incorrect code that was checking an item in the database, and instead of updating it, it was trying to insert it. Due to the primary key being the same it was throwing an error. This has now been solved.

这篇关于Xamarin - SQLite.SQLiteException:使用 SQLite-net-pcl 的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆