概述: 我正在使用 SQLLite 查询 Windows Phone 8.1 解决方案中的现有数据库。我已经改编了这个 solution将数据库数据读回我的项目。但是当我使用数据库连接调用 ToList() 时,我得到了一个没有这样的数据库的错误。
到目前为止,我已经附加了数据库文件,我的 DBHelper 类使用以下代码来查询数据:
using (var dbConn = new SQLiteConnection(dbPath))
{
//No such table error thrown here ->
List<ZoneInfo> zoneInfo = dbConn.Table<ZoneInfo>().ToList<ZoneInfo>();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
这是完整的 DBHelper从我的解决方案中引用现有数据库文件并将其复制到设备的本地文件夹的类。数据库文件本身是 here .
调试步骤:
- 我检查了 ZoneInfo 类属性,检查它们是否与下面数据库架构中每个字段的类型/名称匹配,并且它们匹配。
- dbPath 名称与附加的数据库名称匹配,所以这也不是问题。
- 我还找到了 similar question与 Android 上的 SQLLite 相关,这表明我对表的查询可能存在问题。
- 我还检查了 dbconn 变量,它向我显示了有关错误的更多信息:
我应该采取什么步骤来进一步调试 SQLLite“无表”错误?
异常详情: 异常的具体细节如下,它告诉我没有这样的 ZoneInfo 表:
SQLite.SQLiteException was unhandled by user code
HResult=-2146233088
Message=no such table: ZoneInfo
Source=Parking Tag Picker WRT
StackTrace:
at SQLite.SQLite3.Prepare2(IntPtr db, String query)
at SQLite.SQLiteCommand.Prepare()
at SQLite.SQLiteCommand.<ExecuteDeferredQuery>d__0`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at SQLite.SQLiteCommand.ExecuteQuery[T]()
at SQLite.TableQuery`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Parking_Tag_Picker_WRT.Helpers.DatabaseHelper.ReadZones(String dbPath)
at Parking_Tag_Picker_WRT.ViewModel.TagRequestViewModel.InitZoneInfoAsync()
at Parking_Tag_Picker_WRT.TagRequestPage.OnNavigatedTo(NavigationEventArgs e)
InnerException:
ZoneInfo 类:(将数据库数据映射到一个类)
public class ZoneInfo
{
//The ObjectId property is marked as the Primary Key
[SQLite.PrimaryKey]
[Column("results_list_objectId")]
public string ObjectId { get; set; }
[Column("results_list_zone")]
public string ZoneName { get; set; }
[Column("results_list_tariff_ph")]
public int? TariffPH { get; set; }
[Column("results_list_tariff_pd")]
public int? TariffPD { get; set; }
[Column("results_list_restrictions")]
public string Restrictions { get; set; }
[Column("results_list_days_of_operation")]
public string DaysOpen { get; set; }
[Column("results_list_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;
}
}
数据库模式:
数据库在解决方案中的位置:
最佳答案
根据您的 github 存储库,您的 dbpath
仅代表正确的相对路径 Databases/DublinCityCouncilTable.db
。现在要创建连接,您需要提供复制的数据库的绝对路径,这将是
using (var dbConn = new SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path,@"Databases\DublinCityCouncilTable.db"), true))
关于c# - 如何解决 SQLite "no such table"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759217/