我的问题是关于我的数据库插入语句。我在te插入语句中添加了一个参数,现在每次我尝试添加一个人时它都会给我这个错误。我已经改变了数据库版本,但它仍然给我一个"数据不匹配"错误。我把这个参数添加到插入语句beaucaus它没有保存ID(字符串),每次我退出时,我做的id(用户名+密码)改为整数..所以我想我的数据库认为id是一个整数但是我希望它是一个String。我实际上认为每种类型都是正确的。
这是我的代码,感谢您的帮助!
数据库:
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();
如果你需要其他相关课程,请问我。 这是错误:
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
感谢您的帮助!
答案 0 :(得分:0)
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,密码); //代码的其余部分}
id不应该是String,而是Integer。这就是您收到此错误的原因,因为当您创建表PROFILIEN时,您将COL_1_PROFILIEN(profilien_id)声明为INTEGER。
db.execSQL(" CREATE TABLE IF NOT 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))&#34);