对不起该帖子,但我没有在网上找到任何内容。我在目录“assets”中使用现有数据库。当我想更新列时,函数返回列已更改,但实际上没有任何反应。我试过execSQL,但没有再尝试过。
íny意见将不胜感激!
这是我的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;
}
}
答案 0 :(得分:1)
在调用with open('datafile1.npy', 'wb') as fp:
np.save(data, fp)
np.save(labels, fp)
尝试交换您的代码。
发件人:强>
getWritableDatabase();
以强>
mDatabase=this.getWritableDatabase();
openDatabase();
答案 1 :(得分:0)
我忘了使用checkDatabase。在DatabaseHelper的构造函数中必须包含:
if(!checkDataBase())
{
copyDataBase();
}