Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用对自定义sqlite数据库的更新保存更改_Android_Sqlite_Sql Update - Fatal编程技术网

Android 如何使用对自定义sqlite数据库的更新保存更改

Android 如何使用对自定义sqlite数据库的更新保存更改,android,sqlite,sql-update,Android,Sqlite,Sql Update,我在access文件夹中有一个sqlite数据库。我在我的应用程序中阅读并更新字段。但当我关闭应用程序并再次启动它时,对字段所做的更改就是原始值 当应用程序启动时,数据库似乎被覆盖。是否有办法保存数据并打开新数据库?我做错了什么 // My understanding is: // if no-Database created (first use) // CreateDatabase() // openDatabase() (2nd use and more) a

我在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)});
    }
}
用法: 立即创建

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);

            }
        });

}
改变

checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.READ_ONLY);
在CheckDataBase中,执行以下操作:

checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);

现在可以了。谢谢。

当应用程序启动时,数据库似乎被覆盖了,所以为什么不先检查一下呢。。。在正确的位置发布一些日志语句,并检查它是否出现在logcat中。。。也许你的更新代码不起作用了……更新功能运行良好,数据库似乎无法保存。启动时,CheckDatabase logs数据库尚不存在。这意味着它没有被保存?那么我需要做什么呢?你不会相信的,但我不得不将READ\u ONLY更改为NO\u本地化\u COLLATORS