簡體   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