gpt4 book ai didi

java - 更新现有 sqlite 数据库中的列,但 android 中没有任何变化

转载 作者:行者123 更新时间:2023-12-02 12:07:13 25 4
gpt4 key购买 nike

抱歉发了这个帖子,但我在网上没有找到任何东西。我使用目录“assets”中的现有数据库。当我想更新列时,函数返回该列已更改,但实际上什么也没发生。我尝试使用 execSQL 但没有再出现。

任何意见将不胜感激!

这是我的 DatabaseHelper 代码:

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "program.db";
public static String DBLOCATION = "";
public static final String TableName="ProgramTable";
private Context mContext;
private SQLiteDatabase mDatabase;

public DatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
this.mContext = context;
if(android.os.Build.VERSION.SDK_INT >= 17){
DBLOCATION = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DBLOCATION = "/data/data/" + context.getPackageName() + "/databases/";
}

}

@Override
public void onCreate(SQLiteDatabase db) {

}
private boolean checkDataBase()
{
File dbFile = new File(DBLOCATION + DBNAME);

return dbFile.exists();
}

//Copy the database from assets
public void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DBNAME);
String outFileName = DBLOCATION + DBNAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void openDatabase() {
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase != null && mDatabase.isOpen()) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
if (mDatabase != null) {
mDatabase.close();
}
}
public long Like(int id) {
ContentValues contentValues = new ContentValues();
contentValues.put("Favourite",1);
mDatabase=this.getWritableDatabase();
openDatabase();

long k=mDatabase.update(TableName, contentValues, "ID =" +Integer.valueOf(id),null);
closeDatabase();
return k;
}
public long Unlike(int id) {
ContentValues contentValues = new ContentValues();
contentValues.put("Favourite",0);
mDatabase=this.getWritableDatabase();
openDatabase();
long k =mDatabase.update(TableName, contentValues, "ID ="+Integer.valueOf(id),null);
closeDatabase();
return k;
}
}

最佳答案

在调用getWritableDatabase();之前,您需要先打开数据库

尝试交换您的代码。

来自:

mDatabase=this.getWritableDatabase();
openDatabase();

致:

openDatabase();
mDatabase=this.getWritableDatabase();

关于java - 更新现有 sqlite 数据库中的列,但 android 中没有任何变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46819539/

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