问题
I'm using the SQLite libs and SQLite.Net to query an existing database in my Windows Phone 8.1 project. So far the connection to the db is executing fine but the records being returned are null.
I took the usual debugging steps -
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)
I don't get any compile time error as such but the records being returned are null.
Question: Does anyone know why there is an issue with the bindings provided for the database mapping?
When I stepped through the SQLite.cs class I found the binding count is 0:
Query Code:
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;
}
DB Mapping 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;
}
}
Database schema -
回答1:
Your "DB Mapping POCO" does not match your Database schema.
[Column("tariff_ph")]
public int? TariffPH { get; set; }
[Column("tariff_pd")]
public int? TariffPD { get; set; }
Should probably be
[Column("tariff_ph")]
public float TariffPH { get; set; }
[Column("tariff_pd")]
public int TariffPD { get; set; }
Since you have floating point values in your dataset and both of them are NOT NULLABLE.
I have an minimum example here Update Record in Sqlite Window Phone 8, that creates the database, inserts some data and updates the database. See if that can help you, but I'm pretty sure your data doesn't match correctly.
来源:https://stackoverflow.com/questions/35902785/how-to-resolve-null-bindings-on-sqlite-query