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 8182067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:43:59+00:00 2026-06-07T00:43:59+00:00

I have a database in one activity that I want to open in another

  • 0

I have a database in one activity that I want to open in another activity, also in this activity I wish to create a List View:

my code as follows

messagedb=context.openOrCreateDatabase("message",0, null);
                    messagedb.execSQL(
                            "CREATE TABLE IF NOT EXISTS tab( " +
            " _id INTEGER PRIMARY KEY AUTOINCREMENT ," +
            " nickname varchar,sender INT(13),body varchar)");


                    messagedb.execSQL("INSERT INTO tab VALUES('"+nickname+"','"+sender+"','"+sb+"')");
                    messagedb.close();
                    mydb.close();

when i am inserting values it says your database having 4 columns but you have ente 3 column values………….how can i add _id automatically from table……

my listView activity as below

public class ListView extends ListActivity {

SQLiteDatabase messagedb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_list);



    messagedb=ListView.this.openOrCreateDatabase("message",0, null);
    messagedb.execSQL(
            "CREATE TABLE IF NOT EXISTS tab(" +
            " _id INTEGER PRIMARY KEY AUTOINCREMENT," +
            " nickname varchar,sender INT(13),body varchar)");



    Cursor cur = messagedb.rawQuery("select _id, nickname, body from tab", null);
    String[] from = new String[] { "nickname", "body" };
    int[] to = new int[] { R.id.sender_entry, R.id.body_entry};

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.my_list_entry, cur, from, to);

    this.setListAdapter(mAdapter);

    cur.close();

    messagedb.close();


}

protected void onListItemClick(android.widget.ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    String nickName = ((TextView) v.findViewById(R.id.sender_entry)).getText().toString();

    String body = ((TextView) v.findViewById(R.id.body_entry)).getText().toString();

    Intent intent = new Intent(ListView.this,MessageViewPage.class);
    Bundle b = new Bundle();

    b.putString("nick", nickName);
    b.putString("body", body);

    intent.putExtras(b);

    startActivity(intent);

    finish();





}

}
  • 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-06-07T00:44:01+00:00Added an answer on June 7, 2026 at 12:44 am

    This:

    " _id INTEGER PRIMARY KEY ,"
    

    Should be this if you want the DB to automatically take care of the primary key for you:

    " _id INTEGER PRIMARY KEY AUTOINCREMENT,"
    

    Otherwise you need to manually add the “_id” when you do an insert.

    Using autoincrement is by far the easier solution.

    Then if you want all data from the record, simply do this:

    Cursor cur = messagedb.rawQuery("select * from tab", null); 
    

    If you only want specific bits, only call for them (but remember when using most Android widgets, you must have _id in the cursor, even if you don’t display it). So if you just wanted the nickname, your query might look like this:

    Cursor cur = messagedb.rawQuery("select _id, nickname from tab", null); 
    

    You also should only have code to create the table in one place. Having it in two places is just bad. It looks like what is happening now is you changed one but not the other, and the one you didn’t change is getting called first, creating the table without your change then when the second one gets called, the table alre4ady exists so it doesn’t make your changes. At the least you need to change both table creation statements to be the same. the better solution is to get rid of one of them.

    You should also use ContentValues to do your inserts…

    ContentValues initialValues = new ContentValues();
    initialValues.put("nickname", nickname);
    initialValues.put("sender", sender);
    initialValues.put("body", sb);
    messagedb.insertWithOnConflict(tab, null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);
    

    Oh, you’ll have problems if you close the cursor while your list is still active. Get rid of the cur.close() call and instead all startManagingCursor(cur); right after you get the cursor. That way the system will manage the cursor for you.

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

Sidebar

Related Questions

I have a website that has one database I have also Desktop apps that
I have two database one with relationship(no data) and another with out Relationship (with
i have one database, and it contains some columns. My requirement is that how
I've more than one activity (inside the same Application) that needs to have access
I want to create an informative app that will display a list of content.
I have a ListActivity that presents a list of names from a database using
I have simple database - one table with 6 collumns. 3 of them i
I have one database whose size is growing very fast. Curruntly its size is
I have one database table which contains 8 columns. One of the columns is
I have one database table TABLE_TIPS with two value 1. tips_id (auto increment) 2.

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.