【问题标题】:How to insert data into sqlite database just one time如何一次将数据插入sqlite数据库
【发布时间】:2017-12-04 14:08:04
【问题描述】:

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

INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);

但是每次应用打开该页面时,都会一次又一次地添加产品..请帮助

代码

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);
            }
    });



}

}

【问题讨论】:

标签: android sqlite


【解决方案1】:

试试这样:

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

    }

}

【讨论】:

【解决方案2】:

假设您使用SQLiteOpenHelper 来管理您的数据库,请将插入放在帮助器onCreate() 回调中。当第一次创建数据库文件时,它只会被调用一次。只要记住在作为参数传入的SQLiteDatabase 上调用数据库方法即可。

【讨论】:

  • 不。我没有使用 SQLiteOpenHelper。我已经发布了我的代码。我不知道 SQLiteOpenHelper。
  • 认真考虑使用SQLiteOpenHelper 来管理您的数据库生命周期。
  • 好的。让我看看 SQLiteOpenHelper。但是现在,我怎样才能在我的代码中实现 SQLiteOpenHelper?
【解决方案3】:

您必须使用尚未在数据库中插入该值的循环“if”进行检查。我会做: 1)从表中选择字段 2)

if (field1 != value) then
       execute(INSERT INTO TABLE_NAME VALUES(value1,value2,value3,...valueN));

else{

 // continues with the program
}

3) 关闭连接

附:对不起我的英语不好(我是意大利人)

【讨论】:

    【解决方案4】:

    使用 sharedprefrence 检查数据之前是否已提交。另一种解决方法可能是在 sqlite 中创建一个新表并使用该表检查之前是否已插入数据。 例如。新表为 check_insertion。使用列 id 和 user_id。 现在检查此表中是否有任何条目,如果没有,那么您可以运行您只想第一次运行的查询。

    【讨论】:

      【解决方案5】:

      这很简单,只需创建一个静态变量 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++;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-20
        • 2014-02-23
        • 1970-01-01
        • 1970-01-01
        • 2018-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多