简体   繁体   English

是否可以在Windows Phone中使用现有的SQLite数据库?

[英]It it possible to use existing SQLite database in windows phone?

I am newbie to windows phone development. 我是Windows Phone开发的新手。 I have a requirement to develop a app which was developed in Android and iOS. 我需要开发在Android和iOS中开发的应用。 I need to use the same SQLite database which was used in Android and iOS. 我需要使用与Android和iOS中相同的SQLite数据库。 Is it possible to use existing SQLite database in windows phone? 是否可以在Windows Phone中使用现有的SQLite数据库? If it is yes how to do it ? 如果是,该怎么办?

for basic setup of sqlite use this link 对于sqlite的基本设置,请使用此链接

first add the database file to project and change its properties to COPYALWAYS and "CONTENT" and now copy it to you local folder and make connection to the database using sqlite like this. 首先将数据库文件添加到project中,并将其属性更改为COPYALWAYS和“ CONTENT”,然后将其复制到您的本地文件夹,并使用sqlite这样连接到数据库。

 public static SQLiteAsyncConnection connection;
    public static bool isDatabaseExisting;
    public static async void ConnectToDB()
    {
        try
        {
            StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("DatabaseFinal.db");
            isDatabaseExisting = true;
        }
        catch
        {
            isDatabaseExisting = false;
        }
 if (!isDatabaseExisting)
        {
            try
            {
                StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("DatabaseFinal.db");
                await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
                isDatabaseExisting = true;
            }
            catch
            {
                isDatabaseExisting = false;
            }
        }

        if(isDatabaseExisting)
        connection = new SQLiteAsyncConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path, "DelhiMetroDatabaseFinal.db"), true);
    }
}

} }

this will create your connection too and for using databse for getting data from your db file use can use query like this 这也将创建您的连接,并使用databse从您的db文件中获取数据,可以使用像这样的查询

  public List<dataclass> getdata(string line_id, ref Boolean isdata)
    {
        List<datadataclass> query = new List<datadataclass>();

        try
        {
            query = dal.connection.Table<dataclass>().Where(i => i._id == line_id).ToListAsync().Result;
            isdataclass = true;
            return query;
        }
        catch (NullReferenceException e)
        {
            isdataclass = false;
        }

        return query;
    }

data class will have properties as your column defined in you database. 数据类将具有属性作为数据库中定义的列。

class dataclass
{
    public string _id { get; set; }
    public string name { get; set; }
    public int location { get; set; }
}

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

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