我创建了一个由警报管理器每5秒调用一次的服务。我的服务使用dbHandler类中的方法来添加,更新,检查更新的信息并检查数据库中数据的可用性。将数据插入表后,下次再次调用我的服务时,它仍然没有结果,并且再次输出相同的数据。服务检查和添加数据的日志:
dbHandler onCreate:
@Override
public void onCreate(SQLiteDatabase db){
String query = "CREATE TABLE " + tableN + "(" +
column_id + " INTEGER PRIMARY KEY," +
column_name + " TEXT ," +
column_desc + " TEXT ," +
column_activity + " TEXT ," +
column_contact + " TEXT " +
");";
db.execSQL(query);
}
检查数据并将数据添加到数据库的方法
public void add_n(nDatabse tuple) {
ContentValues values = new ContentValues();
values.put(column_id, tuple.get_id());
values.put(column_name, tuple.get_name());
values.put(column_desc, tuple.get_description());
values.put(column_activity, tuple.get_activity());
values.put(column_contact, tuple.get_contact());
SQLiteDatabase db = getWritableDatabase();
//Inserting in Database
db.insert(tableN, null, values);
//checking Entry of Database
String query = "SELECT * FROM " + tableN + " WHERE " + column_id + " = " + tuple.get_id() + " ";
Cursor c = db.rawQuery(query, null);
if (c.getCount() > 0) {
c.moveToFirst();
Log.d( " After Adding Data...",c.getString(1));}
db.close();
}
检查可用性
public boolean checkNotAvailable(nDatabse tuple) {
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + tableN + " WHERE " + column_id + " = " + tuple.get_id() + " ";
Cursor c = db.rawQuery(query, null);
//Log.d("as",toString().valueOf(tuple.get_id()));
if (c.getCount() == 0) {
Log.d("NO ENTRY", toString().valueOf(tuple.get_id()));
return true;
}
db.close();
return false;
}
从服务
调用方法 //doing 5 times
dbHandler dbH = new dbHandler(this, null, null, 1);
if (dbH.checkNotAvailable(n))
dbH.add_n(n);
Log.d在每次服务电话上重复输出
D/NO ENTRY: 0
D/ After Adding Data...: Name 1
D/NO ENTRY: 1
D/ After Adding Data...: Name 2
D/NO ENTRY: 2
D/ After Adding Data...: Name 3
D/NO ENTRY: 3
D/ After Adding Data...: Name 4
D/NO ENTRY: 4
D/ After Adding Data...: Name 5
答案 0 :(得分:0)
问题是,我将null传递给dbhandler构造函数并且没有指定数据库的名称,这可能导致形成在服务停止时清除的临时数据库。