私の sqlite db は null 値を受け入れていますが、その理由がわかりません。テーブルを「主キー」として、列を「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();
}
}