c# - SQLite in Xamarin throws exception in connection -
i have problem. using xamarin ios , android applications. throws exception when try build connection folder path. code: public class queries { private string folder = dependencyservice.get().getappdatafolder();
public bool createdatabase() { try { using (var connection = new sqliteconnection(path.combine(folder, "test.db"))) { connection.createtable<posten>(); return true; } } catch (sqliteexception ex) { system.diagnostics.debug.writeline("sqliteex", ex.message); return false; } } }
it throws exception in using. use dependencyservice path want put database in.
private string folder = dependencyservice.get<ifilesystemservice>().getappdatafolder();
on android side (i'm testing android) have implemented this:
public class filesystemserviceandroid : ifilesystemservice { public string getappdatafolder() { return system.environment.getfolderpath(system.environment.specialfolder.personal); } }
this exception says:
unhandled exception:
system.typeinitializationexception: type initializer >'sqlite.sqliteconnection' threw exception. occurred
i moved code try access createdatabase method toolbar initialized , got message box in application:
in project sqlite.pcl in xamarin.android, using following database folder , file location:
string dbfolder = android.os.environment.externalstoragedirectory.absolutepath + "/project_name/"; string dbfilename = "project_name.db"; string dbpath = dbfolder + dbfilename; //to check if database file exists: system.io.file.exists(dbpath); //to delete database file: system.io.file.delete(dbpath); sqliteconnection dbconn = new sqliteconnection(dbpath);
Comments
Post a Comment