It it possible to use existing SQLite database in windows phone?
I am newbie to windows phone development. I have a requirement to develop a app which was developed in Android and iOS. I need to use the same SQLite database which was used in Android and iOS. Is it possible to use existing SQLite database in windows phone? If it is yes how to do it ?
for basic setup of sqlite use this link
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.
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
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; }
}
链接地址: http://www.djcxy.com/p/17202.html
上一篇: Windows手机应用程序目标版本