Reputation: 227
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);
}
});
}
}
Upvotes: 3
Views: 8775
Reputation: 126
It's simple just create a static variable count and initialize it to 0. And increment counter to 1 after first insertion and also checks if counter is 0 before insertion. So in Your Case :
static int count = 0;
if(count == 0){
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 ')");
count++;
}
Upvotes: 1
Reputation: 1945
Use sharedprefrence to check if data has been submitted before. Another workaround may be make a new table in sqlite and use that table to check if data have been inserted before. Eg. New table be check_insertion. With column id and user_id. Now check if in this table there is any entry if not then you may run your query which you want to be ran first time only.
Upvotes: 0
Reputation: 1
you must check with a cycle "if" that is not already inserted that value in the database. I would do: 1) select fields from table 2)
if (field1 != value) then
execute(INSERT INTO TABLE_NAME VALUES(value1,value2,value3,...valueN));
else{
// continues with the program
}
3) close the connection
P.S. sorry for my bad english(I'm italian)
Upvotes: 0
Reputation: 2577
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
}
}
Upvotes: 2
Reputation: 152927
Assuming you're using SQLiteOpenHelper
to manage your database, put the inserts in the helper onCreate()
callback. It gets invoked exactly once when the database file is created for the first time. Just remember to call the database methods on the SQLiteDatabase
passed in as a parameter.
Upvotes: 0