如何从两种不同的方法插入数据库

时间:2012-02-18 15:47:02

标签: android sql database stored-procedures

我有一个小问题。 我的应用程序有一个数据库,存储延迟加载的图像和Json值。 我已设法将值存储到我的数据库中,但我似乎无法将它们存储为

我想,它们以here的形式存储在数据库中。

如何让所有条目都从数据库的开头开始。

贝娄是我的代码。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);
    placeData = new DBHelper(this);

    try {
        Images("1","http://media.rightmove.co.uk/31k/30471/29462938
                    /30471_FLE100159_IMG_00_0002_max_200x138.jpg");
        Images("2","http://media.rightmove.co.uk/3k/2265/35402969
                    /2265_9897_IMG_00_0000_max_200x138.jpg");
        Images("3","http://media.rightmove.co.uk/75k/74944/18863829
                    /74944_OSI1000044_IMG_00_0000_max_200x138.jpg");
            } catch (Exception e) {
          }
           try {
        JSONArray properties = null;
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(url);
        try {
            // Getting Array of Contacts
            properties = json.getJSONArray(TAG_PROPERTY);

            // looping through All Contacts
            for (int i = 0; i < properties.length(); i++) {
                JSONObject c = properties.getJSONObject(i);
                String identifier = c.getString(TAG_IDENTIFIER);
                String price = c.getString(TAG_PRICE);
                String bedrooms = c.getString(TAG_BEDROOMS);
                String address = c.getString(TAG_ADDRESS);
                String propertyType =
                                     c.getString(TAG_PROPERTYTYPE);
                id = id;
                if (properties != null) {

                    // insert to database
                    insertData(id,identifier, price, bedrooms,
                                              address,propertyType);
                }

                 }
                  } catch (Exception e) {
                 }

                 } catch (Exception e) {
              }
               }

         private void insertData(String id,String identifier, String price,
             String bedrooms,String address, String propertyType) {
    // TODO Auto-generated method stub
    SQLiteDatabase db = placeData.getWritableDatabase();
    ContentValues v = new ContentValues();
    v.put(DBHelper.C_ID, id);
    v.put(DBHelper.C_IDENTIFIER, identifier);
    v.put(DBHelper.C_PRICE, price);
    v.put(DBHelper.C_BEDROOMS, bedrooms);
    v.put(DBHelper.C_ADDRESS, address);
    v.put(DBHelper.C_PROPERTYTYPE, propertyType);
    db.insert(DBHelper.TABLE, null, v);
    db.close();
}

private void Images(String id, String url) throws ClientProtocolException,
        IOException {
    // TODO Auto-generated method stub
    DefaultHttpClient mHttpClient = new DefaultHttpClient();
    HttpGet mHttpGet = new HttpGet(url);
    HttpResponse mHttpResponse = mHttpClient.execute(mHttpGet);
    if (mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity entity = mHttpResponse.getEntity();

        if (entity != null) {

            // insert to database
            insert(id, EntityUtils.toByteArray(entity));
        }
    }

}

private void insert(String id, byte[] image) {
    // TODO Auto-generated method stub
    SQLiteDatabase db = placeData.getWritableDatabase();
    ContentValues v = new ContentValues();
    v.put(DBHelper.C_ID, id);
    v.put(DBHelper.C_PHOTOS, image);
    db.insert(DBHelper.TABLE, null, v);
    db.close();

}

任何帮助或建议都将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

你没有按任何顺序“存储”它们 - 你可以按照你想要的顺序“检索”它们。

select * from gall
ORDER BY image

或类似的