android albums,android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: albums...

问题

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)

My adapter:

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;

}

}

The table:

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) {

}

}

The query

public void selectDB() {

Cursor cursor = dbReader.query(SecretAlbumDB.TABLE_NAME, null, null, null, null, null, null);

adapter = new MyAdapter(this, cursor);

list.setAdapter(adapter);

The insert:

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

}

Could someone please help me?

回答1:

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

CONTENT

PATH

VIDEO

TIME

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:

Solution 1:

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

Solution 2:

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

}

回答2:

For sure you have declared your all fields (columns) as NOT NULL and you've not put the value to those fields while inserting cv. Either make those columns null acceptable or put all the values to cv for every Columns that are NOT NULL.

来源:https://stackoverflow.com/questions/37234433/android-database-sqlite-sqliteconstraintexception-not-null-constraint-failed-a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值