简体   繁体   中英

cursor is not retrieving data from sqlite database

I facing problem in sqlite database. Actually I have 3 .java files and a databse helper class in which my database is created, tables are created insertion etc. all CRUD functions and I am trying to call them from some other activity class but it is not calling and not retrieving data from table. Here are my files please can anyone tell me where i am making mistake.

MainActivity.java

package mad.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    DatabaseHelper m = new DatabaseHelper(this);
    m.onCreate();

    Button menubutton4 = (Button) findViewById(R.id.InsertRecipeButton);

 menubutton4.setOnClickListener(new View.OnClickListener() {

    public void onClick(View argo) {

     Intent intent4 = new Intent(MainActivity.this, TryActivity.class);
     startActivity(intent4);  
 }
 });
}
}

When I click on the button we will reach TryActivity class which is as follows:

package mad.project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TryActivity extends Activity{

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.insertrecipe);
     final DatabaseHelper m = new DatabaseHelper(this);
              final EditText RecipeBox = (EditText) findViewById(R.id.RecipeBox);
        Button submit_button = (Button) findViewById(R.id.SubmitRecipe);

        submit_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
        String a=RecipeBox.getText().toString();
        m.Insert_Recipe_Into_DB(a);
        Intent intent23 = new Intent(TryActivity.this,RoughActivity.class);
                startActivity(intent23);
            }
            });}}

In this class I have created an object 'm' of Databsehelper class which is used to call Insert_Recipe_Into_DB function of DatabaseHelper class. Uptill now no issue if I remove Intents statements from above. But if I call another activity class after calling insertion function it shows "forcesclose" error. the next activity class is as follows:

RoughActivity.java

package mad.project;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class RoughActivity extends Activity{

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
             final DatabaseHelper m = new DatabaseHelper(this);
             String [] rec ;
             rec=m.get_recipe();
                int rec_len = rec.length;
                 String [] todaymenu = new String[rec_len];
                 for(int i = 1;i<=rec_len;i++)
                 {
                     todaymenu[i]=rec[i-1];
                 }
     this.setListAdapter(newArrayAdapter<String>(RoughActivity.this,
         R.layout.todays_menu_view,R.id.Today,rec));
                 }

private void setListAdapter(ArrayAdapter<String> arrayAdapter) {
    // TODO Auto-generated method stub
    }
  }

DatabaseHelper.java

package mad.project;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{

static final String dbName="CookBookDatabase";

 public DatabaseHelper(Context context)
{
    super(context, dbName, null, 33);
    // TODO Auto-generated constructor stub
}
 static final String Try_Table= "Try_Table";
 static final String Recipe= "Recipe";

public void onCreate() {        
execSQL("CREATE TABLE "+Try_Table+" ( "+ Recipe + " VARCHAR ) " ); 
}

private void execSQL(String string) {
    // TODO Auto-generated method stub
    }

public void Insert_Recipe_Into_DB(String a)
{
    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(Recipe, a);
 db.insert(Try_Table, Recipe, cv);
 db.close();
}

String[] get_recipe()
{
    String [] recipe = null;
    SQLiteDatabase db=this.getReadableDatabase();

    Cursor count =db.rawQuery("SELECT COUNT(*) FROM "+Try_Table, null);

     count.moveToFirst();
     if(count.getInt(0) == 0)
     {
         db.close();
         return recipe;
     }
     if(count.getInt(0)!= 0 )
     {

    Cursor cur=db.rawQuery("SELECT * FROM "+Try_Table, null);

     int br_count =cur.getCount();
     if(br_count != 0)
     {
         int i=0, index;
         recipe= new String[br_count];
         cur.moveToFirst();
         do {
            index=cur.getColumnIndex(Recipe);
             recipe[i++]=cur.getString(index);

         }while(cur.moveToNext());
     }
     }


    db.close();
    return recipe;
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}
}

Please tell me where i am making mistake whether in DatabaseHelper class or RoughActivity class.

You need to open your database in the activity you retrieve or insert data into your SQLite database. Also make sure you close it when your finished.

final DatabaseHelper m = new DatabaseHelper(this);
 m.open();

In your onDestroy or pause. Call

m.close();

This may be the issue, because i dont see where you open your database in the code.

EDIT:

In your DatabaseHelper class. Create a method.

public void open(){
db.open();
}

public void close(){
db.close()
}

Then you will have the method to close and open the database in your activities where you need to insert and retrieve information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM