Android sqlite:插入值时出错:sqlite约束异常
在创建数据库之后,当我尝试插入值时,我发现异常如下Android sqlite:插入值时出错:sqlite约束异常,android,sql,Android,Sql,在创建数据库之后,当我尝试插入值时,我发现异常如下 android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 我不知道为什么会这样。。 任何人都请帮帮我。。 Thanx提前。。。 下面给出了主类和辅助类 public class Database extends Activity{ public static final String MYDATABASE_NAMEEE =
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
}
}
在数据库模式中,您将所有内容声明为非NULL,但在insert中,您并没有为每个字段都指定值。SQLite抛出异常,因为它不知道要在字段中插入什么。删除NOTNULL约束,以便SQlite可以插入一个空值作为默认值,或为每个字段添加一个值。在数据库架构中,您将所有内容声明为NOTNULL,但在insert中,您没有为每个字段添加值。SQLite抛出异常,因为它不知道要在字段中插入什么。删除NOTNULL约束,以便SQlite可以插入一个空值作为默认值,或者为每个字段添加一个值。确保您没有尝试在其中插入一些非唯一的值。 确保您没有试图跳过插入中的任何“NOTNULL”值。提供非空列的值或定义默认值
另一方面,您在两个具有相同值的不同类中定义KEY_CONTENT1等。不要重复。您正在为它们使用静态上下文,因此在一个类中定义它们,并在其他地方使用静态上下文检索它们 确保您没有试图在其中插入非唯一的值。 确保您没有试图跳过插入中的任何“NOTNULL”值。提供非空列的值或定义默认值
另一方面,您在两个具有相同值的不同类中定义KEY_CONTENT1等。不要重复。您正在为它们使用静态上下文,因此在一个类中定义它们,并在其他地方使用静态上下文检索它们 更正数据库的定义。。因为在将字段声明为非空时,。必须向每个字段发送一些数据。。因此,将not nul1从不需要它的数据字段中删除更正数据库的定义..因为在将字段声明为not null时。必须向每个字段发送一些数据。。因此,将not nul1从不需要的数据字段中删除请确保插入的行具有有效主键,也就是说,在表中使其唯一(我认为您之所以有此执行,是因为您尝试插入的行主键已在同一数据库表中定义).请确保插入的行具有有效主键,即,在表中使其唯一(我认为您执行此执行只是因为您尝试插入的行主键已在同一数据库表中定义)。谢谢,raju和Orlymee.我已经按照你说的做了更改,但是仍然有相同的错误..谢谢你,flo,raju和Orlymee.我已经按照你说的做了更改,但是仍然有相同的错误。。