在Android上的SQlite中插入值时出错

时间:2012-12-17 16:22:47

标签: android sqlite runtime-error

这是错误...

12-17 17:17:30.729: E/SQLiteDatabase(21112): Error inserting nome=categoriaTest
12-17 17:17:30.729: E/SQLiteDatabase(21112): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1844)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1717)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at com.ggservice.democracy.pollDataSource.createCategoria(pollDataSource.java:40)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at com.ggservice.democracy.MainActivity.onClick(MainActivity.java:44)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at java.lang.reflect.Method.invoke(Method.java:511)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.view.View$1.onClick(View.java:3064)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.view.View.performClick(View.java:3591)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.view.View$PerformClick.run(View.java:14263)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.os.Handler.handleCallback(Handler.java:605)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.os.Looper.loop(Looper.java:137)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at android.app.ActivityThread.main(ActivityThread.java:4507)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at java.lang.reflect.Method.invoke(Method.java:511)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-17 17:17:30.729: E/SQLiteDatabase(21112):    at dalvik.system.NativeStart.main(Native Method)

虽然,应该这样做的按钮就像:

public void onClick(View view) {
        @SuppressWarnings("unchecked")
        ArrayAdapter<categorie> adapter = (ArrayAdapter<categorie>) getListAdapter();
        categorie categoria = null;
        switch (view.getId()) {
        case R.id.add:

          // Save the new comment to the database
          categoria = datasource.createCategoria("categoriaTest");
          adapter.add(categoria);
          break;

        }
        adapter.notifyDataSetChanged();
      }

,createCategoria就像:

public categorie createCategoria(String categoria) {
            ContentValues values = new ContentValues();
            values.put(MySQLiteHelper.COLUMN_NOME, categoria);
            long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null,
                values);
            Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
                allCategorieColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
            cursor.moveToFirst();
            categorie newCategoria = cursorToCategorie(cursor);
            cursor.close();
            return newCategoria;
          } 

SQliteHelper:

public class MySQLiteHelper extends SQLiteOpenHelper {

      public static final String TABLE_CATEGORIE = "categorie";
      public static final String TABLE_SONDAGGI = "sondaggi";
      public static final String TABLE_TESTORISPOSTE = "testo_risposte";
      public static final String TABLE_RISPOSTE = "risposte";    
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_NOME = "nome";
      public static final String COLUMN_PREF = "preferita"; 
      public static final String COLUMN_CATID = "catid";
      public static final String COLUMN_DOMANDA = "domanda";
      public static final String COLUMN_SONDID = "sondid";
      public static final String COLUMN_RISPOSTA = "risposta";
      public static final String COLUMN_RISID = "risid";
      public static final String COLUMN_SELEZIONATA = "selezionata";


      private static final String DATABASE_NAME = "democracy.db";
      private static final int DATABASE_VERSION = 2;

      // Database creation sql statement


      private static final String DATABASE_CREATE_CATEGORIE = "create table "
          + TABLE_CATEGORIE + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_PREF
          + " integer not null, " + COLUMN_NOME
          + " text not null);";

      private static final String DATABASE_CREATE_SONDAGGI = "create table "
              + TABLE_SONDAGGI + "(" + COLUMN_ID
              + " integer primary key autoincrement, " + COLUMN_CATID
              + " integer not null, "+ COLUMN_DOMANDA +" text not null);";


      private static final String DATABASE_CREATE_TESTORISPOSTE = "create table "
              + TABLE_TESTORISPOSTE + "(" + COLUMN_ID
              + " integer primary key autoincrement, " + COLUMN_SONDID
              + " integer not null, "+ COLUMN_SELEZIONATA
              + " integer not null, "+ COLUMN_RISPOSTA +" text not null);";


      public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE_CATEGORIE);
        database.execSQL(DATABASE_CREATE_SONDAGGI);
        database.execSQL(DATABASE_CREATE_TESTORISPOSTE);


      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONDAGGI);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_TESTORISPOSTE);



        onCreate(db);
      }

    } 

我不知道你是否还需要更多东西,我在android上是一个相当的菜鸟,但我想要了解,我实际上是想通过添加实际应用中的“转换”教程更多的桌子,制作一个好的DAO等等...... 你有什么建议吗? 附:有没有办法“看”我可能“在设备中”创建的数据库?

1 个答案:

答案 0 :(得分:1)

问题是您的列COLUMN_PREF不能为空,并且您通过不插入任何内容在方法中插入null:

COLUMN_PREF + " integer not null,"

-

ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NOME, categoria);
long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null, values);

只需将该值添加到插入或删除'not null'子句。