Android 如何将数据一次性插入sqlite数据库

Android 如何将数据一次性插入sqlite数据库,android,sqlite,Android,Sqlite,我只需要向sqlite数据库添加一次数据。也就是说,我希望我的应用程序的用户将该数据视为已加载数据。如何做到这一点。 我使用查询来执行它 在表中插入名称值(值1、值2、值3、…值n) 但每次应用程序打开该页面时,都会一次又一次地添加产品。请帮助 代码 public class product_display extends Activity { SQLiteDatabase mydb; ListView prd_list; @Override protected void onCreate(B

我只需要向sqlite数据库添加一次数据。也就是说,我希望我的应用程序的用户将该数据视为已加载数据。如何做到这一点。 我使用查询来执行它

在表中插入名称值(值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);
            }
    });



}

}
公共类产品展示扩展活动{
sqlitemydb数据库;
ListView prd_列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.product_dispay);
prd_列表=(ListView)findViewById(R.id.list);
Intent in=getIntent();
Bundle Bundle=in.getExtras();
最终字符串列表=bundle.getString(“key”);
mydb=product\u display.this.openOrCreateDatabase(“products”,MODE\u PRIVATE,null);
mydb.execSQL(“创建不存在的表产品(pimage BLOB、pid INTEGER主键、pname文本、pprice NUMERIC、pspec文本、pfeature文本)”;
mydb.execSQL(“插入产品(pname、pprice、pspec)值('Candle stick 3',4000,'Solar garden/pathway light,太阳能电池板:1块晶体硅太阳能电池,材料:不锈钢,防水且安全”);
游标cr=mydb.rawQuery(“选择*来自产品”,null);
最终字符串[]pname=新字符串[cr.getCount()];
String[]price=新字符串[cr.getCount()];
int i=0;
while(cr.moveToNext())
{
字符串名称=cr.getString(cr.getColumnIndex(“pname”);
String prprice=cr.getString(cr.getColumnIndex(“pprice”);
pname[i]=名称;
价格[i]=价格;
i++;
}
ListAdapter=新的ListAdapter(此,pname,price);
prd_列表。设置适配器(适配器);
prd_list.setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
字符串nme=pname[arg2];
Bundle bun=新Bundle();
bun.putString(“键”,列表);
Bundle bn=新Bundle();
bn.putString(“名称”,nme);
Intent in=新的Intent(产品显示。此,产品详细信息。类);
in.putExtras(bun);
in.putExtras(bn);
星触觉(in);
}
});
}
}

假设您正在使用
SQLiteOpenHelper
管理数据库,请将插入内容放入helper
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) 从表中选择字段 (二)

3) 关闭连接


另外,很抱歉我的英语不好(我是意大利人)

请使用SharedPreference检查以前是否提交过数据。另一种解决方法是在sqlite中创建一个新表,并使用该表检查数据之前是否已插入。 新表格将被检查并插入。具有列id和用户id。
现在检查此表中是否有任何条目,如果没有,则可以运行您只希望第一次运行的查询

只需创建一个静态变量计数并将其初始化为0即可。在第一次插入后将计数器增加到1,并在插入前检查计数器是否为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++;
}

创建表时使列唯一..INSERT将始终插入新项,即使已存在。更新将更改现有行。或者您可以在插入之前检查是否存在。@Duggu我在插入数据时添加了代码的一部分,您在其中编写了一次性数据。但它甚至不是第一次插入数据。好的。但我对SQLiteOpenHelper一无所知。你能告诉我在哪里添加这部分代码吗。?或者我应该做一个新的java页面吗?请看下面的链接号。我不使用SqLeIn OpenHelp。我已经发布了我的代码。我不知道关于SqLeItOpenHelp。认真考虑使用<代码> SqLeLeOpenHelpP>代码>管理数据库生命周期。OKAY。让我查看SqLeLeOpenRelpor。但是,现在,我如何在代码中实现SqLeLeOpenHelpER?
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++;
}