2016-05-15 11 views
3

私のデータベースに新しい項目を追加すると例外が発生しました。android.database.sqlite.SQLiteConstraintException:NOT NULL制約に失敗しました:albums.path(code 1299)

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

誰かお手伝いできますか?

答えて

8

あなたのデータベース定義に次のNOT NULL値が設定されています

CONTENT 
PATH 
VIDEO 
TIME 

しかし、それは間違いなくもPATHやビデオを探しているので、あなたのaddDBであなただけContentValuesでの内容と時間を置きます。それらが存在しない場合は、エラーが発生します。

解決方法1::2つの解決策があります

は、ビデオおよびPATHからNOT NULL制約を削除します。

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

解決方法2:

ContentValuesにVIDEOとPATHを追加します。

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); 
} 
+0

私はちょうどsqliteを学び始めたので、私はそのような間違いをします、ありがとう!問題は解決しました! –

+0

@KayeShine ok幸運、受け入れられたように答えを記入してください。 – Pooya

+0

@Pooya質問は答えを受け入れた後で閉じられません!心配無用。 –

1

すべてのフィールド(列)をNOT NULLと宣言し、cvを挿入している間に値を入力していないことを確認してください。これらの列をnullにするか、NOT NULLという列ごとにすべての値をcvに設定します。

+0

お返事ありがとうございます! –

+0

あなたは大歓迎です! –

関連する問題