Reputation: 430
I am working on an APP which require database, I have created database and it accepts the query, creates the database, and I am able to do all operations. following is the database which works:
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "sandeep.db";
// Table name
private static final String TABLE_C = "table_data";
// Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_C + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
}
The database gets created in default location /data/dataapp_name/ but I cannot access it due to unrooted phone.
my Problem:
1. how to create sandeep.db on sdcard or on phone memory which is publicly accessible?
2. Can I create a Whatsapp like folder and have my database reside in that?
Upvotes: 2
Views: 1711
Reputation: 2604
Use this method named SQLiteDatabase.openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory)
.
It will allow you to create database on sdcard.
You need to add following permission in your AndroidManifest.xml ,
android.permission.WRITE_EXTERNAL_STORAGE
Upvotes: 2