Suppose you have created a flex desktop application with SQLite database. And a user installed that application in his system and suppose during use of that application he has entered some important data inside the database of that application. So when he uninstall that app to install a new version of that application then his previous important data will be gone from his application, and he will lose all his data.
So what is the solution of this problem??
The solution of this problem is, After installation when we first run our application at that time if we programmatically copy the SQLite database file from our installeddirectory and put it inside My Documents directory and set the database path to that and in our whole application we use that database path then, our data will never gone.
|
Let’s have a look at the below code snippets : |
public var appPath:String = ''; public var restorePath:String = ''; public var dbPath:String = ''; // TO STORE DATABASE PATH //this function is used to set the database path. public function setEmpDatabasePath():void { appPath = File.applicationDirectory.nativePath; //contains the path where the application is installed. restorePath = File.documentsDirectory.nativePath; //contains the path upto system's My Documents directory. var file:File = new File(File.documentsDirectory.nativePath + "/EMPDB.db"); // check whether the db file in My Documents directory is exist or not if(file != null && file.exists) { dbPath = file.nativePath; //set the database path } else { var fileToCopyFrom:File = File.applicationDirectory.resolvePath(appPath + "//EMPDB.db"); // copy the db file from application directory var fileToCopyTo:File = File.documentsDirectory; fileToCopyTo = fileToCopyTo.resolvePath(restorePath + "//EMPDB.db"); //select the path i where the db will be overwrite. fileToCopyFrom.copyTo(fileToCopyTo, true); // create or overwrite the db file in the documentary directory. dbPath = file.nativePath; //set the database path } }