Android sqlite数据库未获得更新

Android sqlite数据库未获得更新,android,sqlite,android-sqlite,sqliteopenhelper,Android,Sqlite,Android Sqlite,Sqliteopenhelper,我将通过以下代码更新我的数据库: 这就是我做这件事的方式。我不知道当我要连接到db时是否必须调用create database方法: public int addDBfav(int id) { SQLiteDatabase sqLiteDatabase; DbHelper myDbHelper=new DbHelper(this); try{ myDbHelper.createDatabase();

我将通过以下代码更新我的数据库:

这就是我做这件事的方式。我不知道当我要连接到db时是否必须调用create database方法:

 public int addDBfav(int id)
    {
        SQLiteDatabase sqLiteDatabase;
        DbHelper myDbHelper=new DbHelper(this);
        try{
            myDbHelper.createDatabase();
        }catch (IOException e)
        {
            throw new Error("unable to create database");
    }
    myDbHelper.openDataBase();
    sqLiteDatabase=myDbHelper.getReadableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put("fav",true);
    int part= sqLiteDatabase.update("food",contentValues,"food_id = "+Integer.toString(id),null);
        Log.i("success","successfuly done "+Integer.toString(part));
        return part;
    }
这是我的db助手:

package com.example.android.dezcook;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Android on 6/10/2016.
 */
public class DbHelper  extends SQLiteOpenHelper {

    public static final String DBNAME = "database.sqlite";
    private static String DB_PATH;
    private SQLiteDatabase myDataBase;
    private final Context mycontext;

    public DbHelper(Context context) {
        super(context, DBNAME, null, 1);
        this.mycontext = context;
        DB_PATH = context.getFilesDir().getParentFile().getPath() + "/databases/";

    }


    public void createDatabase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {//database is exist
        }
        else{
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error Copying Database");
            }
        }
    }



    private boolean checkDataBase() {
        SQLiteDatabase DBcheck=null;
        String myPath=DB_PATH+DBNAME;
        try
        {
            SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
        }catch (SQLException e){}
        if(DBcheck!=null)
        {
            DBcheck.close();
        }
        return DBcheck!=null;
    }
    private void copyDataBase() throws IOException
    {
        InputStream myInput=
                mycontext.getAssets().open(DBNAME);
        String outFileName=DB_PATH+DBNAME;
        OutputStream myOutPut=new FileOutputStream(outFileName);
        byte[] buffer=new byte[1024];
        int length;
        while ((length=myInput.read(buffer))>0)
        {
            myOutPut.write(buffer,0,length);
        }
        myOutPut.flush();
        myOutPut.close();
        myInput.close();
    }
    public void openDataBase() throws SQLException
    {
        String myPath=DB_PATH+DBNAME;
        myDataBase=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READONLY);
    }
    public synchronized void close()
    {
        if(myDataBase!=null)
            myDataBase.close();
        super.close();
    }
    // It closes the database if it exists, and then calls super.close()
    // which calls close() in the parent class. It is synchronized which means it cannot run in parallel,
    // calls to that method are queud up to avoid corruption of database

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
这是我的美食课

public class Food implements Parcelable {
    protected int txtid;
    protected String txtName;
    protected String txtDesc;
    protected String txtImage;
    protected boolean txtFav;
    protected String items;
    protected int converted_image;
    public int getTxtid() {
        return txtid;
    }
    public void setTxtid(int txtid) {
        this.txtid = txtid;
    }
    public String getTxtName() {
        return txtName;
    }
    public void setTxtName(String txtName) {
        this.txtName = txtName;
    }
    public String getTxtDesc() {
        return txtDesc;
    }
    public void setTxtDesc(String txtDesc) {
        this.txtDesc = txtDesc;
    }
    public String getTxtImage() {
        return txtImage;
    }
    public void setTxtImage(String txtImage) {
        this.txtImage = txtImage;
    }
    public boolean isTxtFav() {
        return txtFav;
    }
    public void setTxtFav(boolean txtFav) {
        this.txtFav = txtFav;
    }
    public String getItems() {
        return items;
    }
    public void setItems(String items) {
        this.items = items;
    }
    public int getConverted_image() {
        return converted_image;
    }
    public void setConverted_image(int converted_image) {
        this.converted_image = converted_image;
    }
protected Food(){}
    protected Food(Parcel in) {
        txtid = in.readInt();
        txtName = in.readString();
        txtDesc = in.readString();
        txtImage = in.readString();
        txtFav = in.readByte() != 0;
        items = in.readString();
        converted_image = in.readInt();
    }
    public static final Creator<Food> CREATOR = new Creator<Food>() {
        @Override
        public Food createFromParcel(Parcel in) {
            return new Food(in);
        }

        @Override
        public Food[] newArray(int size) {
            return new Food[size];
        }
    };
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(txtid);
        dest.writeString(txtName);
        dest.writeString(txtDesc);
        dest.writeString(txtImage);
        dest.writeByte((byte) (txtFav ? 1 : 0));
        dest.writeString(items);
        dest.writeInt(converted_image);
    }
}
公共级食品工具可包裹{
受保护的int-txtid;
受保护字符串txtName;
受保护字符串txtDesc;
受保护的字符串txtImage;
受保护布尔txtFav;
受保护的字符串项目;
受保护的int转换图像;
public int getTxtid(){
返回txtid;
}
公共无效setTxtid(int-txtid){
this.txtid=txtid;
}
公共字符串getTxtName(){
返回txtName;
}
公共void setTxtName(字符串txtName){
this.txtName=txtName;
}
公共字符串getTxtDesc(){
返回txtDesc;
}
公共void setTxtDesc(字符串txtDesc){
this.txtDesc=txtDesc;
}
公共字符串getTxtImage(){
返回txtImage;
}
公共void setTxtImage(字符串txtImage){
this.txtImage=txtImage;
}
公共布尔值isTxtFav(){
返回txtFav;
}
公共无效设置txtFav(布尔值txtFav){
this.txtFav=txtFav;
}
公共字符串getItems(){
退货项目;
}
公共void集合项(字符串项){
这个项目=项目;
}
public int getu image(){
返回转换后的图像;
}
public void setConverted_image(int converted_image){
this.converted_image=转换的_image;
}
受保护食物({}
受保护食品(包裹内){
txtid=in.readInt();
txtName=in.readString();
txtDesc=in.readString();
txtImage=in.readString();
txtFav=in.readByte()!=0;
items=in.readString();
已转换的_image=in.readInt();
}
公共静态最终创建者=新创建者(){
@凌驾
公共食品包裹(包裹内){
归还新食物(在);
}
@凌驾
公共食品[]新数组(整数大小){
返回新食物[尺寸];
}
};
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入(txtid);
目的地书面记录(txtName);
目的地记录(txtDesc);
目的写入限制(txtImage);
目标写入字节((字节)(txtFav?1:0));
目的地书面资本(项目);
目标写入(转换的图像);
}
}

事实上,它没有运行任何错误,但主要问题是特定的数据库行没有更新。我从android设备监视器中找到它,发现数据库中的特定值没有更新。

您得到了可读的数据库。你不能更新这个。尝试使用可写的。

尝试以下代码:

   public int addDBfav(int id)
    {
        SQLiteDatabase sqLiteDatabase;
        DbHelper myDbHelper=new DbHelper(this);
        try{
            myDbHelper.createDatabase();
        }catch (IOException e)
        {
            throw new Error("unable to create database");
    }
    myDbHelper.openDataBase();
    sqLiteDatabase=myDbHelper.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put("fav",true);
    int part= sqLiteDatabase.update("food",contentValues,"food_id = "+Integer.toString(id),null);
        Log.i("success","successfuly done "+Integer.toString(part));
        return part;
    }

我做了,但没用。我仍然有同样的问题