|
|
|
Android เราเราสามารถ Export โดย แนบไฟล์ SQLite ไปกับ *.APK ได้ไหมครับช่วยแนะนำหน่อยครับ |
|
|
|
|
|
|
|
ใช้การ Copy ได้ครับ
Code (Android-Java)
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DB_PATH = "/data/data/com.project.mydb/databases/";
private static final String DB_NAME = "mydb.db";
private static final String DB_TABLE = "words";
private static final int DB_VERSION = 6;
private static final String TAG = "DataBaseHelper";
int id = 0;
Random random = new Random();
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db){
createDB();
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
Log.w(TAG, "Upgrading DB from version " + oldVersion + " to " +
newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
onCreate(db);
}
public void createDataBase(){
createDB();
}
private void createDB(){
boolean dbExist = dbExists();
if(!dbExist){
copyDataBase();
}
else if(dbExist){
copyDataBase();
}
}
private boolean dbExists(){
SQLiteDatabase db = null;
try{
String dbPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(DB_VERSION);
}
catch(SQLiteException e){
Log.e("SQL Helper", "database not found");
}
if(db != null){
db.close();
}
return db != null ? true : false;
}
private void copyDataBase(){
InputStream iStream = null;
OutputStream oStream = null;
String outFilePath = DB_PATH + DB_NAME;
try{
iStream = myContext.getAssets().open(DB_NAME);
oStream = new FileOutputStream(outFilePath);
byte[] buffer = new byte[1024];
int length;
while((length = iStream.read(buffer))>0){
oStream.write(buffer,0,length);
}
oStream.flush();
oStream.close();
iStream.close();
}
catch(IOException ioe){
throw new Error("Problem copying database from resource file.");
}
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close(){
if (myDataBase != null)
myDataBase.close();
super.close();
}
}
http://stackoverflow.com/questions/4189956/updating-prepopulated-database-in-android
|
|
|
|
|
Date :
2013-05-10 10:09:21 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
ต้องขอเวลาทำความเข้าใจซักพัก
|
|
|
|
|
Date :
2013-05-10 23:32:07 |
By :
ababydroid |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูครับ ได้ยังไงลองเอามาแชร์กันดูครับ
|
|
|
|
|
Date :
2013-05-11 21:01:21 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|