sqllite db android没有正确添加用户

时间:2016-01-26 20:56:04

标签: java android sqlite

我得到一个"尝试调用虚方法' android.content.res.Resources android.content.Context.getResources()'在null对象引用"

public class userDB  extends SQLiteOpenHelper {

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

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

// Assignment table name
private static final String TABLE_USERINFO = "user";

// Assignment Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TOKEN = "token";

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



// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_USERINFO + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TOKEN + " TEXT" +  ")";

    db.execSQL(CREATE_CONTACTS_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_USERINFO);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding user
public void addUser(authToken user) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TOKEN, user.getToken());

    // Inserting Row
    db.insert(TABLE_USERINFO, null, values);
    Toast.makeText(context, "Inserted" + values.toString() , Toast.LENGTH_LONG).show();
    db.close(); // Closing database connection
}



// Getting All Users
public List<authToken> getAllUsers() {
    List<authToken> userList = new ArrayList<>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_USERINFO;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            authToken user = new authToken();
            user.setToken(cursor.getString(1));

            // Adding user to list
            userList.add(user);
        } while (cursor.moveToNext());
    }

    // return user list
    return userList;
}

// Update User
public int updateUser(authToken user) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_TOKEN, user.getToken());

    // updating row
    return db.update(TABLE_USERINFO, values, KEY_ID + " = ?",
            new String[] { String.valueOf(user.getUser().getId())});
}

// Delete user
public void deleteUser(authToken user) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_USERINFO, KEY_ID + " = ?",
            new String[] { String.valueOf(user.getToken()) });
    db.close();
}


// Getting User Count
public int getUserCount() {
    String countQuery = "SELECT  * FROM " + TABLE_USERINFO;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

}

我正在读取这样的信息:(和userinfo.getToken)正在吐出令牌,它的内容与sql没有正确地添加db。

                           userDB db = new userDB(getApplicationContext());
                            dbs = db.getWritableDatabase();
                            //db.onUpgrade(dbs, 1, 1);
                            // Inserting Contacts
                            Log.d("Insert: ", "Inserting .." +                

                             userInfo.getToken());
                            db.addUser(new authToken(userInfo.getToken()));


                            // Reading all 
                            Log.d("Reading: ", "Reading all contacts..");
                            List<authToken> contacts = db.getAllUsers();

                            for (authToken cn : contacts) {
                                String log = "Token: " + cn.getToken();
                                // Writing Contacts to log
                                Log.d("Name: ", log);

                            }

2 个答案:

答案 0 :(得分:1)

你在哪里设置背景?我认为问题在于Toast消息。 您应该在构造函数方法中设置上下文:

Context context;
public userDB (Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

答案 1 :(得分:0)

这可能没有关系但是在方法getUserCount()中,应该在cursor.close()之前调用cursor.getCount()。