2

i want use an insert or replace query, here i m giving the code that i have

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

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

now i want to additionally insert one more field into this table but that field is not string its a byte array format, i want to add an image into this table, i given the data type for this image as BLOB in table, but i cant able to pass byte array format as a parameter into the above sqlite.execsql func, here i m giving the format that i want to insert

byte []byteImage;

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

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

how can i make it possible, i cannot able to convert this byte array into string i want to insert image as BLOB format only, please help me to find any solution

3

2 Answers 2

2

Use a variant of the insert method, which takes a ContentValues object, which allows you to use a byte array directly:

byte[] byteImage;
ContentValues cv = new ContentValues();
cv.put("id", id);
// ...
cv.put("blobcolumn", byteImage);
sqlite.insertWithOnConflict("Stock_Table", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
Sign up to request clarification or add additional context in comments.

2 Comments

then wat about the replace option ?
i cannot able to put CONFLICT_REPLACE in this function.
0

use this way..

@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();

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.