title: How to get columns data from sqlite database in ANDROID?
body: <p>i am working with sqlite database in Android for first time. i have create my database in Sqlite and copy it to androids <code>"data/data/App package/databases"</code>. i am able to copy and open database. i have four table in database i want <code>"State_Name(Column)" from "State_Master(Table)"</code>.</p>
  
  <p>but i am not able to get any data(State name) from table. </p>
  
  <p>Here i post my code:</p>
  
  <pre><code>//DataBase Helper
  
  public class DatabaseHelper extends SQLiteOpenHelper {
  
  private String[] stateArray;
  private Context context;
  private String DB_PATH;
  private static String DB_NAME = "myDatabase.sqlite";
  public static SQLiteDatabase myDatabase;
  public static final String STATE_NAME = "State_Name";
  
  public LoveGodDB(Context context) throws Exception {
      super(context, DB_NAME, null, 1);
      this.context = context;
      DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases/";
      boolean dbexist = checkdatabase();
      if (dbexist) {
          opendatabase();
      } else {
          createdatabase();
      }
  
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL(STATE_NAME);
  }
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
  }
  public void createdatabase() throws Exception {
      boolean dbexist = checkdatabase();
      if (dbexist) {
      } else {
          this.getReadableDatabase();
          try {
              copydatabase();
          } catch (IOException e) {
              throw new Error("Error copying database");
          }
      }
  }
  private boolean checkdatabase() throws Exception {
      boolean checkdb = false;
      try {
          String myPath = DB_PATH + DB_NAME;
          File dbfile = new File(myPath);
          checkdb = dbfile.exists();
      } catch (SQLiteException sle) {
          throw sle;
      }
      return checkdb;
  }
  private void copydatabase() throws IOException {
  
      InputStream myinput = context.getApplicationContext().getAssets().open(DB_NAME);
      OutputStream myoutput = new FileOutputStream("/data/data/"+ context.getApplicationContext().getPackageName() +"/databases/myDataBase.sqlite");
      byte[] buffer = new byte[1024];
      int length;
      while ((length = myinput.read(buffer)) &gt; 0) {
          myoutput.write(buffer, 0, length);
      }
  
      myoutput.flush();
      myoutput.close();
      myinput.close();
  }
  public void opendatabase() throws SQLException {
      String mypath = DB_PATH + DB_NAME;
      LGDataBase = SQLiteDatabase.openDatabase(mypath, null,
              SQLiteDatabase.OPEN_READWRITE);
  
  }
  public synchronized void close() {
      if (LGDataBase != null) {
          LGDataBase.close();
      }
      super.close();
  }
  public Cursor fetchAllTopics() {
  
      return LGDataBase.query("State_Master", new String[] {STATE_NAME}, 
              null, 
              null, 
              null, 
              null, 
              null);
  };
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and my Activity where i want to store data in String Array:</p>
  
  <pre><code>public class MyStateGrid extends Activity{
  
  private Button back;
  private String[] stateNameArray ;
  private GridView gridview = null;
  private DatabaseHelper lgDB;
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.errandboy_state);
  
      back = (Button) findViewById(R.id.back);
      gridview = (GridView) findViewById(R.id.gridview);
      gridview.setAdapter(new ImageAdapter(this));
  
      //stateNameArray = getResources().getStringArray(R.array.stateName);
      try {
          lgDB = new LoveGodDB(this);
          lgDB.opendatabase();
          Cursor c = lgDB.fetchAllTopics();
                      startManagingCursor(c); 
          stateNameArray = new String[]LoveGodDB.STATE_NAME};
      } catch (IOException e) {
  
      } catch (Exception e) {
  
      }finally{
          if(lgDB != null){
              lgDB.close();
          }
      }
  
      gridview.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                  long arg3) {
              Intent stamp = new Intent(getParent(), xxxx.class);
              TabGroupActivity parentActivity = (TabGroupActivity)getParent();
              parentActivity.startChildActivity("Stamp", stamp);
  
          }
      });
  
      OnTouchListener backListener = new OnTouchListener() {
          public boolean  onTouch  (View  v, MotionEvent  event) {
              if (event.getAction()==MotionEvent.ACTION_UP){
                  finish();
              }
              return false;
          }
      };
  
      back.setOnTouchListener(backListener);
  }
  public class ImageAdapter extends BaseAdapter {
      @SuppressWarnings("unused")
      private Context mContext;
  
      public ImageAdapter(Context c) {
          mContext = c;
      }
  
      public int getCount() {
          return stateImageArray.length;
      }
  
      public Object getItem(int position) {
          return null;
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
          LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = vi.inflate(R.layout.galchild, null);
          ImageView stampImage;
          TextView stampName;
          stampImage = (ImageView) v.findViewById(R.id.ImageView01);
          stampName = (TextView) v.findViewById(R.id.stampName);
          stampImage.setScaleType(ImageView.ScaleType.FIT_XY);
          stampImage.setImageResource(R.drawable.stamps);
          stampName.setText(stateNameArray[position]);
          return v;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Any suggestion?</p>
  
  <p>Thanks...</p>