ContentValue将空数据插入SQLite中的表 - android

时间:2017-03-27 17:58:30

标签: android sqlite

它插入一行但没有任何内容。我在以前的应用程序中使用过这种方法,它们都运行良好。请帮助我卡住

public boolean Insert(){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_BUSINESS_NAME,"TEster"); // i tried putting the value directly
    cv.put(COLUMN_BUSINESS_CATEGORY,business_category);
    cv.put(COLUMN_BUSINESS_DESCRIPTION,business_description);
    cv.put(COLUMN_BUSINESS_PHONE,phone_number);
    cv.put(COLUMN_BUSINESS_EMAIL,email_address);
    cv.put(COLUMN_BUSINESS_URL,website);
    cv.put(COLUMN_BUSINESS_ADDRESS,locationAddress.address);
    cv.put(COLUMN_BUSINESS_F_ADDRESS,locationAddress.fAddress);
    cv.put(COLUMN_BUSINESS_LATITUDE,locationAddress.latLng.latitude);
    cv.put(COLUMN_BUSINESS_LONGITUDE,locationAddress.latLng.longitude);
    db.delete(TABLE_MY_BUSINESS, null, null);
    db.insert(TABLE_MY_BUSINESS, COLUMN_ID, cv);
    return true;
}

这是我从数据库中获取的方式

public void Fetch(){
    SQLiteDatabase db = this.getReadableDatabase();
    final String[] column = {COLUMN_BUSINESS_NAME,COLUMN_BUSINESS_CATEGORY,COLUMN_BUSINESS_DESCRIPTION,
            COLUMN_BUSINESS_PHONE,COLUMN_BUSINESS_EMAIL,COLUMN_BUSINESS_URL,COLUMN_BUSINESS_ADDRESS,
            COLUMN_BUSINESS_F_ADDRESS,COLUMN_BUSINESS_LATITUDE,COLUMN_BUSINESS_LONGITUDE};
    String table = TABLE_MY_BUSINESS;
    String selection = COLUMN_ID + " >= ? ";
    String[] selectionArgs = {"0"};
    Cursor cursor = db.query(table,column,selection,selectionArgs,null,null,null);
    cursor.moveToFirst();
    while (cursor.isAfterLast()) {
        this.business_name = cursor.getString(0);
        this.business_category = cursor.getString(1);
        this.business_description = cursor.getString(2);
        this.phone_number = cursor.getString(3);
        this.email_address = cursor.getString(4);
        this.website = cursor.getString(5);
        this.locationAddress.setAddress(cursor.getString(6));
        this.locationAddress.setfAddress(cursor.getString(7));
        this.locationAddress.setLatLng(cursor.getInt(8),cursor.getInt(9));
        cursor.moveToNext();
    }
    cursor.close();
    db.close();
}

这就是我创建表格的方式

String CREATE_MY_BUSINESS_TABLE = "CREATE TABLE "+TABLE_MY_BUSINESS+" " + "("
            +COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +COLUMN_BUSINESS_NAME+" TEXT,"
            +COLUMN_BUSINESS_CATEGORY+" TEXT,"
            +COLUMN_BUSINESS_DESCRIPTION+" TEXT,"
            +COLUMN_BUSINESS_PHONE+" TEXT,"
            +COLUMN_BUSINESS_EMAIL+" TEXT,"
            +COLUMN_BUSINESS_URL+" TEXT,"
            +COLUMN_BUSINESS_ADDRESS+" TEXT,"
            +COLUMN_BUSINESS_F_ADDRESS+" TEXT,"
            +COLUMN_BUSINESS_LONGITUDE+" INTEGER,"
            +COLUMN_BUSINESS_LATITUDE+" INTEGER)";
    db.execSQL(CREATE_USER_TABLE);

1 个答案:

答案 0 :(得分:0)

while (cursor.isAfterLast()) {

仅当光标为空时才执行此while循环的主体。

执行查询将光标定位在第一个条目之前,因此迭代光标的最简单方法是这样的:

Cursor cursor = db.query(...);
while (cursor.moveToNext()) {
    ...
}