我在尝试从内部 SQLite 数据库(Android)中读取时遇到问题。
这是我的本地数据库代码:
public class LocalDatabase extends SQLiteOpenHelper{
static final String DATABASE_NAME = "database";
static final int DATABASE_VER = 2;
static final String PROFILES_TABLE_NAME = "profiles";
static final String KEY_ID = "node_id";
static final String KEY_USERID = "user_id";
static final String KEY_EMAIL = "email";
static final String KEY_FIRSTNAME = "firstname";
static final String KEY_LASTNAME = "lastname";
static final String KEY_STUDY = "study";
static final String KEY_PROFESSION = "profession";
static final String KEY_KNOWS = "knows";
static final String KEY_LOCATION = "location";
static final String KEY_STATUS = "status";
static final String KEY_POINTS = "points";
static final String KEY_GENDER = "gender";
static final String KEY_DOB = "dob";
static final String KEY_CREATED = "created";
public LocalDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VER);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CreateProfileTable = "CREATE TABLE " + PROFILES_TABLE_NAME + "(" +
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_USERID + " TEXT," +
KEY_EMAIL + " TEXT," +
KEY_FIRSTNAME + " TEXT," +
KEY_LASTNAME + " TEXT," +
KEY_STUDY + " TEXT," +
KEY_PROFESSION + " TEXT," +
KEY_KNOWS + " TEXT," +
KEY_LOCATION + " TEXT," +
KEY_STATUS + " TEXT," +
KEY_POINTS + " INTEGER," +
KEY_GENDER + " TEXT," +
KEY_DOB + " TEXT," +
KEY_CREATED + "TEXT)";
db.execSQL(CreateProfileTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + PROFILES_TABLE_NAME);
onCreate(db);
}
public void addUserProfile(String userID, String email, String firstname, String lastname, String study, String profession, String knows,
String location, String status, int points, String gender, String DOB, String created){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues params = new ContentValues();
params.put(KEY_USERID, userID);
params.put(KEY_EMAIL, email);
params.put(KEY_FIRSTNAME, firstname);
params.put(KEY_LASTNAME, lastname);
params.put(KEY_STUDY, study);
params.put(KEY_PROFESSION, profession);
params.put(KEY_KNOWS, knows);
params.put(KEY_LOCATION, location);
params.put(KEY_STATUS, status);
params.put(KEY_POINTS, points);
params.put(KEY_GENDER, gender);
params.put(KEY_DOB, DOB);
params.put(KEY_CREATED, created);
db.insert(PROFILES_TABLE_NAME, null, params);
db.close();
}
public ContentValues getUserProfile(){
String query = "SELECT * FROM " + PROFILES_TABLE_NAME;
ContentValues userProfile = new ContentValues();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
if(c.getCount()>0){
userProfile.put(KEY_USERID, c.getString(1));
userProfile.put(KEY_EMAIL, c.getString(2));
userProfile.put(KEY_FIRSTNAME, c.getString(3));
userProfile.put(KEY_LASTNAME, c.getString(4));
userProfile.put(KEY_STUDY, c.getString(5));
userProfile.put(KEY_PROFESSION, c.getString(6));
userProfile.put(KEY_KNOWS, c.getString(7));
userProfile.put(KEY_LOCATION, c.getString(8));
userProfile.put(KEY_STATUS, c.getString(9));
userProfile.put(KEY_POINTS, c.getString(10));
userProfile.put(KEY_GENDER, c.getString(11));
userProfile.put(KEY_DOB, c.getString(12));
userProfile.put(KEY_CREATED, c.getString(13));
}
c.close();
db.close();
return userProfile;
}
}
问题是,当我尝试使用(getUserProfile)读取数据时,将数据插入数据库(使用添加用户配置文件)后,除了user_id和email之外的所有字段都为空,我检查了addUserProfile中的params变量,它得到了正确的值
有人可以解释一下我做错了什么吗?