lundi 6 avril 2015

Xamarin.Android - Persistent Could Not Open Database file Exception

I was previously using a SQLiteOpenHelper,but decided to take that out and use just the SQLite.NET ORM only.After i made that change,i keep getting the "could not open database file" exception.I have tried a few suggestions from this site, e.g the approach below:



DB_PATH = Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.Personal));

var conn = new SQLiteConnection (System.IO.Path.Combine (DB_PATH, DATABASE_NAME));


but the application either the application crushes or i get the same error.


Below is my code: I click on a log in button from a different activity,which is meant to verify the user's details from the database.This is where i make calls to connecting with the database.



namespace sample.App
{

[Activity (MainLauncher = true)]
public class MainActivity : Activity
{

public List<UsersTable>GetAllUserNames()
{
List<UsersTable> allUserNames = new List<UsersTable> ();

allUserNames = dataManager.GetSingleUserName ();

string name = "";

foreach(var UserName in allUserNames)
{
Console.WriteLine ("Usernames from db :" + name.ToString());
}

return allUserNames;
}

}
}


The DataManager class:



public List<UsersTable> GetSingleUserName()
{
UsersTable user = new UsersTable ();

using (var db = dbHandler.getUserDatabaseConnection ()) {


var userName = db.Query<UsersTable > ("select * from UsersTable where user_name = ?", user.USER_NAME);

return userName;
}

}


The DatabaseHandler class where the db is created and the connection as well



namespace com.Sample.Database
{
public class DatabaseHandler
{

//Constructor
private DatabaseHandler (Context context)
{
try
{

string dataDirectory = Android.OS.Environment.DataDirectory.AbsolutePath;
this.COMPANY_DATABASE_NAME = "first_" + "v" + this.databaseVersion + ".db";

string employerDBPath = "/data/" + "com.employer.company" + "/databases/" + COMPANY_DATABASE_NAME;
string userDBPath = "/data/" + "com.user.test" + "/databases/" + USER_DATABASE_NAME;


this.COMPANY_DATABASE_NAME = dataDirectory + employerDBPath;
this.USER_DATABASE_PATH = dataDirectory + userDBPath;
this.userDatabaseConnection = new SQLiteConnection (this.USER_DATABASE_PATH);
this.userDatabaseConnection.CreateTable<UserInfoTable> ();
this.InsertAdminData ();

catch(Exception ex)
{


}
}
public SQLiteConnection getNuseisDatabaseConnection()
{
var connection = new SQLiteConnection (this.COMPANY_DATABASE_PATH);

return connection;

}
}


}
}


The error is on the call to the getDatabaseException() method from the DataManager class.


Aucun commentaire:

Enregistrer un commentaire