我在access文件夹中有一个sqlite数据库。我读到它并在我的应用程序中更新字段。但是,当我关闭应用程序并再次启动它时,对字段所做的更改将是原始值。
当应用启动时,似乎数据库被覆盖了。有没有办法保存数据,并打开新数据库?我在这里做错了什么?
// My understanding is:
// if no-Database created (first use)
// CreateDatabase()
// openDatabase() (2nd use and more) and this is not updated
public class DataBaseHelper extends SQLiteOpenHelper {
public static DataBaseHelper getInstance(Context context)
{
if(mDBHelper == null)
{
mDBHelper = new DataBaseHelper(context);
}
return mDBHelper;
}
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
//String packageName = context.getPackageName();
if(android.os.Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}else{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//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.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e("CreateDatabase", "Error copying database " + e.toString());
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
Log.v("CheckDatabase", "Database does not exist yet");
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = null;
try {
myInput = myContext.getAssets().open(DB_NAME);
} catch (IOException e) {
e.printStackTrace();
}
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = null;
try {
myOutput = new FileOutputStream(outFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
try {
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);//OPEN_READONLY);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public int updateWeight(int id)
{
W w = getW(id);
int newWeight = w.getWeight()+1;
ContentValues args = new ContentValues();
args.put(col_one, newWeight);
args.put(col_two, word.getTwo());
args.put(col_three, word.getThree());
SQLiteDatabase db = this.getWritableDatabase();
Log.v("update Weight", "Weight = " + newWeight);
return db.update(TABLE_NAME, args, col_id + "=?",
new String[]{String.valueOf(id)});
}
}
用法: 在onCreate
private DataBaseHelper myDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
_gotit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myDbHelper.updateWeight(whereAmI);
}
});
}
答案 0 :(得分:0)
已更改
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.READ_ONLY);
在CheckDataBase中:到:
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
现在可以使用了。谢谢。