2013-05-27 33 views
2

我想用一個插入或更換查詢,這裏即時給予我有參數與sqlite.execSQL函數傳遞的Android

String sql="insert or replace into Stock_Table values (?,?,?,?,?,?)"; 

    sqlite.execSQL(sql, new String[]{id,name,code,vat,itmquan,pric}); 

現在我想另外一個插入更多的外地到此表的代碼,但字段不是字符串 它的一個字節數組格式,我想添加一個圖像到這個表中,我給這個圖像的數據類型爲表中的BLOB,但我無法將字節數組格式作爲參數傳遞到上面的sqlite .execsql func,這裏即時給我的格式,我想插入

byte []byteImage; 

String sql="insert or replace into Stock_Table values (?,?,?,?,?,?,?)"; 

sqlite.execSQL(sql, new String[]{id,name,code,vat,itmquan,pric,byteImage}); 

我怎樣才能使人們有可能,我能不能夠轉換這個字節數組轉換爲字符串,我想插入圖片爲BLOB格式而已,請大家幫我找到任何解決方案

+0

參考此鏈接http://www.coderanch.com/t/550604/Android/Mobile/insert-retreive-image-database –

+0

this li nk非常糟糕......你不應該使用execSQL進行SELECT/INSERT/UPDATE/DELETE !!! http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#replace(java.lang.String,%20java.lang.String,%20android.content.ContentValues) – Selvin

+0

我已經嘗試過@ sunil說,但我不能插入有錯誤,有沒有其他方法? –

回答

2

使用insert method的一個變種,這需要一個ContentValues object,它允許您使用直接的字節數組:

byte[] byteImage; 
ContentValues cv = new ContentValues(); 
cv.put("id", id); 
// ... 
cv.put("blobcolumn", byteImage); 
sqlite.insertWithOnConflict("Stock_Table", null, cv, SQLiteDatabase.CONFLICT_REPLACE); 
+0

然後wat關於替換選項? –

+0

我不能把CONFLICT_REPLACE放在這個函數中。 –

0

使用這種方式..

@Override 
    public void onClick(View v) { 

     SQLiteDatabase myDb;  
     String MySQL; 
     int icount; 
     byte[] byteImage1 = null; 
     byte[] byteImage2 = null; 

     MySQL="create table emp1(_id INTEGER primary key autoincrement, fio TEXT not null, picture BLOB);"; 
      myDb = openOrCreateDatabase("/sdcard/MyDB.db", Context.MODE_PRIVATE, null); 
     //  myDb.execSQL(MySQL); 
      String s=myDb.getPath(); 
      textView.append("\r\n" + s+"\r\n");  
      myDb.execSQL("delete from emp1"); 
      ContentValues newValues = new ContentValues(); 
      newValues.put("fio", "hello dude"); 

      /////////// insert picture to blob field ///////////////////// 
      try 
      { 
      FileInputStream instream = new FileInputStream("/sdcard/sunil.png"); 
      BufferedInputStream bif = new BufferedInputStream(instream); 
      byteImage1 = new byte[bif.available()]; 
      bif.read(byteImage1); 
      textView.append("\r\n" + byteImage1.length+"\r\n"); 
      newValues.put("picture", byteImage1); 

      long ret = myDb.insert("emp1", null, newValues); 
      if(ret<0) 
     textView.append("\r\n!!! Error add blob filed!!!\r\n"); 
      } catch (IOException e) 
      { 
       textView.append("\r\n!!! Error: " + e+"!!!\r\n"); 
      } 

     ////////////Read data //////////////////////////// 
     Cursor cur = myDb.query("emp1",null, null, null, null, null, null); 
      cur.moveToFirst(); 
      while (cur.isAfterLast() == false) 
      { 
       textView.append("\r\n" + cur.getString(1)+"\r\n"); 
       cur.moveToNext(); 
      } 
     ///////Read data from blob field//////////////////// 
      cur.moveToFirst(); 
      byteImage2=cur.getBlob(cur.getColumnIndex("picture")); 
     bmImage.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2, 0, byteImage2.length)); 
      textView.append("\r\n" + byteImage2.length+"\r\n"); 
     ////////////////////////// 
      cur.close(); 
      myDb.close(); 

    }