Android值不会插入数据库中

时间:2017-07-19 14:29:41

标签: android sqlite

我在数据库中存储数据时遇到问题。这是将文本值插入数据库的方法:

private void openVerify() {
    HashMap <String, String> queryValues = new HashMap<String, String>();
    queryValues.put("UserEmail", txtNewEmail.getText().toString());
    queryValues.put("FirstName", txtNewFirstName.getText().toString());
    queryValues.put("LastName", txtNewLastName.getText().toString());
    queryValues.put("Password", txtNewPassword.getText().toString());
    queryValues.put("PhoneNo", txtNewPhoneNumber.getText().toString());
    queryValues.put("Address", txtNewAddress.getText().toString());
    int success = (int)db.insertCustomer(queryValues);

    if (success > 0)
        Toast.makeText(getApplicationContext(), "Code has been sent!", Toast.LENGTH_LONG).show();

        openVerification();
        finish();

}

这是从上面的方法插入数据的SQL,它在另一个类中:

public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE tblUsers (UserID INTEGER PRIMARY KEY AUTOINCREMENT, UserEmail TEXT" +
            "FirstName TEXT, LastName TEXT, Password TEXT, PhoneNo TEXT, Address TEXT) ";
    db.execSQL(query);
}

public int insertCustomer(HashMap<String, String> queryValues){

    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put("UserEmail",queryValues.get("UserEmail"));
    values.put("FirstName",queryValues.get("FirstName"));
    values.put("LastName",queryValues.get("LastName"));
    values.put("Password",queryValues.get("Password"));
    values.put("PhoneNo",queryValues.get("PhoneNo"));
    values.put("Address",queryValues.get("Address"));

    int success = (int) database.insert("tblUsers", null, values);
    database.close();
    return success;
}

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

String query = "CREATE TABLE tblUsers (UserID INTEGER PRIMARY KEY AUTOINCREMENT, UserEmail TEXT," +
        "FirstName TEXT, LastName TEXT, Password TEXT, PhoneNo TEXT, Address TEXT) ";

您是否在 UserEmail文本之后错过了逗号?