mardi 20 janvier 2015

Database using SQLiteOpenHelper Class accessing in main activity

I need to Preload some data in a sqlite database.I tried using Insert query.But it is inserting data everytime I open app.My searches made me understand SQLiteOpenHelper is the best option.But I am a beginner and I have no idea about SQLiteOpenHelper.I dont know how to implement it.I wrote this code.But how to access this database in my main activity.java


DataBaseHelper.java



public class DataBaseHandler extends SQLiteOpenHelper{

public static final String DATABASE_NAME = "products.db";
public static final String CONTACTS_TABLE_NAME = "product";
public static final String CONTACTS_COLUMN_ID = "pid";
public static final String CONTACTS_COLUMN_NAME = "pname";
public static final String CONTACTS_COLUMN_EMAIL = "pspec";
public static final String CONTACTS_COLUMN_STREET = "pprice";
public static final String CONTACTS_COLUMN_CITY = "pfeature";
public static final String CONTACTS_COLUMN_PHONE = "pimage";

public DataBaseHandler(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 Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS product(pimage BLOB,pid INTEGER PRIMARY KEY,pname TEXT,pprice NUMERIC,pspec TEXT,pfeature TEXT)");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 3',4000,'Solar garden / pathway light,Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA ,Material:Stainless steel ,WaterProof and safe ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 4',4500,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 5',3500,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Candle stick 6',6000,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('lawn Delight',8800,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('SGLC10',4500,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Senson',4500,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
db.execSQL("INSERT INTO product(pname,pprice,pspec) VALUES('Thejus',7500,'Solar garden / pathway light, Solar Panel:1pc crystal silicon solar cell, Battery:1pc 1.2V Ni-MH/Ni-CD AA battery 600MA, Material:Stainless steel, WaterProof and safe IP44 ')");
}

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

}

}


Product_display.java



public class product_display extends Activity {

SQLiteDatabase db;

ListView prd_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_disply);
prd_list = (ListView) findViewById(R.id.list);



Intent in=getIntent();
Bundle bundle=in.getExtras();
final String list=bundle.getString("key");

db = product_display.this.openOrCreateDatabase("products", MODE_PRIVATE, null);


Cursor cr = db.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 com.example.login_signup.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);
}

});



}

}

Aucun commentaire:

Enregistrer un commentaire