android SQLite database with databse on specific location possible?

↘锁芯ラ 提交于 2019-12-07 22:37:51

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/20948711/android-sqlite-database-with-databse-on-specific-location-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!