Android:Sqlite保存数据,但在获取数据时,该值为null

时间:2017-09-18 03:21:29

标签: android sqlite

我正在尝试将数据从API保存到sqlite数据库,我的所有数据都被保存但当我尝试检索数据时(driver_id)返回null值。我是否遇到此问题,因为我已将我的值driver_id定义为整数,并且API数据是以字符串形式出现的?

保存数据时的日志记录:

E/LoginCheck: ValueCheck drivername=user driver_id=2 drivercontact=ba 1 kha 2345 busnumber=999999

我的SQLiteHandler如下所示:

 public class SQLiteHandler extends SQLiteOpenHelper {
 private static final String TAG = SQLiteHandler.class.getSimpleName();

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "android_api";

// Login table name
private static final String TABLE_USER = "user";

// Login Table Columns names
private static final String KEY_DRIVER_ID = "driver_id";
private static final String KEY_DRIVER_NAME = "drivername";
private static final String KEY_BUS_NUMBER = "busnumber";
private static final String KEY_SCHOOL_UID = "school_id";
private static final String KEY_CONTACT_NUMBER = "drivercontact";

public SQLiteHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + KEY_DRIVER_ID + " INTEGER PRIMARY KEY," + KEY_DRIVER_NAME + " TEXT,"
            + KEY_BUS_NUMBER + " TEXT UNIQUE," + KEY_CONTACT_NUMBER + " TEXT" + ")";
    db.execSQL(CREATE_LOGIN_TABLE);

    Log.d(TAG, "Database tables created" +CREATE_LOGIN_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);

    // Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 * */
public void addUser(String drivername, String busnumber, String driver_id, String drivercontact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_DRIVER_NAME, drivername); 
    values.put(KEY_BUS_NUMBER, busnumber); 
    values.put(KEY_DRIVER_ID, driver_id); 
    values.put(KEY_CONTACT_NUMBER, drivercontact);

    Log.e("LoginCheck", "ValueCheck " + values.toString());
    // Inserting Row
    long id = db.insert(TABLE_USER, null, values);
    db.close(); // Closing database connection

    Log.d(TAG, "New user inserted into sqlite: " + id);
}

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails() {
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_USER;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        user.put("drivername", cursor.getString(1));
        user.put("busnumber", cursor.getString(2));
        user.put("driver_id", cursor.getString(3));
        user.put("drivercontact", cursor.getString(4));
        Log.e("newvaluecheck", "newvaluecheck " + user.toString());
    }
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());

    return user;
}

/**
 * Re crate database Delete all tables and create them again
 * */
public void deleteUsers() {
    SQLiteDatabase db = this.getWritableDatabase();
    // Delete All Rows
    db.delete(TABLE_USER, null, null);
    db.close();

    Log.d(TAG, "Deleted all user info from sqlite");
}

}

我的logcat:

E/newvaluecheck: newvaluecheck {drivername=user, driver_id=null, drivercontact=ba 1 kha 2345, busnumber=999999}

2 个答案:

答案 0 :(得分:1)

因为您使用

定义表格

String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "(" + KEY_DRIVER_ID + " INTEGER PRIMARY KEY," + KEY_DRIVER_NAME + " TEXT," + KEY_BUS_NUMBER + " TEXT UNIQUE," + KEY_CONTACT_NUMBER + " TEXT" + ")";

然后使用SELECT *(即所有列)提取数据, driver_id 偏移量将 0 (第一列)不是 3

我建议不使用硬编码偏移,而是使用Cursor方法getColumnIndex(columname as String)根据列名提供实际偏移量。

所以我建议使用: -

if (cursor.getCount() > 0) {
        user.put("drivername", cursor.getString(cursor.getColumnIndex(KEY_DRIVER_NAME)));
        user.put("busnumber", cursor.getString(cursor.getColumnIndex(KEY_BUS_NUMBER)));
        user.put("driver_id", cursor.getString(getColumnIndex(KEY_DRIVER_ID)));
        user.put("drivercontact", cursor.getString(getColumnIndex(KEY_CONTACT_NUMBER)));
        Log.e("newvaluecheck", "newvaluecheck " + user.toString());

P.S。没有检查,所以可能有奇怪的错字。

您也可以替换: -

    cursor.moveToFirst();
    if (cursor.getCount() > 0) {

    if(cursor.moveToFirst) {

如果没有第一行要移动,cursor.MoveToFirst将返回false。

您还应该使用正确的Cursor get ?????适用于该柱的方法,例如,对于driver_id column,这将被存储为long,而不是: -

你应该使用

cursor.getString(getColumnIndex(KEY_DRIVER_ID)): -

cursor.getLong(getColumnIndex(KEY_DRIVER_ID))

不使用适当的类型会导致意外结果,尤其是对于String可能会失去精度的双精度数。

答案 1 :(得分:0)

只需将此cursor.getString(1)替换为cursor.getInt(cursor.getColumnIndex("KEY_DRIVER_ID"))

即可