繁体   English   中英

在SQLite Windows Phone 8.1中创建多个表

[英]Create Multiple Tables in SQLite Windows Phone 8.1

我指的是本教程将数据存储在SQLite数据库中。 我正在尝试将另一个表添加到名为“学校”的数据库中。 但是它抛出一个异常,如下所示:没有这样的表:学校。

在此处输入图片说明

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();
        }
    }

DatabaseHelperClass

   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;
            }
        }

当我尝试通过文本框向Schools表输入数据时,会抛出此异常。 数据输入到联系人表中没有任何问题。 我发现了类似的问题,但没有一个回答我的问题:(

看来您已经在设备或仿真器上安装了该应用程序时创建了表,只需卸载应用程序并重新安装它就可以了,这是因为所有表都是在首次安装应用程序时创建的。这很有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM