Xamarin表单SQlite ID自动增量不起作用

时间:2018-11-08 13:43:26

标签: sqlite xamarin xamarin.forms

我正在尝试将一些值插入sqlite数据库。 ID具有主键,自动递增属性。第一个值已成功插入。第二次插入时,ID不会增加。因此,一次又一次插入值,而不是更新。我已经安装了sqlite-net-pcl库。请帮助。

using PCLStorage;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;


public class SQLHelper
    {
        static object locker = new object();

        readonly SQLiteConnection database;

        public SQLHelper()
        {
            database = GetConnection();
            // create the tables  
            database.CreateTable<Offline>();
            database.CreateTable<Category>();
            database.CreateTable<Product>();
            database.CreateTable<Password>();
        }

        public SQLiteConnection GetConnection()
        {
            SQLiteConnection sqlitConnection;
            var sqliteFilename = "Offline.db";
            IFolder folder = PCLStorage.FileSystem.Current.LocalStorage;
            string path = PortablePath.Combine(folder.Path.ToString(), 
            sqliteFilename);
            sqlitConnection = new SQLite.SQLiteConnection(path);
            return sqlitConnection;
        }     

public long Save_password(Password password)
        {
            lock (locker)
            {
                if (password.p_id != 0)
                {
                    return database.Update(password);
                }
                else
                {
                    return database.Insert(password);
                }


            }
        }
}

public class Password 
    {                            
        [PrimaryKey,AutoIncrement]
        public long p_id { set; get; }              

        public string password { get; set; }
        public string type { get; set; }
        public string tb_num { get; set; }        
    }

cs页面:

                    string lbl_1 = e1.Text;
                    string lbl_2 = e2.Text;
                    if (lbl_1.Equals(lbl_2))
                    {
                        Password password = new Password();
                        password.password = lbl_2;
                        long i = App.Database.Save_password(password);
                        if (i > 0)
                        {
                            Debug.WriteLine(i);
                            DependencyService.Get<IToast>().LongAlert("Success");
                        }

                        Navigation.PopAsync();
                    }
                    else
                    {
                        DependencyService.Get<IToast>().LongAlert("Passwords Mismatch!");
                    }

0 个答案:

没有答案