jeudi 2 avril 2015

how to access image from remote MSSQL DB and save to sqllite DB

I want to get data from MSSQL DB on Remote server and save to sqllite db in android application.I don't want to used webservice or other way.I want to access data directly using JDTS library .My Code is everything ok except access image . When save image to sqllite , I get the following error.



04-03 09:31:35.312: W/System.err(20872): java.lang.IllegalArgumentException: the bind value at index 2 is null
04-03 10:31:45.182: W/System.err(6890): at android.database.sqlite.SQLiteProgram.bindBlob(SQLiteProgram.java:178)


This is ProductInfo class



public class ProductInfo {

public String PRODUCT_CODE;
public byte[] PRODUCT_IMAGE;

public String getPRODUCT_CODE() {
return PRODUCT_CODE;
}

public void setPRODUCT_CODE(String pRODUCT_CODE) {
PRODUCT_CODE = pRODUCT_CODE;
}

public byte[] getPRODUCT_IMAGE() {
return PRODUCT_IMAGE;
}

public void setPRODUCT_IMAGE(byte[] blob) {
PRODUCT_IMAGE = blob;
}
}


This is access data from Remote DB



public List<ProductInfo> SelectProductInfo() {
String sql ="select product_code,product_image from ProductInfo ";
ProductInfo _productInfo;
List<ProductInfo> _listProductInfo = new ArrayList();

ResultSet res = null;
Statement stmt = null;

try {
this.openDB();
stmt = DbConn.createStatement();
res = stmt.executeQuery(sql);

while (res.next()) {
_productInfo = new ProductInfo();
_productInfo.setPRODUCT_CODE(res.getString("PRODUCT_CODE")
.toString());
_productInfo.setProduct_Image(res.getBytes("PRODUCT_IMAGE"));
_listProductInfo.add(_productInfo);
} catch (Exception ex) {
ex.printStackTrace();
} finally {res.close();}
}}


This is save to sqllite DB



public void SaveProductInfo(String sql, List<ProductInfo> _listProductInfo) {

try {
if (!mDb.isOpen()) {
this.Open();
}

mDb.beginTransaction();
SQLiteStatement insertStmt = mDb.compileStatement(sql);

for (int i = 0; i < _listProductInfo.size(); i++) {
insertStmt.clearBindings();
insertStmt.bindString(1, _listProductInfo.get(i)
.getPRODUCT_CODE().toString());
insertStmt.bindBlob(2, _listProductInfo.get(i)
.getPRODUCT_IMAGE()); //get error
insertStmt.executeInsert();
}
mDb.setTransactionSuccessful();
} catch (SQLException e) {
mDb.endTransaction();
e.printStackTrace();
} finally { mDb.endTransaction();
mDb.close();}

Aucun commentaire:

Enregistrer un commentaire