返回列cel的id

时间:2017-07-13 18:18:24

标签: java android

嘿,这会返回插入参数的日期的ID,所以想象一下如果插入的日期是01-07-2017并且他在桌子上的ID是4,它会返回4吗?

COL_1="ID";
COL_4="DATES";

public int getDateId(String date){
    int dateID=0;
    String last_query = "SELECT " + COL_1  + " FROM " + TABLE_NAME  + " WHERE " + COL_4 + " = '" + date + "'";
    Cursor c = database.rawQuery(last_query, null);
    if (c != null && c.moveToFirst())
    {
        dateID = c.getInt(c.getColumnIndex("ID"));
    }
    c.close();

    return dateID;
}

对我来说是0。

我在这里调用这个方法:

    @Override
    public void onSelectDate(Date date, View view) {


        if(DatesList.contains(date)){
            myDB = CustomApplication.getDatabaseHelper();

            final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

            String printedData = df.format(date.getTime());


            int dateID = myDB.getDateId(printedData);
            Log.d(Tag,""+dateID);
            String returnedData = myDB.getDates(dateID);

            Intent i = new Intent(getContext(),TesteInfoDisplay.class);



            startActivity(i);
        }
        else{
            Toast.makeText(getContext(),"Não tem nenhum teste nesse dia.",Toast.LENGTH_LONG).show();
        }

    }
};

数据库:https://gist.github.com/Poreis/2305632568388077be0ba122d029d3c7

2 个答案:

答案 0 :(得分:1)

仅使用索引号而不是

  

c.getColumnIndex(" ID&#34)

并试试这个

     COL_1="ID"
 COL_4="DATES"

     public int getDateId(String date){
int dateID=0;
String last_query = "SELECT " + COL_1  + " FROM " + TABLE_NAME  + " WHERE " + COL_4 + " = '" + date + "'";
Cursor c = database.rawQuery(last_query, null);
if (c != null && c.moveToFirst())
{
    dateID = c.getInt(0);
}
c.close();

return dateID;
}

我在这里调用这个方法:

    @Override
    public void onSelectDate(Date date, View view) {


        if(DatesList.contains(date)){
            myDB = CustomApplication.getDatabaseHelper();

            final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

            String printedData = df.format(date.getTime());


            int dateID = myDB.getDateId(printedData);
            Log.d(Tag,""+dateID);
            String returnedData = myDB.getDates(dateID);

            Intent i = new Intent(getContext(),TesteInfoDisplay.class);



            startActivity(i);
        }
        else{
            Toast.makeText(getContext(),"Não tem nenhum teste nesse dia.",Toast.LENGTH_LONG).show();
        }

    }
};

数据库:  https://gist.github.com/Poreis/2305632568388077be0ba122d029d3c7

答案 1 :(得分:0)

这看起来有点复杂,但你应该检索你正在寻找的信息。我写的这很匆忙,如果不是所有的变量名都是你所拥有的,那就很抱歉。

    String[] projections = {
            "ID"
    };


    String selection = "DATES" + " = ?";
    String[] selectionArgs = {date};


    Cursor cursor = db.query(TABLE_NAME,
            projections,
            selection,
            selectionArgs,
            null,
            null,
            null);

    while(cursor.moveToNext()){
        long itemID = (cursor.getLong(cursor.getColumnIndexOrThrow("ID")));
        Log.d(TAG, "ITEM _ID : "+ itemID);
    }
    cursor.close();