更新现有sqlite数据库中的列,但在android

时间:2017-10-18 21:28:03

标签: java android sqlite

对不起该帖子,但我没有在网上找到任何内容。我在目录“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;
    }
}

2 个答案:

答案 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();
}