如何一次性向sqlite数据库中插入数据
How to insert data into sqlite database just one time
我需要将数据添加到 sqlite 数据库中,只有一个 time.That 是我希望我的应用程序的用户将数据视为 perloaded.How 来执行此操作。
我使用查询
执行了它
插入 TABLE_NAME 值(值 1、值 2、值 3、...值 N);
但是每次应用程序打开该页面时,都会一次又一次地添加产品..请帮忙
代码
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);
}
});
}
}
假设您正在使用 SQLiteOpenHelper
来管理您的数据库,请将插入内容放入助手 onCreate()
回调中。当第一次创建数据库文件时,它只会被调用一次。请记住在作为参数传入的 SQLiteDatabase
上调用数据库方法。
像这样尝试:
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
}
}
您必须检查尚未在数据库中插入该值的循环 "if"。我会做:
1) 来自 table 的 select 个字段
2)
if (field1 != value) then
execute(INSERT INTO TABLE_NAME VALUES(value1,value2,value3,...valueN));
else{
// continues with the program
}
3) 关闭连接
P.S。对不起我的英语不好(我是意大利人)
使用sharedprefrence检查之前是否提交过数据。另一种解决方法可能是在 sqlite 中创建一个新的 table 并使用该 table 来检查之前是否已插入数据。
例如。新 table 为 check_insertion。使用列 ID 和 user_id。
现在检查此 table 中是否有任何条目,如果没有,那么您可以 运行 您希望成为 运行 的查询,但仅限第一次。
很简单,只需创建一个静态变量 count 并将其初始化为 0。然后在第一次插入后将 counter 递增到 1,并在插入前检查 counter 是否为 0。所以在你的情况下:
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++;
}
我需要将数据添加到 sqlite 数据库中,只有一个 time.That 是我希望我的应用程序的用户将数据视为 perloaded.How 来执行此操作。 我使用查询
执行了它插入 TABLE_NAME 值(值 1、值 2、值 3、...值 N);
但是每次应用程序打开该页面时,都会一次又一次地添加产品..请帮忙
代码
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);
}
});
}
}
假设您正在使用 SQLiteOpenHelper
来管理您的数据库,请将插入内容放入助手 onCreate()
回调中。当第一次创建数据库文件时,它只会被调用一次。请记住在作为参数传入的 SQLiteDatabase
上调用数据库方法。
像这样尝试:
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
}
}
您必须检查尚未在数据库中插入该值的循环 "if"。我会做: 1) 来自 table 的 select 个字段 2)
if (field1 != value) then
execute(INSERT INTO TABLE_NAME VALUES(value1,value2,value3,...valueN));
else{
// continues with the program
}
3) 关闭连接
P.S。对不起我的英语不好(我是意大利人)
使用sharedprefrence检查之前是否提交过数据。另一种解决方法可能是在 sqlite 中创建一个新的 table 并使用该 table 来检查之前是否已插入数据。 例如。新 table 为 check_insertion。使用列 ID 和 user_id。 现在检查此 table 中是否有任何条目,如果没有,那么您可以 运行 您希望成为 运行 的查询,但仅限第一次。
很简单,只需创建一个静态变量 count 并将其初始化为 0。然后在第一次插入后将 counter 递增到 1,并在插入前检查 counter 是否为 0。所以在你的情况下:
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++;
}