Create Database from assets folder
suggest changePut your dbname.sqlite or dbname.db file in assets folder of your project.
public class Databasehelper extends SQLiteOpenHelper {
public static final String TAG = Databasehelper.class.getSimpleName();
public static int flag;
// Exact name of the file in assets, including the extension.
static String DB_NAME = "dbname.sqlite";
private final Context myContext;
String outFileName = "";
private String DB_PATH;
private SQLiteDatabase db;
public Databasehelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
// getDatabasePath is where SQLiteOpenHelper opens the file.
// getFilesDir()/databases is a different directory.
DB_PATH = context.getDatabasePath(DB_NAME).getParent() + "/";
Log.e(TAG, "Databasehelper: DB_PATH " + DB_PATH);
outFileName = context.getDatabasePath(DB_NAME).getAbsolutePath();
File file = new File(DB_PATH);
Log.e(TAG, "Databasehelper: " + file.exists());
if (!file.exists()) {
file.mkdir();
}
}
/**
* Creates an empty database, then replaces that file with the packaged one.
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (!dbExist) {
// Create the empty file in the real databases directory, then close
// it so the copy can replace the file.
this.getReadableDatabase();
this.close();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exists to avoid copying the file on every open.
*
* @return true if it exists, false if it does not
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(outFileName, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// Missing file. createDataBase copies it. Do not copy here too.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This copies the asset bytes onto that file.
*/
private void copyDataBase() throws IOException {
Log.i("Database",
"New database is being copied to device!");
byte[] buffer = new byte[1024];
OutputStream myOutput = null;
int length;
// Open your local db as the input stream
InputStream myInput = null;
try {
myInput = myContext.getAssets().open(DB_NAME);
// transfer bytes from the inputfile to the
// outputfile
myOutput = new FileOutputStream(outFileName);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
Log.i("Database",
"New database has been copied to device!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void openDataBase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
Log.e(TAG, "openDataBase: Open " + db.isOpen());
}
@Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
getId is not defined on this helper. Open the copied database and run the query on db from openDataBase.
// Create Databasehelper class object in your activity.
private Databasehelper db;
In onCreate, create the helper and call createDataBase():
db = new Databasehelper(MainActivity.this);
try {
db.createDataBase();
} catch (Exception e) {
e.printStackTrace();
}
Perform all of your insert, update, delete and select operation as shown below.
String query = "select Max(Id) as Id from " + TABLE_NAME;
db.openDataBase();
int count = db.getId(query);
db.close();
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents