gpt4 book ai didi

android - sqlite数据库没有得到更新

转载 作者:行者123 更新时间:2023-11-29 19:45:25 25 4
gpt4 key购买 nike

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

这就是我用来做到这一点的方式。我不知道当我想连接到数据库时是否必须调用创建数据库方法:

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

这是我的数据库助手:

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 设备监视器中拉出发现了它,并看到数据库中的特定值没有更新。

最佳答案

试试这段代码:

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

关于android - sqlite数据库没有得到更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37835098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com