0

Data are successfully inserted in DB.But when I try to retrieve data app force closes. So the scenario is user enters a date in a activity and clicking of a button a search is made in DB.If match exists,i am going to display that in a new activity.

ViewByDate Activity

        ViewDateResult vb=new ViewDateResult();
        String s=et1.getText().toString();
        vb.givedate(s);
        Intent i=new Intent(ViewByDate.this,ViewDateResult.class);
        ViewByDate.this.startActivity(i);

Here the editext value is passed as a string to next activity when the button is clicked.Once done i am starting the new activity

ViewDateResult Activity

EditText t1,t2,t3,t4;
SQLiteDatabase db;
public static String que;
void givedate(String date) 
{
 que=date;
 }
private static final String SELECT_SQL = "SELECT * FROM updatedata where Date = '" + que + "'";
private Cursor c;
protected void show()

{

   String In=c.getString(0);
   String out=c.getString(1);
   String date=c.getString(2);
   String Total=c.getString(3);
   t1.setText(In);
   t2.setText(out);

   t3.setText(date);
   t4.setText(Total);
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_date_result);
    t1=(EditText)findViewById(R.id.id1);
    t2=(EditText)findViewById(R.id.id2);
    t3=(EditText)findViewById(R.id.id3);
    t4=(EditText)findViewById(R.id.id4);
     Inserted_DB in=new Inserted_DB(this);
   SQLiteDatabase db1=in.get();

    c= db1.rawQuery(SELECT_SQL,null);

    show();
    c.close();
    db1.close();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_date_result, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

My Helper class for insertion and retrieval:

SQLiteDatabase db;
Context ctx;
public helper help;

public Inserted_DB(Context ctx)
{
    this.ctx=ctx;
    help=new helper(ctx);
}


public static class helper extends SQLiteOpenHelper
{
    public helper(Context ctx)

    {
        super(ctx,dbname,null,2);

    }
    @Override
    public void onCreate(SQLiteDatabase db) {

       try
       {
           String sql = "create table "+ table +" (InTime text,OutTime text,Date text,Differ text)";
           db.execSQL(sql);

       }
       catch(Exception e1)
        {
            e1.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + table);
        onCreate(db);
    }
}
 public void update(String In1,String out1,String date1,String d1)
   {

  db=help.getWritableDatabase();
  ContentValues con=new ContentValues();
  con.put(in,In1);
  con.put(out,out1);
  con.put(date,date1);
  con.put(diff,d1);
  db.insert(table,null,con);
  db.close();
 }
  public SQLiteDatabase get()
  {

    db=help.getReadableDatabase();
    return db;
  }

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{myapps.com.workhours/myapps.com.workhours.ViewDateResult}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
        at android.app.ActivityThread.access$800(ActivityThread.java:147)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5253)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Updated the logs and the code with the changes suggested.But still same issue.

Help me out to figure this. Thanks in advance

Sriram S
  • 169
  • 14

2 Answers2

1
1. You need to initalize db object.

YourDatabaseHelperclass database = new YourDatabaseHelperclass(this);
SQLiteDatabase db = database.getReadableDatabase();

2. you need to add single qotes in where condition.
"SELECT * FROM makeway where Date= '"+ que+"'" ;

EditText t1,t2,t3,t4;
SQLiteDatabase db;
public static String que;
void givedate(String date) 
{
 que=date;
 }
 private static final String SELECT_SQL = "SELECT * FROM makeway where Date = '"+ que+"'" ;
private Cursor c;
protected void show()

{

   String In=c.getString(0);
   String out=c.getString(1);
   String date=c.getString(2);
   String Total=c.getString(3);
   t1.setText(In);
   t2.setText(out);

   t3.setText(date);
   t4.setText(Total);
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_date_result);
    t1=(EditText)findViewById(R.id.id1);
    t2=(EditText)findViewById(R.id.id2);
    t3=(EditText)findViewById(R.id.id3);
    t4=(EditText)findViewById(R.id.id4);
YourDatabaseHelperclass database = new YourDatabaseHelperclass(this);
SQLiteDatabase db = database.getReadableDatabase();
    c= db.rawQuery(SELECT_SQL,null);

    show();
      c.close();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_view_date_result, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
0

Add this in your class,

SQLiteDatabase db;

@Override
    protected void onCreate(Bundle savedInstanceState) {

   db = new DBAdapter(ChangePasswordActivity.this);

   db.open();
        //code here
        c= db.rawQuery(SELECT_SQL,null);
   db.close();
}

AND

Change

private static final String SELECT_SQL = "SELECT * FROM makeway where Date="+ que ;

TO

private static final String SELECT_SQL = "SELECT * From makeway where Date = '" + que + "'";
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142