I have a normal opening of a database file, it works perfectly fine when opening a blank db file with only the metadata table, but as soon as I make another table, it causes the app to crash. Maybe it's different version db files? Please comment if you need more of the code
private static String DB_PATH = "/data/data/com.example.andrew.ubair4/databases/";
private static String DB_NAME = "coordinates";
String myPath = DB_PATH + DB_NAME;
private SQLiteDatabase db;
public DataBaseHelper(Context context){
super(context, DB_NAME, null, 1);
this.myContext = context;
Log.d("TAGG","enter constructor");
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); //<---- crashes right here IF I have a second table in the database
Log.d("TAGG","2");
My metadata table:
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
INSERT INTO "android_metadata" VALUES ('en_US')
Add 1 more table:
CREATE TABLE something (
column1,
column2,
column3,
PRIMARY KEY (column1)
);
Edit: Okay, I have new information. If I so much as take the db file, email it to myself, and replace it back into the original directory. It'll crash. Yes you heard me. I don't even alter the file, I just email the file to myself, delete the original, and replace it with the exact same file. Then it crashes. I'm about to tear my hair out.
As I can see, you are opening the DB in read-only mode. Adding the table - is a modification, and exception thrown shows you that you should open it in write mode before modifying it scheme or adding\deleting some data.
Related
I want to edit an existing .db file in SD card directly. According to this, using the following code, it is possible to copy that database to /data/data/PACKAGE_NAME/databases/ and then edit it, but I want to edit it in the SD card itself.
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);
Is it possible?
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
With this, you can read/write to your app's internal storage... be that a DB file or not.
OR.... you CAN open/read the data from the internal DB, and then systematically copy it over into a new DB on the external storage.
This is tedious, but it gives you the opportunity to 'scrub' your DB of any 'private' info that you might not want to share.
and "tedious" isn't "quite" true, you just need to create each table again, then read each table and insert that data into the new DB.
thanks to friends ... this is the answer :
first u should define the database file like this :
SQLiteDatabase myDB;
then, for example for inserting :
myDB = SQLiteDatabase.openDatabase("/sdcard/mydb.db", null, SQLiteDatabase.OPEN_READWRITE);
ContentValues cv1 = new ContentValues();
cv1.put("Field1", "new Field");
cv1.put("Field2", "other Field");
myDB.insert("TABLE_NAME", null, cv1);
or for update use this :
myDB.update("TABLE_NAME", cv1, "_id=" + 1, null);
I have developed an android app which will run just on one device,and i have an SQLite DB and i want to copy that in //data/data//databases directory on device internal storage,but i have two problems:
This directory for my app is not visible for me, while files of some other apps are visible.
I created this directory manually and copied the DB there, but it didn't work.
In emulator i copied db to the files that was created automatically and app works properly.
thanks for your helps
and this is my sqlite code:
SQLiteDatabase db=openOrCreateDatabase("a",MODE_PRIVATE, null);
Cursor c=db.rawQuery("SELECT * FROM person WHERE id=?", new String [] {String.valueOf(1)});
c.moveToFirst();
String name=c.getString(c.getColumnIndex("name"));
db.close();
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(name);
You have to copy your SQLite database in assets folder, which is inside the main project folder. You cannot place it directly inside the /data/data/your application package/.
Follow this tutorial to get going on this.
There is a line in code,
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/";
replace this with
private static String DB_PATH;
and
update the constructor to
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
// you will get a warning if you'll try to hardcode your path
DB_PATH = myContext.getDatabasePath(DB_NAME).getPath();
}
rest is all explained in this tutorial.
Im trying to do a simple example using my own SQLite database, android and greenDAO generator to generate classes for my android app.
My database file is defined this way:
1) Create a database called "OneTableDB" (without extension, SQLite 3) with the following structure:
Entity: Professor
professorID: primarykey
name: text
age: int
Entity: android_metadata
locale: text
Then i populated android_metadata with the value 'en_US', and the entity with few rows.
2) placed on my Android app structure inside: proj_root/databases/
Full path to database file: proj_root/databases/OneTableDB
3)i have a method to check whether database exists or not (in my case, it has to exist, since i placed inside databases folder)
private boolean databaseExists() {
SQLiteDatabase sqliteDatabase = null;
try {
String databasePath = DB_PATH + DB_NAME;
File f = new File(DB_PATH + DB_NAME);
f.exists();
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (sqliteDatabase != null) {
sqliteDatabase.close();
}
return sqliteDatabase != null ? true : false;
}
//DB_PATH = /data/data/com.myapp.android_dao_tests/databases/
//DB_NAME = OneTableDB
debugging on f.exists(), it returns false value and then breaks on
sqliteDatabase = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READONLY);
During the debugging i used adb shell to check if the path was right, and in fact i can navigate to /data/data/com.myapp.android_dao_tests/ and there is no databases folder!
Any idea how can i solve this problem?
Thanks in advance?
The DB "template" is saved in the assets/ folder, in order for it to be bundled in the apk. The code then copies the DB from assets/ to databases/ to make it accessible as a regular SQLite DB.
After further investigation, it seems like Android refuses to acknowledge the new DB as its own. Apparently, the built-in DB mechanism wasn't meant to be used this way.
The correct way to approach it is by keeping the data in textual format in assets/ so that if the app starts and finds there's no DB, it will create the schema itself, and populate it with the data in the text files from the assets/ folder.
Can I talk to the database of another application from inside my code?
Only, If that database is not a private to that application. (Also if your device is rooted then you can access any application's database) Also if that database is like a content Provider then you can access other application's database in your application. Like Android native phonebook database. (As it used their database as a content Provider.)
No, You can not.
same question
But you can try it by rooting the phone
link >> link
Reading DB from SDCARD>>
see the location of DB file then create a method inside a class, the method is like:
public class DB_Path {
public final SQLiteDatabase getDB() {
File dbfile = new File("path of file like : /sdcard/TheDataBaseFile");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
}
}
initialize like
public DB_Path dbp = new DB_Path();
public SQLiteDatabase db = dbp.getDB();
after that you can call the cursor with db.
Cursor cur = db.rawQuery("the sql query",null);
Reference link >> link
How to get table names from database >> link
I create my database using sqliteadmin (version 0.8.3.2),I place this file into my asset directory and then copy this file into data/data/mypackage/databases/mydb,its ok.now when I am trying to open this file getting exception as android.database.sqlite.SQLiteException: unable to open database file,below code i am using to open the mydb.
private static final String DB_PATH = "/data/data/src.com/databases/";
private static final String DB_NAME = "mydb";
String mypath = DB_PATH + DB_NAME;
try{
dbBF = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}catch(Exception ex)
{System.out.print("H![enter image description here][1]ere is an Exception"+ex);
}
Cursor cur = dbBF.rawQuery("SELECT * FROM"+"myTable" , null);
Your approach seems right. Do you have extension on the your database file? Like mydb.db. If you do than you should add it complete name of the file.
I had the same problem. First please note that your database file should not be bigger than 1.2MB, and if it's, then try to split it. Second, instead of the following lines,
DBAdapter db = new DBAdapter(this);
db.openDataBase(); //Bad! db not created yet!
try to use
DBAdapter db = new DBAdapter(this);
db.createDataBase(); //needs exception handling
db.openDataBase();
I copied it from How does android access a sqlite database included in the assets folder . Anyway, it works for me. In case of any problem, let me know.