Any way to rename a database of a published app? - android

Is there anyway to rename a database of a published android app? I am working something out to just change the database name and onCreate of that database, check for the old database and copy the contents out, but wondering if thats my only recourse.

You could try renaming the file before opening it the first time. It is located in:
getApplication().getDatabasePath("databasename");
Replace "databasename" with the one you are using.
And when you open it for the first time afterwards, use the new name you have selected.

You can use following java commands
File f = new File(PATH + DB_NAME);
f.renameTo(new File(PATH + NEWName));
Where PATH is the path on the device

File database=getApplicationContext().getDatabasePath("oldDatabase.db");
if (database.exists()) {
File newPath = getApplicationContext().getDatabasePath("newDatabase.db");
database.renameTo(newPath);
}

Related

How to create a sub directory inside a private directory in android

I have created a directory like this:
File mydir = context.getDir("my_private_dir", Context.MODE_PRIVATE);
Now I want to create another directory inside my_private_dirfor which I've tried doing it this way:
File file = new File(mydir.getAbsolutePath()+ File.separator + date);
if (file.mkdir())
Log.d(context.getClass().getSimpleName()," date dir created");
I don't know why this is not working out. The subdirectory is not getting created at all.
I want all the directories to be private and only my app should be able to access it. SO I thought to make the parent directory private and to create subdirectories inside it normally will be enough. But I'm not able to understand why the subdirectory is not getting created.
Can anyone please help me with this problem?
Thanks in advance :)
Regards
Try to use mkdirs() instead of mkdir()
File file = new File(mydir.getAbsolutePath()+ File.separator + date);
if (file.mkdirs())
Log.d(context.getClass().getSimpleName()," date dir created");

Adding a folder to the Application folder

I've used application folder
data/data/com.xxx.xxx/databases/Customer.db
to store a database,it works fine and i could open and used it,but i wanted to add multiple folders to this path like
/data/data/com.xxx.xxx/databases/b36f6e58-0971-4f79-aca0-dada4201d886/Customer.db
but when i download the database and put it on the path and when i want to open it, it throws Exception that couldn't open the database.i have also try downloading the db ,check and makeDir the path then move db to the path but it doesn't solve the issue.
is there anything wrong with adding another folder in application folder or am i missing something? Any help would be appreciated.
Try this way:
File newDir = new File(getFilesDir(), "newDir");
if (!newDir.exists()) {
newDir.mkdirs();
}
File inside newDir can be accessed with openFileInput/openFileOutput. For both you need a context

android data save directories

My application is mostly c++ (using NDK) so I use fopen, fwrite, etc. standard functions to create and game save files and write into them.
When I use fopen("game.sav", "wb"), it appears that it's being created at path
/data/user/10/com.my.game/files/game.sav.
My app is multi-user. So I want to have a separated folders where users store their save-files. And instead of the path above I'd like to have paths like
/data/user/10/com.my.game/files/user0/game.sav,
/data/user/10/com.my.game/files/user1/game.sav, etc
My app's frontend is in Java, and when new user is being registered, I want to create a folder /data/user/10/com.my.game/files/user0/. But I don't know how to do it, because
final File newDir = context.getDir("user0", Context.MODE_PRIVATE);
results in path being created at /data/user/10/com.my.game/app_user0 that's a different path.
It is possible to create folders at /data/user/10/com.my.game/files/ and how ?
Simple way to do it, this code you can change it suit many conditions. If you know that your path is different from what getFilesDir() gets you then you can create a File first of all by using a path that you know and the last 2 lines of code will still be same.
File file = this.getFilesDir(); // this will get you internal directory path
Log.d("BLA BLA", file.getAbsolutePath());
File newfile = new File(file.getAbsolutePath() + "/foo"); // foo is the directory 2 create
newfile.mkdir();
And if you know the path to "files" directory:
File newfile2 = new File("/data/data/com.example.stackoverflow/files" + "/foo2");
newfile2.mkdir();
Both code works.
Proof of Working:

Rename a file in the internal storage

What's the best/easiest way to rename a file in the application's internal storage? I find it a bit strange that there is a Context.deleteFile() method, but no "move" or "rename" function. Do I have to go all the way through saving the file's contents, deleting it, creating a new one and then copying the contents into that one? Or is there a way to copy a file over an existing file?
Update (Aug. 30, 2012):
As per the suggested solution below, which I cannot get to work:
I have a file called shoppinglists.csv
Then I create a new file called shoppinglists.tmp, and copy the contents from shoppinglists.csv AND some new entries into that. The shoppinglist.tmp file is then a new version of the shoppinglists.csv file
Then I delete the old shoppinglists.csv file
Then I need to rename the shoppinglists.tmp file to shoppinglists.csv
I tried this:
ctx.deleteFile("shoppinglists.csv"); <--- delete the old file
File oldfile = new File("shoppinglists.tmp");
File newfile = new File("shoppinglists.csv");
oldfile.renameTo(newfile);
However, this doesn't work. After deleteFile(), nothing more happens, and I'm left with the new shoppinglists.tmp file.
What am I missing?
NB: There are no errors or anything in LogCat.
Instead of using a raw File constructor, use the method getFileStreamPath provided by the Context. That is to say, do:
File oldfile = ctx.getFileStreamPath("shoppinglists.tmp");
File newfile = ctx.getFileStreamPath("shoppinglists.csv");
oldfile.renameTo(newfile);
renameTO() doesn't work in my environment (Eclipse Indigo, AVD with android version 2.3). The solution is to skip the temporary file method alltogeher, since it doesn't seem to be possible to solve in any reasonable time frame.
I think we cannot use File.renameTo() method in Internal Storage environment.
Renaming a file in this environment, can do:
- Copy content of the old file to new file.
- Delete old file.
File file = new File("your old file/folder name");
File file2 = new File("your new file/folder name");
boolean success = file.renameTo(file2);

android-Include a database at the time of installation

In android, I'd like to include a database at the time of installation i.e. in the .apk file. Is it possible to do that? I have some data in a database that I would want to use in the app. I don't want to use OnCreate method to create a database. So, where should I keep my db file so that it is accessible to the app after installation?
create your database file, include it in your assets directory and on first launch of your application copy it to /data/data/PACKAGE_NAME/databases/
This website helped me out greatly.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
I create my SQLite databases separately, add them to the assets directory in the app package, and then on launch of the app copy the databases into the /data/data/com.company.appname/databases/ directory for use there.
I also check to see if the file already exists before bothering to copy it over (so I generally only copy it on first ever launch).
eg
boolean createDB= false;
File dbDir = new File(DB_PATH);
File dbFile = new File(DB_PATH + DB_NAME);
if(!dbDir.exists())
{
dbDir.mkdir();
createDB = true;
}
else if (!dbFile.exists())
{
createDB = true;
}

Categories

Resources