2

我在尝试从内部 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变量,它得到了正确的值

有人可以解释一下我做错了什么吗?

4

2 回答 2

0

你错过了一个空格。 KEY_CREATED + "TEXT)";应该KEY_CREATED + " TEXT)";

我通过测试活动将您的所有代码放入 Eclipse 中,并且能够使其正常工作。

我将以下代码放入我的活动中以对其进行测试(在使用 adb 删除由上述错字引起的带有坏表的数据库之后),一切都按预期工作。

    LocalDatabase db = new LocalDatabase(this);
    db.addUserProfile("01", "email", "firstname", "lastname", "study",
            "profession", "knows", "location", "status", 1,
            "gender", "DOB", "created");

    ContentValues cv = new ContentValues();

    cv = db.getUserProfile();

cv 包含预期值。如果您仍有问题,请告诉我们。

如果您在删除旧数据库时遇到问题,请尝试DATABASE_VER在 LocalDatabase 类中更新版本号。

于 2012-05-20T10:13:29.277 回答
0

我的猜测是您最初输入了一些值(可能仅用于用户 ID 和电子邮件字段),后来没有清除应用程序的数据,因此再次重建数据库(并且您继续在数据库中保留那些先前输入的值)。在这种情况下,即使您输入了新值,您也将仅为结果cContentValues中的第一个数据行构建对象(包含结果)(您这样做)。第一个值是只有用户 ID 和电子邮件的值,你可以得到其他所有值。Cursor c.moveToFirst();null

此外,您的 sql 创建字符串中有错字CreateProfileTable,应该是这样的:

...KEY_CREATED + " TEXT)";

像上面一样修改CreateProfileTable字符串,然后卸载并重新安装您的应用程序。

于 2012-05-20T09:56:08.270 回答