** 数据库**
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME
+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " + key_msg + " STRING, " + key_isread + " STRING)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
//*************************--------插入 GCM 消息---------* *************************//
public void insert_GCM_receive_data(String msg) {
String value;
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(key_msg, msg);
cv.put(key_isread, "N");
value = cv.toString();
db.insert(TABLE_NAME, null, cv);
System.out.println("/n******this is temp table name " + TABLE_NAME + "\nthis is temp msg " + cv + "\nmsg" + msg + "\nval" + value);
db.close();
}
//--------------------------------------------- --------------------------------------------------//
//**********************----------获取警报数据---------- --******************************//
公共 ArrayList get_alert_msg() {
ArrayList<String> name = new ArrayList<String>();
try {
SQLiteDatabase db = getWritableDatabase();
Cursor c = null;
c = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
System.out.println(c);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String str_id = c.getString(0);
String str_msg = c.getString(1);
String str_read = c.getString(2);
Log.e("value", str_id + str_msg + str_read);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("msg", str_msg);
hm.put("isread", str_read);
name.add(str_msg);
}
c.close();
db.close();
} catch (Exception e) {
Log.e("this not work", "" + e);
}
return name;
}
//调用方法如2活动
dbHelper.get_alert_msg();
....
数据库类中的数据操作和检索操作使用...
一旦您在检索数据后关闭数据库。
对两个活动使用通用数据库方法..希望这对您有所帮助..