我的sqlite db接受空值,我不知道为什么。我将我的表作为“主键”,将我的列作为“Text Not Null”。 Db不应接受null,非唯一值和大于第1行的插入。
DB:
public void onCreate(SQLiteDatabase db) {
String sqlDataStore = "create table if not exists " +
TABLE_NAME_CREDENTIALS + " ("+ BaseColumns._ID + " integer primary key ,"
+ COLUMN_NAME_USERNAME + " text not null unique,"
+ COLUMN_NAME_PASSWORD + " text not null unique);";
db.execSQL(sqlDataStore);
}
public static boolean Login(String username, String password) {
Boolean Login = true;
Cursor c = db.rawQuery(
"SELECT * FROM " + TABLE_NAME_CREDENTIALS + " WHERE "
+ COLUMN_NAME_USERNAME + " = '" + username +"' AND "+ COLUMN_NAME_PASSWORD +" = '"+ password +"'" , null);
if (c.getCount() > 0) {
Login = false;
}
c.close();
return Login;
}
插入方法:
private void insertCredentials(RegDetails regDetails){
LoginDB androidOpenDBHelper = new LoginDB(this);
SQLiteDatabase sqliteDB = androidOpenDBHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(LoginDB.COLUMN_NAME_USERNAME, rUsername);
contentValues.put(LoginDB.COLUMN_NAME_PASSWORD, rPasscode);
long affectedColumnid = sqliteDB.insert(LoginDB.TABLE_NAME_CREDENTIALS, null, contentValues);
if(affectedColumnid > 1){
Toast.makeText(getApplicationContext(), "Account Already Exist! Please login" + affectedColumnid, Toast.LENGTH_SHORT).show();
finish();
}else {
Toast.makeText(getApplicationContext(), "Credentials Saved! Please login" + affectedColumnid, Toast.LENGTH_SHORT).show();
}
sqliteDB.close();
finish();
}
}
答案 0 :(得分:0)
这是因为您没有使用SQLite网站上描述的ON CONFLICT FAIL:http://www.sqlite.org/lang_conflict.html
如果您想要查询示例,可以查看上一个问题:NOT NULL columns in SQLite and error catching
希望这会对你有所帮助。
Side注意:对于构建long String,最好使用StringBuilder。