我的问题是如何在我从Web服务收到的sqlite数据库中存储值。我是Android新手。我需要一些帮助。提前致谢。我现在几天都在搜索一个合适的答案,完全没有解决方案。
以下responseStr
是我收到的对象,来自webservice的响应,我希望将其值存储在数据库中。
SQLiteDB sqlite_obj = new SQLiteDB(UrlActivity.this);
try{
JSONObject mainObject = new JSONObject(responseStr);
String url = mainObject.getString("url");
if(url.equals("valid")){
//JSONObject data= new JSONObject();
//data.put("data",jsonArray2);
//List <NameValuePair> nvps2 = new ArrayList <NameValuePair>();
//nvps2.add(new BasicNameValuePair("data", mainObject.toString()));
try{
ProgressBar1.setVisibility(View.GONE);
Toast.makeText(UrlActivity.this, responseStr, Toast.LENGTH_LONG).show();
sqlite_obj.open();
sqlite_obj.deleteAll();
JSONArray jsonArray2 = new JSONArray();
jsonArray2.put(mainObject);
for (int i = 0; i < jsonArray2.length(); i++) {
JSONObject object = jsonArray2.getJSONObject(i);
//sqlite_obj.insert();
}
sqlite_obj.close();
Toast.makeText(getBaseContext(), "Stored in SQLite DB", Toast.LENGTH_LONG).show();
} catch (android.database.sqlite.SQLiteException e) {
Log.e(TAG, "SQLiteException:" + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception:" + e.getMessage());
}
}
另外,下面是我的SQLiteDB类。
public class SQLiteDB {
public static final String KEY_ROWID = "_id";
public static final String KEY_USER_ID= "user_id";
public static final String KEY_NUMBER_OF_CHECKIN= "no_of_checkin";
public static final String KEY_STATUS="status";
public static final String KEY_URL="url";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "SQLiteDB";
private static final String DATABASE_TABLE = "databasetable";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table databasetable (_id integer primary key autoincrement, " +
"user_id integer not null, " +
"no_of_checkin integer not null, " +
"status integer not null, " +
"url text not null );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public SQLiteDB(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS databasetable");
onCreate(db);
}
}
//---open SQLite DB---
public SQLiteDB open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
//---close SQLite DB---
public void close() {
DBHelper.close();
}
//---insert data into SQLite DB---
public long insert(
Integer user_id, Integer no_of_checkin, Integer status, String url) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USER_ID, user_id);
initialValues.put(KEY_NUMBER_OF_CHECKIN, no_of_checkin);
initialValues.put(KEY_STATUS, status);
initialValues.put(KEY_URL, url);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---Delete All Data from table in SQLite DB---
public void deleteAll() {
db.delete(DATABASE_TABLE, null, null);
}
}