2012-03-19 55 views
0

創建數據庫,當我試圖插入值後,我發現異常如下..的Android源碼:錯誤插入值:SQLite的約束例外

android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 

我不知道爲什麼會這樣.. 任何人都請幫助我.. Thanx提前... 主類和輔助類在下面給出..

public class Database extends Activity{ 

    public static final String MYDATABASE_NAMEEE = "MY_DATABASE_RESTAURANT"; 
    public static final int MYDATABASE_VERSION = 4; 
    public static final String MYDATABASE_TABLE = "Restaurant"; 
    public static final String KEY_ID = "_id"; 
    public static final String KEY_CONTENT1 = "Restaurant_name"; 
    public static final String KEY_CONTENT2 = "Ac_or_nonac"; 
    public static final String KEY_CONTENT3 = "Total_chairs"; 
    public static final String KEY_CONTENT4 = "Reserved_chairs"; 
    public static final String KEY_CONTENT5 = "Date"; 
    public static final String KEY_CONTENT6 = "fromTime"; 
    public static final String KEY_CONTENT7 = "toTime"; 
    public static final String KEY_CONTENT8 = "Name"; 
    public static final String KEY_CONTENT9 = "Contact_Number"; 
    public static final String KEY_CONTENT10 = "Table_id"; 

    private Helper helper; 
    private SQLiteDatabase database; 

    Button button_submit; 
    ListView listView; 
    EditText editText_name,editText_number,editText_seats,editText_date,editText_fromtime,editText_totime; 
    SimpleCursorAdapter cursorAdapter; 
    Cursor cursor; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.databaselist); 

     listView = (ListView) findViewById(R.id.listView1); 
     button_submit = (Button) findViewById(R.id.button_submit); 
     editText_date = (EditText) findViewById(R.id.editText_date); 
     editText_fromtime = (EditText) findViewById(R.id.editText_fromtime); 
     editText_name = (EditText) findViewById(R.id.editText_name); 
     editText_number = (EditText) findViewById(R.id.editText_contactnumber); 
     editText_seats = (EditText) findViewById(R.id.editText_seatsneeded); 
     editText_totime = (EditText) findViewById(R.id.editText_totime); 


     helper = new Helper(this, MYDATABASE_NAMEEE, null, MYDATABASE_VERSION); 
     database = helper.getWritableDatabase(); 

     cursor = database.query(MYDATABASE_TABLE, null,null, null, null, null, null); 
     String[] from=new String[]{KEY_ID,KEY_CONTENT3,KEY_CONTENT4,KEY_CONTENT5 
       ,KEY_CONTENT6,KEY_CONTENT7,KEY_CONTENT8,KEY_CONTENT9,KEY_CONTENT10}; 
     int[] to=new int[]{R.id.textView_id,R.id.textView_totalno_ofseats,R.id.textView_no_ofseatsreserved,R.id.textView_date 
      ,R.id.textView_timefrom,R.id.textView_totime,R.id.textView_name,R.id.textView_contactnumber,R.id.textView_tableid}; 
     cursorAdapter=new SimpleCursorAdapter(getApplicationContext(), R.layout.databasetext, cursor, from, to); 
     listView.setAdapter(cursorAdapter); 


     button_submit.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
      String date = editText_date.getText().toString(); 
      String fromtime = editText_fromtime.getText().toString(); 
      String name = editText_name.getText().toString(); 
      String number = editText_number.getText().toString(); 
      String seats = editText_seats.getText().toString(); 
      String totime = editText_totime.getText().toString(); 

      ContentValues cv1 = new ContentValues(); 
      cv1.put(KEY_CONTENT5, date); 
      cv1.put(KEY_CONTENT6, fromtime); 
      cv1.put(KEY_CONTENT8, name); 
      cv1.put(KEY_CONTENT9, number); 
      cv1.put(KEY_CONTENT4, seats); 
      database.insert(MYDATABASE_TABLE, null, cv1); 
      //cursor.requery(); 

      editText_date.setText(""); 
      editText_fromtime.setText(""); 
      editText_name.setText(""); 
      editText_number.setText(""); 
      editText_seats.setText(""); 
      editText_totime.setText(""); 
      filldata(); 

      } 
     }); 
    } 
    private void filldata() { 
     // TODO Auto-generated method stub 
     Cursor cursor = database.query(MYDATABASE_TABLE, null, 
       null, null, null, null,null); 

     String[] from=new String[]{KEY_ID,KEY_CONTENT3,KEY_CONTENT4,KEY_CONTENT5 
       ,KEY_CONTENT6,KEY_CONTENT7,KEY_CONTENT8,KEY_CONTENT9,KEY_CONTENT10}; 
     int[] to=new int[]{R.id.textView_id,R.id.textView_totalno_ofseats,R.id.textView_no_ofseatsreserved,R.id.textView_date 
      ,R.id.textView_timefrom,R.id.textView_totime,R.id.textView_name,R.id.textView_contactnumber,R.id.textView_tableid}; 
     cursorAdapter=new SimpleCursorAdapter(getApplicationContext(), R.layout.databasetext, cursor, from, to); 
     listView.setAdapter(cursorAdapter); 

    } 
} 




public class Helper extends SQLiteOpenHelper{ 

    public static final String MYDATABASE_TABLE = "Restaurant"; 
    public static final String KEY_ID = "_id"; 
    public static final String KEY_CONTENT1 = "Restaurant_name"; 
    public static final String KEY_CONTENT2 = "Ac_or_nonac"; 
    public static final String KEY_CONTENT3 = "Total_chairs"; 
    public static final String KEY_CONTENT4 = "Reserved_chairs"; 
    public static final String KEY_CONTENT5 = "Date"; 
    public static final String KEY_CONTENT6 = "fromTime"; 
    public static final String KEY_CONTENT7 = "toTime"; 
    public static final String KEY_CONTENT8 = "Name"; 
    public static final String KEY_CONTENT9 = "Contact_Number"; 
    public static final String KEY_CONTENT10 = "Table_id"; 


    private static final String SCRIPT_CREATE_DATABASE = 
      "create table " + MYDATABASE_TABLE + " (" 
      + KEY_ID + " integer primary key, " 
      + KEY_CONTENT1 + " text not null, " 
      + KEY_CONTENT2 + " text not null, " 
      + KEY_CONTENT3 + " integer not null, " 
      + KEY_CONTENT4 + " integer not null, " 
      + KEY_CONTENT5 + " text not null, " 
      + KEY_CONTENT6 + " text not null, " 
      + KEY_CONTENT7 + " text not null, " 
      + KEY_CONTENT8 + " text not null, " 
      + KEY_CONTENT9 + " text not null, " 
      + KEY_CONTENT10 + " text not null) "; 


    public Helper(Context context, String name, CursorFactory factory, 
      int version) { 
     super(context, name, factory, version); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(SCRIPT_CREATE_DATABASE); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

    } 
} 

回答

1

在數據庫架構,宣佈一切爲NOT NULL,但在你插入你不要沒有爲每個領域提供線索。 SQLite的拋出一個異常,因爲它不知道該怎麼插入的字段。刪除NOT NULL約束,使SQlite的可插入一個空值作爲默認或每個字段添加值。

0

確保您是不是要插入一些價值在那裏,不是唯一的。 確保你沒有嘗試在您的插入忽略「不空」的價值觀。提供非空列值或定義默認值。

在不同的說明中,您定義了KEY_CONTENT1等等,並且在2個不同的類中使用相同的值。不要重複。您正在使用一個靜態的背景下爲他們感到在一個類中定義它們,使用靜態上下文來獲取其他地方他們。

+0

謝謝flo,raju和Orlymee.I已經做出了改變,因爲你說過,但仍然有相同的錯誤.. – 2012-03-19 11:19:36

1

更正聲明領域數據庫..because的定義NOT NULL ..你必須發送一些數據每場..所以在不需要它

2

取出未從數據字段nul1要確定你正在插入一行有效的主鍵,即,使其唯一在表中(我認爲你有這個exeception只是因爲你試圖插入一行的哪個主鍵已被定義在相同的數據庫表)。

+0

這是我的情況...謝謝 – Juan 2015-05-26 13:30:34