you will achieve this task y using the following way.
- First create your database by using SQlite Manager/ SQlite Studio.
- store that DB in Asserts folder and use the following code
public class DataBase extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/your package name here/";
public static String DB_NAME = "your database name .sqlite";
public static SQLiteDatabase _database;
private final Context myContext;
public static String apstorphe = "'";
public static String sep = ",";
private AtomicInteger mOpenCounter = new AtomicInteger();
public DataBase(Context context) throws IOException {
super(context, DB_NAME, null, 1);
this.myContext = context;
createDataBase();
}
/**
* Creates a empty database on the system and rewrites it with your own database.
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
// By calling this method and empty database will be created into the default system path
// of your application so we are gonna be able to overwrite that database with our database.
try {
copyDataBase();
} catch (IOException e) {
System.out.println();
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
public 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[2048];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public static SQLiteDatabase openDataBase() throws SQLException {
// Open the database
if (_database == null) {
_database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
} else if (!_database.isOpen()) {
_database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}
return _database;
}
public static void closedatabase() {
if (_database != null)
_database.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static Cursor getdata(String query_str) {
Cursor mCursor = _database.rawQuery(query_str, null);
return mCursor;
}
public static void executeQuery(String strQuery) {
try {
_database.execSQL(strQuery);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}