【发布时间】:2014-04-07 21:26:32
【问题描述】:
我正在尝试制作一款 Android 应用来帮助患有头痛的人。我有一个 sqlite 数据库来存储危机,用户可以通过按下按钮来添加危机。相同的按钮用于指示危机已经结束。换句话说,当您感到头痛时,您按下按钮;然后,当它结束时,再次按下它,应用程序会更新相应的“结束日期”条目。
但如果我的插入做得很好,我的更新根本不会更新。这是它应该如何工作的:
我首先检索数据库中的最新条目(即具有最大 id 的条目),然后获取实际日期,并将其放入 ContentValue。最后我更新了条目。
这是按钮代码:
public void onClickStartStop(View v){
Log.v("andromed", "Starting/Stopping crisis");
String d = new Date().toString();
ContentValues cv = new ContentValues();
String user_info = "";
String[] projection = {CriseContract.COLUMN_NAME_CRISE_ID, CriseContract.COLUMN_NAME_CRISE_DEBUT,CriseContract.COLUMN_NAME_CRISE_FIN};
Cursor criseCursor = getContentResolver().query(CriseContract.CONTENT_URI, projection,"SELECT MAX("+CriseContract.COLUMN_NAME_CRISE_ID+") FROM "+CriseContract.TABLE_NAME, null, null);
Log.v("andromed",""+criseCursor.getCount());
if(criseCursor.getCount()>=0){
while(criseCursor.moveToNext()){
String date_fin = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_FIN));
if(!(date_fin==(null))){
Log.v("andromed","Date exists "+date_fin);
user_info = "Crise enregistrée";
cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, d);
Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv);
}else{
String date_deb = criseCursor.getString(criseCursor.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_DEBUT));
if(date_deb==null){
Log.v("andromed","No date in db");
user_info = "Crise enregistrée";
cv.put(CriseContract.COLUMN_NAME_CRISE_DEBUT, d);
Uri u = getContentResolver().insert(CriseContract.CONTENT_URI, cv);
}else{
Log.v("andromed", "Need to close the crisis");
cv.put(CriseContract.COLUMN_NAME_CRISE_FIN, d);
int tmp = getMaxId();
String where = CriseContract.COLUMN_NAME_CRISE_ID+"="+tmp;
String[] st = {""+tmp};
int nup = getContentResolver().update(CriseContract.CONTENT_URI,cv, where, null);
Log.v("andromed", nup+" rows updated");
user_info = "Crise terminée";
}
}
}
}else{
user_info = "Erreur lors de la lecture";
}
Toast t = Toast.makeText(getApplicationContext(),user_info, Toast.LENGTH_LONG);
t.show();
}
(别介意日志和吐司的东西,只为我)。
这是我检索最大 id 的函数:
private int getMaxId(){
String[] projection = {CriseContract.COLUMN_NAME_CRISE_ID};
String selection = "SELECT MAX("+CriseContract.COLUMN_NAME_CRISE_ID+") FROM "+CriseContract.TABLE_NAME;
Cursor c = getContentResolver().query(CriseContract.CONTENT_URI, projection, selection, null, null);
Log.v("andromed", ""+c.getCount());
int maxid=-1;
if(c!=null){
while(c.moveToNext()){
maxid = c.getInt(c.getColumnIndex(CriseContract.COLUMN_NAME_CRISE_ID));
}
}
Log.v("andromed", "Greatest id in table Crise : "+maxid);
return maxid;
}
当然还有我的合同课:
public final static class CriseContract{
public static final String AUTHORITY = "com.piertris.andromed";
public static final String BASE_PATH = "database";
public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/"+BASE_PATH);
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE+"/"+BASE_PATH;
public static final String CONTENT_ITEM_TYPE= ContentResolver.CURSOR_ITEM_BASE_TYPE+"/andromed";
public static final String TABLE_NAME = "crises";
public static final String COLUMN_NAME_CRISE_ID = "criseid";
public static final String COLUMN_NAME_CRISE_DEBUT = "date_debut";
public static final String COLUMN_NAME_CRISE_FIN = "date_fin";
public static final String COLUMN_NAME_INTENSITE = "intensite";
public static final String COLUMN_NAME_SYMPTOM = "symptome";
public static final String COLUMN_NAME_MED = "prise_med";
public static final String COLUMN_NAME_MEDS = "type_med";
public static final String COLUMN_NAME_AURA = "aura";
public static final String COLUMN_NAME_COMMENT = "comments";
}
当我试图结束当前的危机时,我的 Logcat 告诉我更新了 0 行。
感谢 SO,由于错误使用该功能,我已经纠正了其他问题,但这次,我找到的唯一链接是这个:Android content provider not updating database 并且 OP 刚刚添加了一条评论,说他更新了他的 ContentProvider,但仅此而已。
我做错了什么?我是否“错误命名”了我的列名?我是否误用了更新功能?
感谢您的帮助。
编辑
感谢 Jozua,我意识到我没有在我的 ContentProvider 文件中实现更新功能。好吧,我现在感觉非常愚蠢。编写 update() 函数后,我会及时通知您它是如何工作的。
再次感谢 Jozua。
【问题讨论】:
-
一款让人头疼的应用哈...酷
-
@hiphopdroid 嘿,学校项目,你知道...
-
是的@vrashnak,我知道
-
@hiphopdroid 好吧,至少它仍然很有趣,但相当困难。
-
困难的事情总是让好奇的人感兴趣;)
标签: android android-sqlite android-contentprovider