Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

android - Issue From Reading from a SQLite Database

I dont have much knowledge in using the "Cursor" to read data of SQLite database but using some references and examples i compiled a few to setup this i dont why it would read from the database, i check if data is entered into the database using a database browser and its present can some one tell me how to make this work?

I have a separate class holding the db connections and queries, in that i have a function to get all fields which have a long value of a specifed value

public Cursor getAppointments(long date) throws SQLException
    {
    Cursor mCursor =
    db.query(true, DATABASE_TABLE, new String[] {
    KEY_ROWID,
    APP_TIME,
    APP_TITLE,
    },
    APP_DATE + "="  + date,
    null,
    null,
    null,
    null,
    null);
    if (mCursor != null) {
    mCursor.moveToFirst();
        }
    return mCursor;
    }

what this code is supposed to do is get all the records which have the corresponding date as the queried value and return.

Now in the main class i have this code to catch and display in a Textview, row by row.

 db.open();
       Display.setText("");
       Cursor c = db.getAppointments(SDate);

      if (c.moveToFirst())
          Display.setText(DisplayTitle(c));
       else
       Toast.makeText(this, "No title found",
       Toast.LENGTH_LONG).show();

       db.close();

and the display title method is

private String DisplayTitle(Cursor c) {

        String Final ="";

        Final = c.getString(1)  + " " +c.getString(2) + " " +  c.getString(3)  + "
" ;

        return Final;

    }

Can someone tell me what are the changes necessary inorder to retrieve the records which have the same long value as the date and display it in the textview, there can be multiple records with the same long value, so have to retrieve all the records with the same long value for date and display it in the textview.

Cheers, Looing for a fast reply. :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this simple approach of using cursor:

 String q = "SELECT * FROM DATABASE_TABLE where(APP_DATE like '"+date+"')";//your query
 Cursor c = db.rawQuery(q, null); //  db is a object of SQLiteDatabase type  

Then retrieve value of respective column using column name with getColumnIndex() method if you are not sure about column index like:

              if(c.getCount() == 0)
              {                   
                 //No entry found
              }
              else  {
                  c.moveToFirst();
                  do {
                      String mail = c.getString(c.getColumnIndex("KEY_ROWID"))+c.getString(c.getColumnIndex("APP_TIME"))+c.getString(c.getColumnIndex("APP_TITLE")); 
                        // do whatever you like with each record                    
                    } while (c.moveToNext());               
            c.close();
            db.close();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...