我的問題是關於我的數據庫插入語句。我添加了一個參數來插入語句,現在每次我嘗試添加一個人時,它都會給出這個錯誤。我已經改變了數據庫版本,但仍然給我一個「數據不匹配」的錯誤。我添加了這個參數插入語句beaucaus它沒有保存ID(字符串),每次我註銷時,我所做的(用戶名+密碼)的ID在一個整數更改..所以我想我的數據庫認爲該ID整數是但我想它作爲一個字符串。我其實認爲每種類型都是對的。錯誤插入android.database.sqlite.SQLiteDatatypeMismatchException:數據類型不匹配(代碼20)
這是我的代碼,感謝您的幫助!
數據庫:
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper instance;
//Database Versie
public static final int database_version = 3;
//Database Naam
public static final String DATABASE_NAME = "Biercafevlaanderen.db";
//Tabel Namen
public static final String TABLE_NAME_PROFIELEN = "profielen";
public static final String TABLE_NAME_BIERCAFES = "biercafes";
//Kollom namen
// voor tabel profiles
public static String COL_1_PROFIELEN= "profile_id";
public static final String COL_2_PROFIELEN= "username";
public static final String COL_3_PROFIELEN = "firstname";
public static final String COL_4_PROFIELEN = "email";
public static final String COL_5_PROFIELEN = "password";
//voor tabel
public static final String COL_1_BIERCAFES = "biercafe_id";
public static final String COL_2_BIERCAFES = "biercafe_name";
public static final String COL_3_BIERCAFES = "biercafe_postcode";
public static final String COL_4_BIERCAFES = "biercafe_plaats";
public static final String COL_5_BIERCAFES = "biercafe_gemeente";
public static final String COL_6_BIERCAFES = "biercafe_omschrijving";
public static final String COL_7_BIERCAFES = "biercafe_phone";
public static final String COL_8_BIERCAFES = "biercafe_website";
public static final String COL_9_BIERCAFES = "biercafe_email";
public static final String COL_10_BIERCAFES = "profielid";
public static synchronized DatabaseHelper getInstance(Context context) {
if (instance == null) {
instance = new DatabaseHelper(context.getApplicationContext());
}
return instance;
}
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, database_version);
SQLiteDatabase db = this.getWritableDatabase();
}
//HERE FALLS THE ERROR
public int insertProfiel (String id,String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1_PROFIELEN,id);
contentValues.put(COL_2_PROFIELEN, username);
contentValues.put(COL_5_PROFIELEN, password);
// de insert methode geeft -1 terug als het niet gelukt is en de row value als het wel gelukt is
long result = db.insert(TABLE_NAME_PROFIELEN, null, contentValues);
return (int)result;
}
@Override
public void onCreate(SQLiteDatabase db) {
// de tabel creëren voor de profielen
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_PROFIELEN + " (" + COL_1_PROFIELEN + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_2_PROFIELEN + " VARCHAR(255), " + COL_3_PROFIELEN + " VARCHAR(255), " + COL_4_PROFIELEN + " VARCHAR(255), " + COL_5_PROFIELEN + " VARCHAR(255))");
// de tabel creëren voor de locaties met foreign key profile_id
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_BIERCAFES + " (" + COL_1_BIERCAFES + " INTEGER PRIMARY KEY, " + COL_2_BIERCAFES + " VARCHAR(255), " + COL_3_BIERCAFES + " VARCHAR(255), " + COL_4_BIERCAFES + " VARCHAR(255), " + COL_5_BIERCAFES + " VARCHAR(255), " + COL_6_BIERCAFES +
" VARCHAR(255), " + COL_7_BIERCAFES + " VARCHAR(255), " + COL_8_BIERCAFES + " VARCHAR(255), " + COL_9_BIERCAFES + " VARCHAR(255), " + COL_10_BIERCAFES + " VARCHAR(255), FOREIGN KEY (" + COL_10_BIERCAFES + ") REFERENCES " + TABLE_NAME_PROFIELEN + "(" + COL_1_PROFIELEN + "))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_PROFIELEN);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_BIERCAFES);
onCreate(db);
}
}
這是我在DB 註冊插入用戶:
Profiel p = new Profiel(etUsername.getText().toString()+""+etPassword.getText().toString(),etUsername.getText().toString(),"","",etConfPass.getText().toString());
mListener.getMyDB().insertProfiel(p.getId(),p.getUsername(), p.getPassword());
System.out.println(mListener.getMyDB().insertProfiel(p.getId(),p.getUsername(), p.getPassword()));
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("profiel", p.toJson());
Snackbar.make(v, "Registered succesfully", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startActivity(intent);
getActivity().finish();
如果需要其他relavent班,問我。 這是錯誤:
E/SQLiteLog: (20) statement aborts at 5: [INSERT INTO profielen(password,username,profile_id) VALUES (?,?,?)] datatype mismatch
E/SQLiteDatabase: Error inserting password=zzz username=zzz profile_id=zzzzzz
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
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:1474)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.cedri.bcv.DB.DatabaseHelper.insertProfiel(DatabaseHelper.java:144)
at com.example.cedri.bcv.Fragments.RegisterFragment$2.onClick(RegisterFragment.java:66)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
的insertstatement給了我這樣的:
I/System.out: -1
感謝您的幫助!
但我想它作爲一個字符串,因爲我的ID =用戶名+密碼,這個組合是一個字符串,我在哪裏說ID必須是一個整數? – Peter
我相信你應該保持原樣(INTEGER PRIMARY KEY AUTOINCREMENT),因爲這不是你正在嘗試做的一個好習慣。您正試圖在您的數據庫上創建冗餘數據,因爲不需要將兩個字段放在另一個字段中,仍然保留所有這些字段。 –
非常感謝!我改變了我的第一列的類型,現在它的話:)! – Peter