i have created a database with the table name tbl_customer and tbl_product…i can view the tbl_customer values but can’t see tbl_product values.. I use adb shell to confirm my insertion. Can any one plz help me to figure out the issue
private void addProFromDB() {
// TODO Auto-generated method stub
ArrayList<String> results = new ArrayList<String>();
SQLiteDatabase sampleDB = null;
try {
list = (ListView)findViewById(android.R.id.list);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, 1, null);
sampleDB.execSQL("create table tbl_product("
+ "pro_id integer PRIMARY KEY autoincrement,"
+ "pro_name text," + "pro_price integer);");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAMES
+ " Values ('1','Milk','60');");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAMES
+ " Values ('2','Sugar','70');");
sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAMES
+ " Values ('3','Oil','200');");
Cursor c = sampleDB.query(SAMPLE_TABLE_NAMES, null, null, null, null, null,
null);
char pro_nameColumnIndex = (char) c.getColumnIndexOrThrow("pro_name");
int pro_priceColumnIndex = (int) c.getColumnIndexOrThrow("pro_price");
} finally {
if (sampleDB != null)
sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAMES);
sampleDB.close();
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, new ArrayList()));
new AddStringTask().execute();
}
Also help me how i can get its primary key and display the selected value…
Best regards….
First, it is a good idea to consider using the
insert()method onSQLiteDatabase, instead of executingINSERTstatements viaexecSQL(), particularly if you do not have much SQL experience.Second, normally, an
INSERTstatement lists the columns to be inserted into, which you are not doing. SQLite might support your syntax, but it makes for more fragile code.Third, with an
autoincrementcolumn, you do not assign your own values, as you are trying to do.Fourth, you are putting string values into
integercolumns. SQLite supports that, but not by actually converting the values to integers, but rather storing the strings themselves in the columns, which may not be what you want.Fifth,
getColumnIndexOrThrow()returns anint, not achar.Sixth, you are deleting your data from the table milliseconds after inserting it, in your
finallyblock, assuming thatSAMPLE_TABLE_NAMESis set totbl_product. If those values are not equal, then you are inserting into and querying from a table that does not exist.Seventh, your
Cursoris dead as soon as youclose()the database in yourfinallyblock.Some combination of those probably explains “the issue”.