本文介绍了android.database.sqlite.SQLiteConstraintException:NOT NULL约束失败:albums.path(代码1299)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向数据库中添加新项目时,我的应用程序发生了异常.

An exception happened in my app when I add a new item into my database.

Error inserting time=2016年05月14日 23:42:03 content=zxdc
    android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: albums.path (code 1299)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:780)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
        at com.driver.shinekaye.olddriver.AddContent.addDB(AddContent.java:53)
        at com.driver.shinekaye.olddriver.AddContent.onClick(AddContent.java:70)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的适配器:

public class MyAdapter extends BaseAdapter {

    private Context context;
    private Cursor cursor;
    private LinearLayout layout;

    public MyAdapter(Context context, Cursor cursor) {
        this.context = context;
        this.cursor = cursor;

    }

    @Override
    public int getCount() {
        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {
        return cursor.getPosition();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = (LinearLayout) inflater.inflate(R.layout.activity_secretalbum_item, null);
        TextView contentTv = (TextView) layout.findViewById(R.id.list_content);
        TextView timeTv = (TextView) layout.findViewById(R.id.list_time);
        ImageView imgIv = (ImageView) layout.findViewById(R.id.list_img);
        ImageView videoIv = (ImageView) layout.findViewById(R.id.list_video);
        cursor.moveToPosition(position);
        String content = cursor.getString(cursor.getColumnIndex("content"));
        String time = cursor.getString(cursor.getColumnIndex("time"));
        contentTv.setText(content);
        timeTv.setText(time);

        return layout;
    }
}

表格:

public class SecretAlbumDB extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "albums";
    public static final String CONTENT = "content";
    public static final String PATH = "path";
    public static final String VIDEO = "video";
    public static final String ID = "_id";
    public static final String TIME = "time";

    public SecretAlbumDB(Context context) {
        super(context, "albums", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT
                + " TEXT NOT NULL," + PATH
                + " TEXT NOT NULL," + VIDEO
                + " TEXT NOT NULL," + TIME
                + " TEXT NOT NULL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

查询

 public void selectDB() {
    Cursor cursor = dbReader.query(SecretAlbumDB.TABLE_NAME, null, null, null, null, null, null);
    adapter = new MyAdapter(this, cursor);
    list.setAdapter(adapter);

插入内容:

private void addDB() {
    ContentValues cv = new ContentValues();
    cv.put(SecretAlbumDB.CONTENT, editText.getText().toString());
    cv.put(SecretAlbumDB.TIME, getTime());
    dbWriter.insert(SecretAlbumDB.TABLE_NAME, null, cv);
}

有人可以帮我吗?

推荐答案

您的数据库定义中具有以下NOT NULL值:

You have the following NOT NULL values in your database definition:

CONTENT
PATH
VIDEO
TIME

但是在您的addDB中,您仅将CONTENT和TIME放在ContentValues中,因此它肯定也在寻找PATH和VIDEO.如果它们不存在,它将给出错误.有两种解决方法:

But in your addDB you only put CONTENT and TIME in ContentValues so it definitely looking for PATH and VIDEO too. If they aren't present, it will give the error. There are two solution:

解决方案1:

从视频和路径中删除NOT NULL约束:

remove NOT NULL constraint from VIDEO and PATH:

        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT
            + " TEXT NOT NULL," + PATH
            + " TEXT," + VIDEO
            + " TEXT," + TIME
            + " TEXT NOT NULL)");

解决方案2:

将VIDEO和PATH添加到ContentValues:

add VIDEO and PATH to ContentValues:

private void addDB() {
    ContentValues cv = new ContentValues();
    cv.put(SecretAlbumDB.CONTENT, editText.getText().toString());
    cv.put(SecretAlbumDB.TIME, getTime());
    cv.put(SecretAlbumDB.VIDEO, getVideo());
    cv.put(SecretAlbumDB.Path, getPath());

    dbWriter.insert(SecretAlbumDB.TABLE_NAME, null, cv);
}

这篇关于android.database.sqlite.SQLiteConstraintException:NOT NULL约束失败:albums.path(代码1299)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:03