我有一个应用程序,它填充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;
}
我在那些构造函数后面有我的二传手和吸气剂。
我还有一个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);
}
这是获取所有用户及其详细信息的方法: //获取所有用户 public List getAllUsers(){ 列出contactList = new ArrayList(); //选择所有查询 String 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告诉我userDBAdapter类中的行user.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL) ) );
存在问题。
我认为我能够正确初始化我的电子邮件列,否则我的数据插入序列会出错。如果我注释掉该行,我的活动中的user.getEmail();
方法将为空。
我对这个问题感到困惑,我正确地初始化了表并正确插入了值,我不明白为什么我无法正确获取值。
答案 0 :(得分:0)
我的另一个简单的错误,抱歉。
在我的userDBAdapter类中,行public static final String EMAIL = "EMAIL";
应为:public static final String EMAIL = "Email";