【问题标题】:SQLite db duplicate values AndroidSQLite db重复值Android
【发布时间】:2023-03-12 01:20:01
【问题描述】:

我在我的应用程序中使用了spinner,它从 SQLite 数据库中获取值。问题是每次我从 Eclipse 运行应用程序时,值都会一次又一次地加载。所以如果我第一次在spinner 中有 5 个值,第二次我有 10 个值,然后是 15,然后是 20 等等。我该如何解决这个问题?

这是数据库部分:

在onCreate:

createTable();
        insertIntoTable();

        ArrayList<String> my_array = new ArrayList<String>();
        my_array = getTableValues();

        My_spinner = (Spinner) findViewById(R.id.scelte);
        My_spinner.setOnItemSelectedListener(this);
        ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
        My_spinner.setAdapter(my_Adapter);

然后:

// CREATE TABLE IF NOT EXISTS
    public void createTable() {
        try {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
            mydb.execSQL("CREATE TABLE IF  NOT EXISTS " + TABLE
                    + " (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);");
            mydb.close();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Errore durante la creazione della tabella",
                    Toast.LENGTH_LONG);
        }
    }

    // THIS FUNCTION INSERTS DATA TO THE DATABASE
    public void insertIntoTable() {
        try {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Cibo e Bevande','cibo')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Trasporti','trasporti')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Svago','svago')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Acquisti vari','acquisti')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Sanità','sanità')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Altro','altro')");
            mydb.close();


        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "Error in inserting into table", Toast.LENGTH_LONG);
        }
    }

    // THIS FUNCTION SHOWS DATA FROM THE DATABASE
    public ArrayList<String> getTableValues() {

        ArrayList<String> my_array = new ArrayList<String>();
        try {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
            Cursor allrows = mydb.rawQuery("SELECT * FROM " + TABLE, null);
            System.out.println("COUNT : " + allrows.getCount());

            if (allrows.moveToFirst()) {
                do {

                    String ID = allrows.getString(0);
                    String NAME = allrows.getString(1);
                    String PLACE = allrows.getString(2);
                    my_array.add(NAME);


                } while (allrows.moveToNext());
            }
            allrows.close();
            mydb.close();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Errore.",
                    Toast.LENGTH_LONG);
        }
        return my_array;

    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        String selecteditem = My_spinner.getSelectedItem().toString();
        TextView categorietext = (TextView) findViewById(R.id.sceltelabel);
        categorietext.setText(selecteditem);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

【问题讨论】:

  • 在将数据插入表之前进行检查。您可以1.查询DB,如果为空则插入,或者2.使用SharedPreference表示DB是否已经初始化。

标签: java android android-sqlite android-spinner


【解决方案1】:

你运行这个

insertIntoTable();

每次在onCreate 中都会添加越来越多的数据。

您应该使用扩展SQLiteOpenHelper 类并使用onCreate 方法来创建您的数据库和初始化表。见Android developer reference。

【讨论】:

【解决方案2】:

每次您打开活动时,onCreate 活动方法都会被调用,而在您的 onCreate 方法中,您将调用 insertIntoTable();,它每次都会插入新记录。

【讨论】:

    【解决方案3】:

    每次运行您的应用程序时,您都会调用insertintotable() 方法,所以它正在发生

    解决这个问题:

    检查表是否为空。如果为空则调用insertintotable()方法否则不要

    【讨论】:

      【解决方案4】:

      每次运行应用程序时,都会调用onCreate(),因此也会调用insertIntoTable()。您必须在插入数据之前添加检查。其中一种方法是在插入数据之前检查数据库是否为空。

      public void insertIntoTable() {
          try {
              mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
              Cursor c = mydb.query(TABLE, null, null, null, null, null, null);
              if (c == null || c.getCount() == 0) {
                  mydb.execSQL("INSERT INTO " + TABLE
                      + "(NAME, PLACE) VALUES('Cibo e Bevande','cibo')");
                  mydb.execSQL("INSERT INTO " + TABLE
                  + "(NAME, PLACE) VALUES('Trasporti','trasporti')");
                  mydb.execSQL("INSERT INTO " + TABLE
                  + "(NAME, PLACE) VALUES('Svago','svago')");
                  mydb.execSQL("INSERT INTO " + TABLE
                  + "(NAME, PLACE) VALUES('Acquisti vari','acquisti')");
                  mydb.execSQL("INSERT INTO " + TABLE
                  + "(NAME, PLACE) VALUES('Sanità','sanità')");
                  mydb.execSQL("INSERT INTO " + TABLE
                  + "(NAME, PLACE) VALUES('Altro','altro')");
                  mydb.close();
              }
              c.close();
          } catch (Exception e) {
              Toast.makeText(getApplicationContext(),
                  "Error in inserting into table", Toast.LENGTH_LONG);
          }
      }
      

      【讨论】:

      • The method query(String, String[], String, String[], String, String, String) in the type SQLiteDatabase is not applicable for the arguments (String, null, null, null, null, null) :(
      • 我加了null,现在我试试
      • 抱歉错字!很高兴它奏效了。如果可能,请在您认为正确时标记答案以表明问题已解决。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2020-10-11
      相关资源
      最近更新 更多