How to use existing/Predefined table in android? - android

Hi everyone i have one question i.e. i have some database table's which are already exist in database but i am not able to use them in my application. I am able to connect to database but i am not getting the tables which i have already created. I am not creating tables programmectically but i want to use existing table in my database. If i am creating tables programmetically then it's fine but how can i use existing table.
If anyone is having any idea plz suggest me some solution.

Answer to my question is simpelly copy your database by using the followig code.
private void CopyDataBase() throws IOException {
// open the local database
InputStream copy = context.getAssets().open(UR_DB_NAME);
// path where database is created
String path_DB = DB_PATH + DB_NAME;
// Open the empty dbOut as the output stream
OutputStream dbOut = new FileOutputStream(path_DB);
// copy database from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = copy.read(buffer)) > 0) {
dbOut.write(buffer, 0, length);
}
// Close the streams
dbOut.flush();
dbOut.close();
copy.close();
}

Related

Android - SQLite Manager is hidden in eclipse

I am developing a simple database application.
I want to see the inserted data. I have follow this instructions to view the data.
But in eclipse the SQLite manage is hidden. Please let me any idea to show view the data.
Open view Window->Show View->File Explorer.
In this view go to data/data/"your app name"/databases/"your database" This is you database file.
For me, I use SQLite Database Browser.
Here, you can finf a good tutorial to know how to use it exactly.
Open the DDMS perspective and select the emulator, open the path: /data/data/your.app.name/databases/your.db and click the db file.
The icon will be then enabled
[UPDATE]
I noticed you are using ALL CAPS file names (...).
But I guess the plugin is case sensitive.
Try to make (at least) the extension SMALL CAPS: .db
Suppose If you have phone then you can copy the database to sdCard
private static String DB_NAME = "db_name";
private String DB_PATH = Environment.getDataDirectory().getAbsolutePath()
+ "/data/package_name/databases/";
private void copyToSdCard() throws IOException {
byte[] buffer = new byte[1024];
int length;
String outFileName = "/sdcard/file_name.sqlite";
OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput;
myInput = new FileInputStream(DB_PATH + DB_NAME);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
Then using mozila firefox add the add-on sqlitemanager and you can check your db. please replace the package name and db name.

Not able to access sqlite database Android

Ive created an app that stores user login information into an sqlite database. However when i try to access it using Root Explorer i get an error.
An error occurred while opening the database. disk i/o error (code 3850):, while compiling: Select * from sqlite_master where type in('table','view') order by name.
The app works fine, and it is storing the log in information as required. However i cant access it on the root explorer?
Any suggestions?
please call this function:
public static void copyDataBase(Context mActivity) throws IOException {
InputStream myInput = new FileInputStream(new File("/data/data/"
+ mActivity.getPackageName() + "/databases/" + "yourdb.sqlite"));
File files = new File("/sdcard/files/");
files.mkdirs();
String outFileName = "/sdcard/files/your.sqlite";
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int bufferLength;
while ((bufferLength = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, bufferLength);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
your database is copied /sdcard/files/your.sqlite this path.
You could add a button in your app to view the database information or write the database information to a file in external storage.
Otherwise, maybe you need to root your device to access the database.
Just give permissions to all users to the file [YOUR_DB].db-journal
For me it worked.

Android restore backup from database file on sdcard

There are a few questions here on stackoverflow where people use BackupAgents to synchronize the apps data with googles cloud (see here). In my specific case the requirements are much more restrictive due to the nature of the more or less sensitive data. Everything must only be stored on the device itself and the app must not connect to the internet.
The main Activity contains a ViewPager which hosts a few ListFragments. Each ListFragment has its own Loader which swaps the cursor in a SimpleCursorAdapter class.
Before both backing up and restoring the database I destroy the loaders with:
getLoaderManager().destroy(LOADER_ID);
My backup function is similar to this anwser:
final String inFileName = "/data/data/<your.app.package>/databases/foo.db";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/database_copy.db";
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
The restore function is similar in that it copies the database from the sdcard back into the internal app folder, stops the loaders and overwrites the database file.
public static void restore(Context context) throws IOException {
if (externalStorageIsWriteable()) {
// getBackupDir() is a path to the folder on the sdcard
File fileBackup = new File(getBackupDir(), WineSQLiteHelper.DB_NAME);
if (!fileBackup.exists()) {
throw new IOException("File not found");
}
File importFile = getImportDatabase();
try {
FileUtils.copyFile(fileBackup, importFile);
MySQLiteHelper dbHelper = new MySQLiteHelper(context);
dbHelper.close();
dbHelper = null;
File file = new File(Environment.getDataDirectory() + "/data/"
+ PACKAGE + "/databases/" + WineSQLiteHelper.DB_NAME);
FileUtils.copyFile(importFile, file);
// Remove temporary import file.
importFile.delete();
} catch (IOException e) {
throw e;
}
} else {
throw new IOException("External Storage not writeable");
}
}
But somehow the MainActivity gets recreated after I've overwritten the database file and I recieve a few
SQLiteException: no such column
My guess is that perhaps there are still open connections to the database, but I'm not sure about that since this is the first time I have to work with databases this closely.
How to properly close all database connections of a ContentProvider? I can't find anything in the documentation about this. And is this necessary?
How do I properly restore the database?

Saving sqlite data onto phone

I am currently trying to access my database using my android phone however it doesn't work. It does work on the emulator. So I was wondering if I need to copy the data into my phone's internal memory by using
try{
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}catch(SQLiteException e){
Another issue is that I did not use "SQLiteOpenHelper", so does the above method still work? I simply did a
private final String DB_NAME = "MemberData";
private final String TABLE_NAME = "MemberDB";
sampleDB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
Cursor c = sampleDB.rawQuery("SELECT companyNameEng FROM " +
TABLE_NAME + " ORDER BY companyNameEng ASC", null);
Yes, you must copy the db file out of the apk and into your apps database directory (or somewhere else that your app can access). The assets directory is not really a directory when the .apk file is created... it's just data that certain functions in the framework (getAssets()) know how to access. To allow other functions that expect a real path to work, you need to copy the data out to a proper directory on the device.
The usual destination for a database is "data/data/your.package.name/databases/".
Your method looks like will work fine... it is standard java io. SQLiteOpenHelper is more for helping you do upgrades and create new dbs. I always use one (never know if I'm going to change how I do things even if I don't need it initially), but you certainly don't have to.

Not empty LiteSQL DB at start

I think that this is rather easy question. I am too young in android stuff already. I want to prepare application which will be using database. In every example I've shown, there is an empty database where application is firstly started and after that there are some inserts. I want to have app with rather big db so I want to have filled db when app is started. How can I prepare db and attach it to program?
put your filled database in Package's Asset directory,
at application runtime just copy that database to application's internal storage like
data/data/<package name>/database directory.
then use it.
EDIT: this for copy database from asset directory to database directory,
private void copyDataBase() throws IOException {
try {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open("your Database file name");
// Path to the just created empty db
String outFileName = "/data/data/<package name>/databases/";
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("error", e.toString());
}
}

Categories

Resources