Not able to access sqlite database Android - 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.

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.

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());
}
}

problem in copying data from assets to application

For my application i need to read data from my own data base. I put my db in "ASSETS" folder and using following code to copy into the path
DB_PATH = "/data/data/com.android.example/databases/"
But the table is not getting created.And its throwing exception and forced close.
private void copyDataBase() throws IOException{
//Open your local db as the input stream
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();
}
In above code "buffer" is not getting data . so its not writing data to myOutput. I found this code in net and modified . could any body help me to read the file in assets folder to SD card.I need to store data in SD card from external file and read from it.
Check you database file size, there is a limitation around 1.2 Mb for files in the "assets" folder.
One way around it is to use the Unix "split" command, add the split files in the res/raw folder, and then splice them back together into the db by altering the code you have slightly.
Read this blog (http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/) for more information.
You must put data not in ASSETS folder but into assets - please double check!

How to use existing/Predefined table in 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();
}

Categories

Resources