I need to add data to a sqlite database just one time.That is I want users of my app see that data as perloaded.How to do that. I performed it using the query
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
But each time app opens that page it,again and again add the products..Please help
code
public class product_display extends Activity {
SQLiteDatabase mydb;
ListView prd_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dispay);
prd_list = (ListView) findViewById(R.id.list);
Intent in=getIntent();
Bundle bundle=in.getExtras();
final String list=bundle.getString("key");
mydb = product_display.this.openOrCreateDatabase("products", MODE_PRIVATE, null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS product(pimage BLOB,pid INTEGER PRIMARY KEY,pname TEXT,pprice NUMERIC,pspec TEXT,pfeature TEXT)");
mydb.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 3',4000,'Solar garden / pathway light,Solar Panel:1pc crystal silicon solar cell,Material:Stainless steel ,WaterProof and safe ')");
Cursor cr = mydb.rawQuery("SELECT * FROM product", null);
final String[] pname = new String[cr.getCount()];
String[] price = new String[cr.getCount()];
int i = 0;
while(cr.moveToNext())
{
String name = cr.getString(cr.getColumnIndex("pname"));
String prprice = cr.getString(cr.getColumnIndex("pprice"));
pname[i] = name;
price[i] = prprice;
i++;
}
ListAdapter adapter = new ListAdapter(this, pname, price);
prd_list.setAdapter(adapter);
prd_list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String nme = pname[arg2];
Bundle bun = new Bundle();
bun.putString("key",list);
Bundle bn = new Bundle();
bn.putString("name",nme);
Intent in = new Intent(product_display.this,Product_Details.class);
in.putExtras(bun);
in.putExtras(bn);
startActivity(in);
}
});
}
}