如何解决 SQLite 查询上的空绑定?
How to resolve null bindings on SQLite query?
我正在使用 SQLite 库和 SQLite.Net 查询我的 Windows Phone 8.1 项目中的现有数据库。到目前为止,与数据库的连接执行正常,但返回的记录为空。
我采用了通常的调试步骤 -
1.Checked the binding types and names which map to the names in my db schema.
2.Verified the data exists in the DB.
3.Stepped through the code that queries the database, found that the db bindings are null. (This suggests to me an issue with the field mappings in my POCO)
我没有收到任何编译时错误,但返回的记录为空。
问题:
有谁知道为什么为数据库映射提供的绑定有问题?
当我单步执行 SQLite.cs class 时,我发现绑定计数为 0:
查询代码:
using (var dbConn = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, AppDBPath), true))
{
List<ZoneInfo> zoneInfo = dbConn.Query<ZoneInfo>("select * from " + tableName).ToList<ZoneInfo>();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
数据库映射 POCO:
public class ZoneInfo
{
//The ObjectId property is marked as the Primary Key
[SQLite.PrimaryKey]
[Column("objectId")]
public string ObjectId { get; set; }
[Column("zone")]
public string ZoneName { get; set; }
[Column("tariff_ph")]
public int? TariffPH { get; set; }
[Column("tariff_pd")]
public int? TariffPD { get; set; }
[Column("restrictions")]
public string Restrictions { get; set; }
[Column("days_of_operation")]
public string DaysOpen { get; set; }
[Column("hours_of_operation")]
public string HoursOpen { get; set; }
public ZoneInfo()
{
}
public ZoneInfo(string objectId, string zoneName, int tariffPH, int tariffPD,
string restrictions, string daysOpen, string hoursOpen )
{
ObjectId = objectId;
ZoneName = zoneName;
TariffPH = tariffPH;
TariffPD = tariffPD;
Restrictions = restrictions;
DaysOpen = daysOpen;
HoursOpen = hoursOpen;
}
}
数据库架构 -
您的 "DB Mapping POCO" 与您的数据库架构不匹配。
[Column("tariff_ph")]
public int? TariffPH { get; set; }
[Column("tariff_pd")]
public int? TariffPD { get; set; }
应该是
[Column("tariff_ph")]
public float TariffPH { get; set; }
[Column("tariff_pd")]
public int TariffPD { get; set; }
因为您的数据集中有浮点值,并且它们都不可为空。
我这里有一个最小示例 Update Record in Sqlite Window Phone 8,它创建数据库、插入一些数据并更新数据库。看看这是否对您有帮助,但我很确定您的数据不正确匹配。
我正在使用 SQLite 库和 SQLite.Net 查询我的 Windows Phone 8.1 项目中的现有数据库。到目前为止,与数据库的连接执行正常,但返回的记录为空。
我采用了通常的调试步骤 -
1.Checked the binding types and names which map to the names in my db schema.
2.Verified the data exists in the DB.
3.Stepped through the code that queries the database, found that the db bindings are null. (This suggests to me an issue with the field mappings in my POCO)
我没有收到任何编译时错误,但返回的记录为空。
问题: 有谁知道为什么为数据库映射提供的绑定有问题?
当我单步执行 SQLite.cs class 时,我发现绑定计数为 0:
查询代码:
using (var dbConn = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, AppDBPath), true))
{
List<ZoneInfo> zoneInfo = dbConn.Query<ZoneInfo>("select * from " + tableName).ToList<ZoneInfo>();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
数据库映射 POCO:
public class ZoneInfo
{
//The ObjectId property is marked as the Primary Key
[SQLite.PrimaryKey]
[Column("objectId")]
public string ObjectId { get; set; }
[Column("zone")]
public string ZoneName { get; set; }
[Column("tariff_ph")]
public int? TariffPH { get; set; }
[Column("tariff_pd")]
public int? TariffPD { get; set; }
[Column("restrictions")]
public string Restrictions { get; set; }
[Column("days_of_operation")]
public string DaysOpen { get; set; }
[Column("hours_of_operation")]
public string HoursOpen { get; set; }
public ZoneInfo()
{
}
public ZoneInfo(string objectId, string zoneName, int tariffPH, int tariffPD,
string restrictions, string daysOpen, string hoursOpen )
{
ObjectId = objectId;
ZoneName = zoneName;
TariffPH = tariffPH;
TariffPD = tariffPD;
Restrictions = restrictions;
DaysOpen = daysOpen;
HoursOpen = hoursOpen;
}
}
数据库架构 -
您的 "DB Mapping POCO" 与您的数据库架构不匹配。
[Column("tariff_ph")]
public int? TariffPH { get; set; }
[Column("tariff_pd")]
public int? TariffPD { get; set; }
应该是
[Column("tariff_ph")]
public float TariffPH { get; set; }
[Column("tariff_pd")]
public int TariffPD { get; set; }
因为您的数据集中有浮点值,并且它们都不可为空。
我这里有一个最小示例 Update Record in Sqlite Window Phone 8,它创建数据库、插入一些数据并更新数据库。看看这是否对您有帮助,但我很确定您的数据不正确匹配。