Sqlite Issues with OnePlus device - android

We have an android app that uses SQLite for some form of persistent storage. The sqlite code works on all devices except Oneplus devices. ( I am testing on Oneplus 2 A2003)
Following is the error that I am seeing in logcat.
android.database.sqlite.SQLiteException: no such table: testtable
(code 1): , while compiling: INSERT INTO
testtable(ID,CompletedOn,CreatedOn,Type,Pending,Priority,Attributes)
VALUES (?,?,?,?,?,?,?)
Following is the piece of database that is used for open database
SQLiteDatabase db = SQLiteDatabase.openDatabase(this.getHelperContext().getDatabasePath(DATABASE_NAME).getAbsolutePath(), null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS);
Have tried even specifying the access rights while opening, but no difference.
e.g. SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE
Any help would be appreciated.

Check this answer https://stackoverflow.com/a/33032467/4504324 it solved the issue for me.
Just close the db before copying the sqlite stream into internal database.
private void copyDataBase() throws IOException {
try {
//Open your local db as the input stream
InputStream myInput = mContext.getAssets().open("databases/" + DB_NAME+".sqlite");
// Path to the just created empty db
String outFileName = mContext.getDatabasePath(DB_NAME).getAbsolutePath();
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//close the database so that stream can be copied
this.getReadableDatabase().close();
//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) {
e.printStackTrace();
}
}

Related

how to decrypt sqlite database android using sqlcipher?

I need a local database for my application.I created one and i encrypted it.(I know the pw).Now i want to load this db to my original application's assets folder.I want to decrypt it before copy.
I had a copy code like this this is working for unencrpyted db.
How can I translate this for my encrypted db.Thanks
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[5120];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
The proper way to do this would be to copy the encrypted database into a local folder. Then you can open it with SQLCipher and use sqlcipher_export() to convert it into a standard (non-encrypted) SQLite database.

android database copy error through programming

I have a strange problem I am trying to copy database from asset folder to app database folder "/data/data/YOUR_PACKAGE/databases/" by the below code. this is working fine for Samsung mobile not working for gionee mobile I don't know why please help
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();
}
How do you generate the DB_PATH? Maybe it is not available on the gionee mobile device (because the manufacturer modded the OS).
You should use the code below to determine outFileName:
String outFileName = context.getDatabasePath(DB_NAME).getPath();
If that still throws an exception, please update your post with more details.

Access SqlLite Database which is shipped with application with SqlLite Database Browser

I have copied database in Android Project folder and It is working fine. Now, I am inserting values in table. So I want to know that Is there anyway by which I can see those data using SqlLite Database Browser ???
Database in assets folder, I opened it but I can't see any new data.
My Code to insert data:, It is working fine, no errors.
public boolean SaveUserResponse(String QId, String OptionId,
String ResponseDate) {
try {
ContentValues cv = new ContentValues();
cv.put("QId", QId);
cv.put("OptionId", OptionId);
cv.put("ResponseDate", ResponseDate);
mDb.insert("tblUserResponse", null, cv);
Log.d("SaveUserResponse", "User Response has been Saved.");
return true;
} catch (Exception ex) {
Log.d("SaveUserResponse", ex.toString());
return false;
}
You can fetch your DB from device using DDMS (can be found under android-sdk\tools). There open Device -> File Explorer. Then navigate to data\data\your_app and pull your DB to a disk.
BTW, your DB shouldn't be placed under assets, here is a code how to copy it from assets (you can call it on the first run of your app):
void copyDataBase() throws IOException {
String DB_PATH = "/data/data/<app>/databases/";
String DB_NAME = "db.sqlite";
//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();
}

Copy two SQLite Database in asset folder in Android

For the last few days I've been trying to copy two databases in the asset folder, but could not get any success.
One database I managed to copy and access it. But the second one, I need your help.
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("/data/data/(packagename)/databases /(datbasename).sqlite");
// transfer byte to inputfile to 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();
}
Do this for your second database.

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