sqlite数据库没有得到更新

时间:2016-06-15 12:11:14

标签: android sqlite android-sqlite sqliteopenhelper

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

这是我用来做这件事的方式。我不知道当我想连接到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);
    }
}

实际上它没有运行任何错误,但主要问题是特定的数据库行没有更新。我从Android设备监视器中拉出来发现它并且看到数据库中的特定值没有更新。

2 个答案:

答案 0 :(得分:1)

你有可读的数据库。你不能更新这个。尽量使用可写。

答案 1 :(得分:1)

试试这段代码:

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