SQLLITE-Xamarin Form -如何检查一个值是否已经存在



我想检查一个值是否已经存在,如果存在则返回true,如果不为false。但是,我可能在逻辑上遗漏了一些东西,因为即使值存在

,也总是返回null。示例

public bool ExistPref(int userid)
{
var result = prefDB.db.Table<notificationsPreferences>().Where(t => t.UserId == userid).FirstOrDefault();
Console.WriteLine("user exist "+result+userid);
return result != null;
}

以及用户是否存在我想更新他的记录,否则插入新的值

public int UpdateNotifications(notificationsPreferences item)
{
if (item.UserId != 0)
{
return prefDB.db.Update(item);
}

else
{
return  prefDB.db.Insert(item);

问题是在这里** Id进入对象项目,但由于某种原因不保存4位数的Id,得到保存的是例如4,我假设是我添加这个项目的次数?/这就是为什么我的结果总是假的,因为它没有保存真实的id。

}
}

如果用户存在,我想更新他的记录,否则插入新的值

如果你想在sqlite中插入新记录或更新记录,你可以看看下面的代码:

private void insertorupdate(User model)
{
var data = _SQLiteConnection.Table<User>();
var d1 = data.Where(x => x.userId == model.userId).FirstOrDefault();
if (d1 == null)
{
_SQLiteConnection.Insert(model);
Console.WriteLine("Sucessfully Added");
}
else
{
Console.WriteLine("Already Mail id Exist");
_SQLiteConnection.Update(model);
}
}
public class User
{
[PrimaryKey, AutoIncrement]
public int userId { get; set; }
public string userName { get; set; }      
public string password { get; set; }

}

useAny

return prefDB.db.Table<notificationsPreferences>().Any(t => t.UserId == userid);

相关内容

最新更新