The database i want to import is over 5Mb. When i put the .db in asset folder i got some errors. So i split the database into smaller db files and assemble the pieces to create the database. So is there any way i can import the database from sdcard and start using the db without splitting and assembling?
Thanks in advance.
Yes, there is:
place the database file inside the sdcard then create a method inside a class, the method is like:
public class DB_Path {
public final SQLiteDatabase getOrders() {
File dbfile = new File("/sdcard/TheDataBaseFile");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
}
}
-----------------------
initialize like
public DB_Path dbp = new DB_Path();
public SQLiteDatabase db = dbp.getOrders();
after that you can call the cursor with db.
Cursor cur = db.rawQuery("the sql query",null);
Related
I am trying to insert a record in my database but it's not updating my database.
My insert method:
public void insert(String name, String status, String seat, String id, String pnr) {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_ID, id);
contentValues.put(DatabaseHelper.COL_NAME, name);
contentValues.put(DatabaseHelper.COL_PNR, pnr);
contentValues.put(DatabaseHelper.COL_STATUS, status);
contentValues.put(DatabaseHelper.COL_SEAT_NO, seat);
database.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
}
Code when inserting:
dbManager = new DBManager(getContext());
dbManager.open();
dbManager.insert("Name","XYZ","B1 21", "2","2348384");
open method:
public DBManager open() throws SQLException {
databaseHelper = new DatabaseHelper(context) {};
database = databaseHelper.getWritableDatabase();
return this;
}
I am using SQLite Asset Helper in my DatabaseHelper and the database is stored in assests/databases
Thank you in advance!
Assets folder is meant for read-only purposes. You cannot alter the contents at run time - except build/development time.
In your case, I would suggest you to copy the database file from assets to your app's private folder (or your desired location) on SD/external storage and then use that database file to perform read/write operations.
In short, your assets' database is good for read operations only.
Note: same rules applies to Raw resources too and they serve a slightly different purpose.
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);
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.
I put my sqlite database file in the "assets" folder And i write a DAO calss to get data from database,But the information from log.e means i can not open the database.
public class GetData {
private static String DB_PATH = "/data/data/com.SGMalls/databases/mallMapv2.sqlite";
private static SQLiteDatabase myDataBase;
public static ArrayList<Mall> getMall(){
ArrayList<Mall> mallArrayList=new ArrayList<Mall>();
String queryString="select id,title from malls order by title";
myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,SQLiteDatabase.OPEN_READONLY);
Cursor cursor=myDataBase.rawQuery(queryString, null);
if(cursor!=null){
cursor.moveToFirst();
while(!cursor.isLast()){
Mall mall=new Mall();
mall.setMallid(cursor.getInt(0));
mall.setMallname(cursor.getString(1));
mallArrayList.add(mall);
cursor.moveToNext();
} }
myDataBase.close();
return mallArrayList;
}}
The assets/ folder has nothing to do with databases, directly. If you put a database in the assets/ folder, you need to copy it from the assets/ folder to where you want the database to reside in the actual filesystem.
Have a look at this link .
You will have to call first createDataBase() method. If createDataBase() runs successfully, you can check your /data/data/com.SGMalls/databases/mallMapv2.sqlite is really present.
If it exists already, it won't do any harm to it.
copyDataBase() should give you some explanations about how it does to copy from assets to ../databases/..