2017-10-18 103 views
0

對不起,但我沒有在網上找到任何東西。我在我的目錄「資產」中使用現有的數據庫。當我想更新一列時,函數返回該列已經改變,但實際上並沒有發生。我試着用execSQL,但沒有再次。更新現有的sqlite數據庫中的列,但沒有任何更改android

任何意見將不勝感激!

這裏是我的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

解決方案

我忘記用checkDatabase。在DatabaseHelper的構造函數中必須包含:

if(!checkDataBase()) 
{ 
    copyDataBase(); 
} 
1

您需要調用getWritableDatabase();

嘗試交換你的代碼之前先打開您的數據庫。

來自:

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

到:

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

不起作用。 :( – EagleJ