Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6330797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:56:09+00:00 2026-05-24T17:56:09+00:00

i am working with sqlite database in Android for first time. i have create

  • 0

i am working with sqlite database in Android for first time. i have create my database in Sqlite and copy it to androids "data/data/App package/databases". i am able to copy and open database. i have four table in database i want "State_Name(Column)" from "State_Master(Table)".

but i am not able to get any data(State name) from table.

Here i post my 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)) > 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);
};


}

}

and my Activity where i want to store data in String Array:

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<?> 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;
    }
}

}

Any suggestion?

Thanks…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:56:10+00:00Added an answer on May 24, 2026 at 5:56 pm

    I don’t understand this line:

    stateNameArray = new String[]LoveGodDB.STATE_NAME
    

    try changing it to:

    stateNameArray = new String[c.count()];
    

    Then you need to put the state names in the Cursor into the String array:

    int i = 0;
    c.moveToFirst();
    while (c.isAfterLast() == false) {
        statNameArray[i]  = c.getString(0);
        i++;
        c.moveToNext();
    }
    

    Answer adapted from: Iterate through rows from Sqlite-query

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on my first android app and have an SQLite database set up
I'm working on an Android application that stores data in a SQLite database. My
I'm working on my first Android app, and am almost to the point where
I'm working with a sqlite database, using python/django. I would need to have my
I am working with a SQLite database on android. My database manager is a
I've started working on some Android applications and have a question regarding how people
I'm working on an Android app with a Java component and a C++ component
I am working on my first Android Application and I am trying to implement
I'm working on an Android application that will come with lots of data. I'd
Working on a project at the moment and we have to implement soft deletion

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.