Reputation: 39
I am trying to DL a .sqlite file in my android app, store it and then use it. If I understood correctly I first need to DL the db from an url, put it in the assets folder and then copy it to the : "/data/data/{package_name}/databases/" folder. I found this answer helping me to copy the DB from the assets directory but I still can't download the DB at first. Simple export and import of a SQLite database on Android
I am fairly new in android dev, if someone know a step by step tutorial or an open source app doing that it would be really great.
Upvotes: 1
Views: 2240
Reputation: 11
To manually create database, use Contract Android class.Use SQLiteBrowser Download .sqlite file using Monitor in the Android Studio to get the data.
Upvotes: 1
Reputation: 2032
First: Create a folder have name: assets in app/assets
Then: Copy db.sqlite to folder assets
public class SqliteHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/package.your.name/databases/";
private static String DB_NAME = "your_name_db.sqlite";
private final Context context;
public SqliteHelper(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
}
/**
* copy database from assets to the device if not existed
**/
public boolean isCreatedDatabase() throws IOException {
// Default là đã có DB
boolean result = true;
// Nếu chưa tồn tại DB thì copy từ Asses vào Data
if (!checkExistDataBase()) {
this.getReadableDatabase();
try {
copyDataBase();
result = false;
} catch (Exception e) {
throw new Error("Error copying database");
}
// nó chưa tồn tại thì file sharedpreferences cũng chưa tồn tại tạo luôn
//isCreateSP();
}
return result;
}
/**
* check database exist on the device?
*/
private boolean checkExistDataBase() {
try {
String myPath = DB_PATH + DB_NAME;
File fileDB = new File(myPath);
if (fileDB.exists()) {
return true;
} else
return false;
} catch (Exception e) {
return false;
}
}
/**
* copy database from assets folder to the device
*
* @throws IOException
*/
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
OutputStream myOutput = new FileOutputStream(DB_PATH + DB_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
isCreateDB();
}
/*
* Copy db from asset vào database
*/
private boolean isCreateDB() {
SqliteHelper data = new SqliteHelper(this);
try {
return data.isCreatedDatabase();
} catch (IOException e) {
Toast.makeText(this, "Error copying database", Toast.LENGTH_LONG).show();
e.printStackTrace();
return false;
}
}
Upvotes: 0