如何在where子句中使用字符串比较正确编写SQLite更新函数

时间:2014-05-09 21:45:48

标签: java android sqlite sql-update

我有一个表,其中两列首先是日期,第二列是计数器。我没有主键,但日期是一个。所以我的代码检查表中是否存在当前日期,如果是,它只会增加计数器,否则它将添加一个新条目。只有最后一行才能存在当前日期。这是代码:

SQLiteOpenHelper实现:     公共类SqliteHelperInstance扩展了SQLiteOpenHelper {

public static final String DATABASE_NAME = "app_stats";
public static final String TABLE_NAME = "day_stats";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_CIGCOUNT = "cigcount";
public static final int DATABASE_VERSION = 1;

private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " +  COLUMN_DATE + " text, "
+ COLUMN_CIGCOUNT + " integer );";

public SqliteHelperInstance(Context context){
    super(context,DATABASE_NAME , null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_DB);

}
}

数据库管理器的实现:

public class DbManager {

private SqliteHelperInstance sqlHelper;
private SQLiteDatabase database;
private String[] columns =  { SqliteHelperInstance.COLUMN_DATE, SqliteHelperInstance.COLUMN_CIGCOUNT};
 public DbManager(Context context){
    sqlHelper = new SqliteHelperInstance(context);
}
 public void addEntry(Calendar cal){
    ContentValues values = new ContentValues();
    String dateEntry = dateFormatter(cal);
Cursor cursor = database.query(SqliteHelperInstance.TABLE_NAME, null, null, null, null, null, 
            SqliteHelperInstance.COLUMN_DATE + " DESC LIMIT 1");
    //check if the last entered date is equal to the current date
    if(cursor.moveToFirst()){
        String lastDate = cursor.getString(0);
        if(lastDate.equalsIgnoreCase(dateEntry)){
            //the cigNum should be updated for the current date
            int cigNum = cursor.getInt(1);
            cigNum ++;

            values.put(SqliteHelperInstance.COLUMN_DATE,lastDate);
            values.put(SqliteHelperInstance.COLUMN_CIGCOUNT, cigNum);
            int i = database.update(SqliteHelperInstance.TABLE_NAME, values,SqliteHelperInstance.COLUMN_DATE + " = " + lastDate , null);
            if(i == 0){
                Log.d("Tag", "No row is affected");
            }
        }
        else{
            //the last date is different than current day so we need to add a new entry
            DbEntry newEntry = new DbEntry();
            newEntry.setDate(dateEntry);
            values.put(SqliteHelperInstance.COLUMN_DATE, dateEntry);
            values.put(SqliteHelperInstance.COLUMN_CIGCOUNT, newEntry.getCigNum());
            database.insert(SqliteHelperInstance.TABLE_NAME, null, values);
        }
    }
    else{
        //its the first time that the application is loading
        DbEntry newEntry = new DbEntry();
        newEntry.setDate(dateEntry);
        newEntry.setCignum(1);
        values.put(SqliteHelperInstance.COLUMN_DATE, dateEntry);
        values.put(SqliteHelperInstance.COLUMN_CIGCOUNT, newEntry.getCigNum());
        Log.i(null, "values are set");
        database.insert(SqliteHelperInstance.TABLE_NAME, null, values);
    }


}

但是当我运行应用程序时,我一直受到更新函数影响的0行。我假设where子句中的比较没有返回任何特定的行。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试像这样获取lastDate:

String lastDate = cursor.getString(cursor.getColumnIndex(SqliteHelperInstance.COLUMN_DATE));

此外,您应该使用参数化查询:

int i = database.update(SqliteHelperInstance.TABLE_NAME, values,SqliteHelperInstance.COLUMN_DATE + " = ?", new String[]{ lastDate });

除此之外,我不确定会出现什么问题。