0

我有一个应用程序填充 11 个数据库表,然后获取数据并将其显示在 LogCat 中。在我将主键更改为自动增量之前,我的应用程序一直可以正常工作。更改对所有表都有效,但一张表给我带来了问题。

这是我的 DBAdapter,以及我生成用户数据库的部分(给我带来问题的部分):

//Variables for the User table
public static final String TABLE_USER = "User";
public static final String USER_ID = "_id";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String L_NAME = "L_Name";
public static final String M_NAME = "M_Name";
public static final String F_NAME = "F_Name";
public static final String OFFICE = "Office";
public static final String CELLPHONENUMBER = "Cellphone_Number";
public static final String LANDLINE = "Landline";
public static final String ADDRESS = "Address";
public static final String EMAIL = "Email";

private static final String DATABASE_CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER 
        + "(" + USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
        + USERNAME + " TEXT not null, "
        + PASSWORD + " TEXT not null, "
        + L_NAME + " TEXT not null, "
        + M_NAME + " TEXT not null, "
        + F_NAME + " TEXT not null, "
        + OFFICE + " TEXT not null, "
        + CELLPHONENUMBER + " TEXT not null, "
        + LANDLINE + " TEXT not null, "
        + ADDRESS + " TEXT not null, "
        + EMAIL + " TEXT not null);" ;

当然几行下来,在我的 DBAdapter 的onCreate方法中,我有这个:

        database.execSQL(DATABASE_CREATE_AUTHORIZED_PERSONNEL_TABLE);

我还有一个 userClass,如下所示:

public class UserClass {

private String _id;
private String username;
private String password;
private String l_name;
private String m_name;
private String f_name;
private String office;
private String Cellphone_Number;
private String Landline;
private String Address;
private String Email;


//constructor - empty
public UserClass(){

}

//constructor
public UserClass(String userName, String passWord, String LastName, String MiddleName, String FirstName, 
                    String Office, String cpNumber, String landLine, String Address, String Email){
    this.username = userName;
    this.password = passWord;
    this.l_name = LastName;
    this.m_name = MiddleName;
    this.f_name = FirstName;
    this.office = Office;
    this.Cellphone_Number = cpNumber;
    this.Landline = landLine;
    this.Address = Address;
    this.Email = Email;

}

在这些构造函数之后,我有我的 setter 和 getter。

我还有一个 UserDBAdapter 类,它负责数据库查询和数据获取,它是我的数据访问对象。这是addUser(UserClass user)方法:

//add one user
public long addUser(UserClass user){

    ContentValues values = new ContentValues();

    values.put(USERNAME, user.getUsername());
    values.put(PASSWORD, user.getPassword());
    values.put(L_NAME, user.getL_name());
    values.put(M_NAME, user.getM_name());
    values.put(F_NAME, user.getF_name());
    values.put(OFFICE, user.getOffice());
    values.put(CELLPHONENUMBER, user.getCellphone_Number());
    values.put(LANDLINE, user.getLandline());
    values.put(ADDRESS, user.getAddress());
    values.put(EMAIL, user.getEmail());

    return this.mDB.insert(TABLE_USER, null, values);
}

这是获取所有用户及其详细信息的方法: //get all users public List getAllUsers() { List contactList = new ArrayList(); // 选择所有查询字符串 selectQuery = "SELECT * FROM " + TABLE_USER;

    Cursor cursor = mDB.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            UserClass user = new UserClass();

            user.set_id(cursor.getString(cursor.getColumnIndex(USER_ID) ) );
            user.setUsername(cursor.getString(cursor.getColumnIndex(USERNAME) ) );
            user.setPassword(cursor.getString(cursor.getColumnIndex(PASSWORD) ) );
            user.setL_name(cursor.getString(cursor.getColumnIndex(L_NAME) ) );
            user.setM_name(cursor.getString(cursor.getColumnIndex(M_NAME) ) );
            user.setF_name(cursor.getString(cursor.getColumnIndex(F_NAME) ) );
            user.setOffice(cursor.getString(cursor.getColumnIndex(OFFICE) ) );
            user.setCellphone_Number(cursor.getString(cursor.getColumnIndex(CELLPHONENUMBER) ) );
            user.setLandline(cursor.getString(cursor.getColumnIndex(LANDLINE) ) );
            user.setAddress(cursor.getString(cursor.getColumnIndex(ADDRESS) ) );
            user.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL) ) );


            // Adding contact to list
            contactList.add(user);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

在我的活动中,我正在填充数据库并获取数据:

public void initializeUserDB(){
    Log.d("Users", "Inserting . . .");

    userDB.open();
    userDB.addUser(new UserClass("user1", "1234", "Doe", "Smith", "John", "Makati", 
                                    "09123456789", "1234567", "Lorem Ipsum Dolor", "LoremIpsum@Dolor.com"));
    userDB.addUser(new UserClass("user2", "1234", "Smith", "Doe", "John", "Makati", 
                                    "cpNumber", "Landline", "address", "electronic Mail Address"));


    //Check if they were inputted properly
    Log.d("Reading: ", "Reading all users . . ."); 
    List<UserClass> users = userDB.getAllUsers();       

    for (UserClass user : users) {
        String log = "Id: " + user.get_id() + "\n" +
                        "Username: " + user.getUsername() + "\n" +
                        "Password: " + user.getPassword() + "\n" +
                        "Last Name: " + user.getL_name() + "\n" +
                        "Middle Name: " + user.getM_name() + "\n" +
                        "First Name: " + user.getF_name() + "\n" +
                        "Office: " + user.getOffice() + "\n" +
                        "Cellphone Number: " + user.getCellphone_Number() + "\n" +
                        "Landline: " + user.getLandline() + "\n" +
                        "Address: " + user.getAddress() +"\n" +
                        "Email: " + user.getEmail();
            // Writing Contacts to log
        Log.d("Details: ", log);
    }
    userDB.close();
}

数据已正确插入,但是,在获取数据时,LogCat 告诉user.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL) ) );我 userDBAdapter 类中的行有问题。

我想我能够正确初始化我的电子邮件列,否则我的数据插入顺序会出错。如果我注释掉那行,user.getEmail();我活动中的方法会给我 null。

我对这个问题感到困惑,我正确初始化了表格并正确插入了值,我不明白为什么我无法正确获取值。

4

1 回答 1

0

我的另一个简单错误,对不起。

在我的 userDBAdapter 类中,该行public static final String EMAIL = "EMAIL";应该是:public static final String EMAIL = "Email";

于 2013-08-10T06:20:34.770 回答