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();
}
}
This:
Should be this if you want the DB to automatically take care of the primary key for you:
Otherwise you need to manually add the “_id” when you do an insert.
Using
autoincrementis by far the easier solution.Then if you want all data from the record, simply do this:
If you only want specific bits, only call for them (but remember when using most Android widgets, you must have
_idin the cursor, even if you don’t display it). So if you just wanted the nickname, your query might look like this: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…
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 allstartManagingCursor(cur);right after you get the cursor. That way the system will manage the cursor for you.