如何将数据插入到SQLite数据库只是一个时间 [英] How to insert data into sqlite database just one time
本文介绍了如何将数据插入到SQLite数据库只是一个时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将数据添加到SQLite数据库只是一个time.That是我想要我的应用程序的用户看到的数据作为perloaded.How做到这一点。
我使用的查询执行它。
INSERT INTO TABLE_NAME VALUES(值1,值2,值3,...值N);
但每次应用打开该网页时,连连加products..Please帮助
code
公共类product_display延伸活动{SQLiteDatabase MYDB;
ListView的prd_list;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.product_dispay);
prd_list =(ListView控件)findViewById(R.id.list); 在意向= getIntent();
捆绑包= in.getExtras();
最终字符串列表= bundle.getString(钥匙);
MYDB = product_display.this.openOrCreateDatabase(产品,MODE_PRIVATE,NULL);
mydb.execSQL(CREATE TABLE IF NOT EXISTS产品(pimage BLOB,PID INTEGER PRIMARY KEY,PNAME TEXT,pprice NUMERIC,PSPEC TEXT,pfeature TEXT));
mydb.execSQL(INSERT INTO产品(PNAME,pprice,PSPEC)VALUES('蜡烛坚持3,4000,太阳能庭院灯/通路灯,太阳能电池板:1块晶体硅太阳能电池,材质:不锈钢,防水,安全 )); 光标CR = mydb.rawQuery(SELECT * FROM产品,NULL);
最终的String [] = PNAME新的String [cr.getCount()];
的String []价格=新的String [cr.getCount()]; INT I = 0;
而(cr.moveToNext())
{
字符串名称= cr.getString(cr.getColumnIndex(PNAME));
串prprice = cr.getString(cr.getColumnIndex(pprice));
PNAME [I] =名称;
价格由[i] = prprice;
我++;
} ListAdapter适配器=新ListAdapter(这一点,PNAME,价格);
prd_list.setAdapter(适配器); prd_list.setOnItemClickListener(新OnItemClickListener(){ 公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
// TODO自动生成方法存根
字符串NME = PNAME [ARG2]
捆绑馍=新包();
bun.putString(钥匙,清单);
捆绑亿=新包();
bn.putString(名,NME);
在意向=新意图(product_display.this,Product_Details.class);
in.putExtras(BUN);
in.putExtras(BN);
startActivity(在);
}
});}}
解决方案
尝试这样的:
公共类TestDatabaseHelper扩展SQLiteOpenHelper { 公共TestDatabaseHelper(上下文的背景下,字符串名称,
CursorFactory工厂,诠释的版本){
超(背景下,名称,厂家,版本);
// TODO自动生成构造函数存根
} @覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO创建表在这里
//这里插入pre装载记录 } @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根 }}
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);
}
});
}
}
解决方案
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
}
}
这篇关于如何将数据插入到SQLite数据库只是一个时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文