android:为什么我得到错误插入Dato = null当Dato我设置允许null?

时间:2014-09-07 19:25:21

标签: java android sqlite

当我使用SQLLite时,我在android中遇到此错误:Dato = null,当我定义该列可以有空值时

我收到下一个错误:

09-07 18:35:59.585: E/SQLiteDatabase(572): Error inserting Dato=null
09-07 18:35:59.585: E/SQLiteDatabase(572): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at info.android.DataBaseManager.insertar(DataBaseManager.java:38)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at info.android.FindAllFragment$1.onClick(FindAllFragment.java:71)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.view.View.performClick(View.java:3511)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.view.View$PerformClick.run(View.java:14105)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.os.Handler.handleCallback(Handler.java:605)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.os.Looper.loop(Looper.java:137)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at java.lang.reflect.Method.invokeNative(Native Method)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at java.lang.reflect.Method.invoke(Method.java:511)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-07 18:35:59.585: E/SQLiteDatabase(572):  at dalvik.system.NativeStart.main(Native Method)

我的课程是:

public class DataBaseManager {
    public static final String TABLENAME = "config";

    public static final String CN_ID = "Id";
    public static final String CN_KEY = "Key";
    public static final String CN_DATO = "Dato";


    public static final String CREATE_TABLE = "create table " + TABLENAME + " ("
            + CN_ID + " integer primary key autoincrement, "
            + CN_KEY + " TEXT NOT NULL, "
            + CN_DATO + " TEXT"
            + ");";

    DBHelper helper = null;
    SQLiteDatabase db  = null;
    SQLiteDatabase dbread  = null;


    public DataBaseManager(Context  context) {
        helper = new DBHelper(context);     
        db = helper.getWritableDatabase();
        dbread = helper.getReadableDatabase(); 
    }

    public void insertar(String valor) {
        ContentValues cv = new ContentValues();


        cv.put(CN_DATO, valor);

        db.insert(TABLENAME, null, cv);
    }

    public void eliminar(String Key) {
        db.delete(TABLENAME, CN_KEY + "=?" , new String[] { Key }) ;
    }

    public void modificarDatoById(int Id, String valor) {
        ContentValues cv = new ContentValues();

        cv.put(CN_DATO, valor);

        db.update(TABLENAME, cv, CN_ID + "=?" , new String[] { valor });
    }


    public void modificarDatoByKey(String Key, String Valor) {
        ContentValues cv = new ContentValues();

        cv.put(CN_DATO, Valor);

        db.update(TABLENAME, cv, CN_KEY + "=?" , new String[] { Valor });
    }

    public void trashTable() 
    {
        db.execSQL("DELETE FROM " + TABLENAME);
    }

    public int countRowsTable() 
    {   

        Cursor cursor = dbread.query(TABLENAME, null, null, null, null, null, null);

        return cursor.getCount();
    }

    public String getDatoByKey(String Key) {
        String[] columnas = new String[] {CN_DATO};
        String res = null;

        Cursor cursor = dbread.query(TABLENAME, columnas, CN_KEY  + "=?", new String[] { Key }, null, null, null);

        cursor.moveToFirst();
        if (cursor.getCount() > 0) res = cursor.getString(0); 

        return res;
    }

    public boolean existKey(String Key) {
        String[] columnas = new String[] {CN_DATO};

        Cursor cursor = dbread.query(TABLENAME, columnas, CN_KEY  + "= ?", new String[] { Key }, null, null, null);

        return cursor.getCount() > 0;   
    }


}

我打电话给:

DataBaseManager db = new DataBaseManager(v.getContext());
db.insertar(null);

为什么我在Dato I设置允许null时收到错误插入Dato = null?

1 个答案:

答案 0 :(得分:1)

insertar()方法不会在Key列中插入任何内容,该列被声明为NOT NULL。