Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert data into sqlite database just one time

Tags:

android

sqlite

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



}

}
like image 313
panda_heart_3 Avatar asked Feb 23 '26 12:02

panda_heart_3


1 Answers

try Like this:

public class TestDatabaseHelper extends SQLiteOpenHelper{

    public TestDatabaseHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Create Your tables here
        //and insert the pre loaded records here

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}
like image 91
No Name Avatar answered Feb 26 '26 03:02

No Name