C# 在SQLite Windows Phone 8.1中创建多个表
我指的是在SQLite数据库中存储数据。我正在尝试向数据库中添加另一个名为“Schools”的表。但它抛出了一个例外,即没有这样的表格:学校 班级C# 在SQLite Windows Phone 8.1中创建多个表,c#,sqlite,windows-phone-8.1,C#,Sqlite,Windows Phone 8.1,我指的是在SQLite数据库中存储数据。我正在尝试向数据库中添加另一个名为“Schools”的表。但它抛出了一个例外,即没有这样的表格:学校 班级 public class Contacts { //The Id property is marked as the Primary Key [SQLite.PrimaryKey, SQLite.AutoIncrement] public int Id { get; set; } public string Name
public class Contacts
{
//The Id property is marked as the Primary Key
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Address { get; set; }
public string School { get; set; }
public string Gardient { get; set; }
public string PhoneNumber { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string CreationDate { get; set; }
public Contacts()
{
//empty constructor
}
public Contacts( string name, string age, string address, string school, string gardient, string phone_no, string latitude, string longitude)
{
Name = name;
Age = age;
Address = address;
School = school;
Gardient = gardient;
PhoneNumber = phone_no;
Latitude = latitude;
Longitude = longitude;
CreationDate = DateTime.Now.ToString();
}
}
public class Schools
{
//The Id property is marked as the Primary Key
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int Id { get; set; }
public string School { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public string CreationDate { get; set; }
public Schools()
{
//empty constructor
}
public Schools( string school,string latitude, string longitude)
{
School = school;
Latitude = latitude;
Longitude = longitude;
CreationDate = DateTime.Now.ToString();
}
}
数据库帮助类
public class DatabaseHelperClass
{
SQLiteConnection dbConn;
//Create Tabble
public async Task<bool> onCreate(string DB_PATH)
{
try
{
if (!CheckFileExists(DB_PATH).Result)
{
using (dbConn = new SQLiteConnection(DB_PATH))
{
dbConn.CreateTable<Schools>();
dbConn.CreateTable<Contacts>();
}
}
return true;
}
catch
{
return false;
}
}
public void createtable()
{
SQLite.SQLiteConnection db = new SQLite.SQLiteConnection(DB_PATH);
db.CreateTable<Schools>();
db.CreateTable<Contacts>();
}
private async Task<bool> CheckFileExists(string fileName)
{
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return true;
}
catch
{
return false;
}
}
公共类数据库HelperClass
{
sqliteconnectiondbconn;
//创建选项卡
公共异步任务onCreate(字符串DB_路径)
{
尝试
{
如果(!CheckFileExists(DB_PATH).Result)
{
使用(dbConn=newsqliteconnection(DB_路径))
{
dbConn.CreateTable();
dbConn.CreateTable();
}
}
返回true;
}
抓住
{
返回false;
}
}
公共void createtable()
{
SQLite.SQLiteConnection db=new SQLite.SQLiteConnection(db_路径);
db.CreateTable();
db.CreateTable();
}
私有异步任务CheckFileExists(字符串文件名)
{
尝试
{
var store=wait Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(文件名);
返回true;
}
抓住
{
返回false;
}
}
当我试图通过文本框将数据输入学校表时,此异常会引发。数据输入联系人表时没有任何问题。我发现了类似的问题,但没有人回答我的问题:(当应用程序已经安装在设备或模拟器上时,您似乎已经创建了表。只需卸载应用程序并重新安装它。它会工作。这是因为所有表都是在您第一次安装应用程序时创建的。希望这会有所帮助。您是否使用相同的代码在app()中的app.xaml.cs文件中创建表建造师?这就是问题所在。我错过了那个部分。非常感谢你的提醒。如果我的回答对你有帮助的话……请标记为正确,这样其他人也可以享受:)