C# SQLite查询失败,但仅在脱机时失败
我有一个简单的SQLite方法,它返回一个给定两个参数的类——类型T和要搜索的参数值C# SQLite查询失败,但仅在脱机时失败,c#,sqlite,xamarin.ios,C#,Sqlite,Xamarin.ios,我有一个简单的SQLite方法,它返回一个给定两个参数的类——类型T和要搜索的参数值 public T GetSingleObject<T>(string id) where T:IIdentity, new() { lock (dbLock) { using (var sqlCon = new SQLiteConnection(DBPath)) { sqlCon.
public T GetSingleObject<T>(string id) where T:IIdentity, new()
{
lock (dbLock)
{
using (var sqlCon = new SQLiteConnection(DBPath))
{
sqlCon.Execute(Constants.DBClauseSyncOff);
sqlCon.BeginTransaction();
string sql = string.Format("SELECT * FROM {0} WHERE id=\"{1}\"", GetName(typeof(T).ToString()), id);
var data = sqlCon.Query<T>(sql);
return data[0];
}
}
}
我已经尝试了我能想到的所有方法来访问这张桌子-包括一个直道
sqlCon.ExecuteScalar<MyClass>("SELECT * FROM MyClass");
sqlCon.ExecuteScalar(“从MyClass中选择*);
没有离线,完美的在线数据
类本身看起来是这样的
using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using SQLite;
namespace Models
{
public class MyClass : IIdentity
{
[PrimaryKey]
public string id { get; set; }
public string home_id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public bool syncenabled { get; set; }
public string tradingname { get; set; }
public string address { get; set; }
public string town { get; set; }
public string state { get; set; }
public string country { get; set; }
public string securitystamp { get; set; }
public string password_question { get; set; }
public string password_answer { get; set; }
public string mobileServiceAuthenticationToken { get; set; }
public string providerUserKey { get; set; }
public string mobiledeviceid { get; set; }
public DateTime last_login { get; set; }
public DateTime __createdAt { get; set; }
public DateTime __updatedAt { get; set; }
public string user_type { get; set; }
public bool is_deleted { get; set; }
public bool account_enabled { get; set; }
public string subscription_id { get; set; }
[IgnoreDataMember, Ignore]
public List<MyUsers> UserModules { get { return AppDelegate.Self.DBManager.GetListOfObjects<MyUsers>("user_id", id); } }
public override string ToString()
{
return string.Format("[MyClasss: id={0}, home_id={1}, username={2}, password={3}, firstname={4}, lastname={5}, email={6}, syncenabled={7}, tradingname={8}, address={9}, town={10}, state={11}, country={12}, securitystamp={13}, password_question={14}, password_answer={15}, mobileServiceAuthenticationToken={16}, providerUserKey={17}, mobiledeviceid={18}, last_login={19}, __createdAt={20}, __updatedAt={21}, user_type={22}, is_deleted={23}, account_enabled={24}, subscription_id={25}, UserModules={26}]", id, home_id, username, password, firstname, lastname, email, syncenabled, tradingname, address, town, state, country, securitystamp, password_question, password_answer, mobileServiceAuthenticationToken, providerUserKey, mobiledeviceid, last_login, __createdAt, __updatedAt, user_type, is_deleted, account_enabled, subscription_id, UserModules);
}
}
使用系统;
使用System.Runtime.Serialization;
使用System.Collections.Generic;
使用SQLite;
名称空间模型
{
公共类MyClass:身份
{
[主密钥]
公共字符串id{get;set;}
公共字符串home_id{get;set;}
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公共字符串名{get;set;}
公共字符串lastname{get;set;}
公共字符串电子邮件{get;set;}
公共bool syncenabled{get;set;}
公共字符串tradingname{get;set;}
公共字符串地址{get;set;}
公共字符串{get;set;}
公共字符串状态{get;set;}
公共字符串国家{get;set;}
公共字符串securitystamp{get;set;}
公共字符串密码\u问题{get;set;}
公共字符串密码\u答案{get;set;}
公共字符串mobileServiceAuthenticationToken{get;set;}
公共字符串providerUserKey{get;set;}
公共字符串mobiledeviceid{get;set;}
公共日期时间上次登录{get;set;}
公共日期时间uu createdAt{get;set;}
公共日期时间uu updatedAt{get;set;}
公共字符串用户类型{get;set;}
公共布尔已删除{get;set;}
公共bool帐户启用{get;set;}
公共字符串订阅\u id{get;set;}
[忽略数据成员,忽略]
公共列表UserModules{get{return AppDelegate.Self.DBManager.GetListOfObjects(“user_id”,id);}
公共重写字符串ToString()
{
返回string.Format(“[MyClasss:id={0},home\u id={1},username={2},password={3},firstname={4},lastname={5},email={6},syncenabled={7},tradingname={8},address={9},town 10},state={11},country={12},MobilityStamp={13},password={14},password={answer={15},Authentication 16},UserKey={={18} ,last_login={19},_createdAt={20},_updatedAt={21},user_type={22},is_deleted={23},account_enabled={24},subscription_id={25},UserModules={26},id,home\u id,用户名,密码,firstname,lastname,email,syncenabled,tradingname,地址,城镇,州,国家,securitystamp,密码问题,密码答案,mobileServiceAuthenticationToken,providerUserKey,mobiledeviceid,上次登录,创建数据,更新数据,用户类型,已删除,已启用帐户,订阅id,用户模块);
}
}
脱机时,我看不到任何内容会导致问题。查询总是返回null脱机,数据联机。旁注:代码尖叫SQL注入。将其更改为使用参数。您的数据库文件位于哪里?我假设是本地的。因此我认为您面临的错误与设备的连接状态无关。我已经检查了数据库的连接,正常工作,其他每个表正常工作并返回值,但这个表总是返回null.ctacke-我在其他地方有SQL保护代码:)
sqlCon.ExecuteScalar<MyClass>("SELECT * FROM MyClass");
using System;
using System.Runtime.Serialization;
using System.Collections.Generic;
using SQLite;
namespace Models
{
public class MyClass : IIdentity
{
[PrimaryKey]
public string id { get; set; }
public string home_id { get; set; }
public string username { get; set; }
public string password { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string email { get; set; }
public bool syncenabled { get; set; }
public string tradingname { get; set; }
public string address { get; set; }
public string town { get; set; }
public string state { get; set; }
public string country { get; set; }
public string securitystamp { get; set; }
public string password_question { get; set; }
public string password_answer { get; set; }
public string mobileServiceAuthenticationToken { get; set; }
public string providerUserKey { get; set; }
public string mobiledeviceid { get; set; }
public DateTime last_login { get; set; }
public DateTime __createdAt { get; set; }
public DateTime __updatedAt { get; set; }
public string user_type { get; set; }
public bool is_deleted { get; set; }
public bool account_enabled { get; set; }
public string subscription_id { get; set; }
[IgnoreDataMember, Ignore]
public List<MyUsers> UserModules { get { return AppDelegate.Self.DBManager.GetListOfObjects<MyUsers>("user_id", id); } }
public override string ToString()
{
return string.Format("[MyClasss: id={0}, home_id={1}, username={2}, password={3}, firstname={4}, lastname={5}, email={6}, syncenabled={7}, tradingname={8}, address={9}, town={10}, state={11}, country={12}, securitystamp={13}, password_question={14}, password_answer={15}, mobileServiceAuthenticationToken={16}, providerUserKey={17}, mobiledeviceid={18}, last_login={19}, __createdAt={20}, __updatedAt={21}, user_type={22}, is_deleted={23}, account_enabled={24}, subscription_id={25}, UserModules={26}]", id, home_id, username, password, firstname, lastname, email, syncenabled, tradingname, address, town, state, country, securitystamp, password_question, password_answer, mobileServiceAuthenticationToken, providerUserKey, mobiledeviceid, last_login, __createdAt, __updatedAt, user_type, is_deleted, account_enabled, subscription_id, UserModules);
}
}