SQLITE数据库更新

时间:2011-11-02 12:01:25

标签: android

我创建了一个包含以下元素的数据库表:TABLE_CALLLOG(SDate varchar,Localcall varchar,National varchar);

我第一次阅读模拟器中的calllog&将值分配给数据库的表(TABLE_CALLLOG)。但是如果模拟器的calllog中有新条目而没有删除数据库,我就无法更新表。

private void Fun_UpdateCallInfo(String strDate, int iLCO,int iLCM, int iLCL) 
{ 
  try 
    { 
       myDB = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
        ContentValues initialValues = new ContentValues(); 
         String[] ss = { strDate }; 
         initialValues.put("LocalCallsOnnet", String.valueOf(iLCO));
          initialValues.put("LocalCallsMobile", String.valueOf(iLCM));
          initialValues.put("LocalCallsLandline", String.valueOf(iLCL));
         myDB.update(TABLE_CALLINFO, initialValues, "SDate=?", ss); 
          if (myDB != null) 
              { 
                 myDB.close(); 
              }
     } catch (Exception ex) 
       { 
        if (myDB != null) 
           { 
            myDB.close(); 
           } 
        } 
   }

请提供代码如何操作。

1 个答案:

答案 0 :(得分:2)

公共类DataBaseHelper扩展SQLiteOpenHelper {     私有语境mycontext;

private String DB_PATH = "/data/data/com.example.android.podemo/databases/";
private static String DB_NAME = "PartyDetails.sqlite";
public SQLiteDatabase myDataBase;
/*private String DB_PATH = "/data/data/"
                            + mycontext.getApplicationContext().getPackageName()
                            + "/databases/";*/

public DataBaseHelper(Context context) throws IOException  {
    super(context,DB_NAME,null,1);
    this.mycontext=context;
    boolean dbexist = checkdatabase();
    if(dbexist)
    {
        System.out.println("Database exists");
        opendatabase(); 
    }
    else
    {
        System.out.println("Database doesn't exist");
    createdatabase();
    }

}

public void createdatabase() throws IOException{
    boolean dbexist = checkdatabase();
    if(dbexist)
    {
        System.out.println(" Database exists.");
    }
    else{
        this.getReadableDatabase();
    try{
            copydatabase();
        }
        catch(IOException e){
            throw new Error("Error copying database");
        }
    }
}
private boolean checkdatabase() {
    //SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try{
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
        checkdb = dbfile.exists();
    }
    catch(SQLiteException e){
        System.out.println("Database doesn't exist");
    }

    /*if(checkdb!=null){
        checkdb.close();
    }
    return checkdb!=null? true :false;*/
    return checkdb;
}
private void copydatabase() throws IOException {

    //Open your local db as the input stream
    InputStream myinput = mycontext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream("/data/data/com.example.android.podemo/databases/PartyDetails.sqlite");

    // transfer byte to inputfile to outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myinput.read(buffer))>0)
    {
        myoutput.write(buffer,0,length);
    }

    //Close the streams
    myoutput.flush();
    myoutput.close();
    myinput.close();

}

public void opendatabase() throws SQLException
{
    //Open the database
    String mypath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);

}



public synchronized void close(){
    if(myDataBase != null){
        myDataBase.close();
    }
    super.close();
}