SQLIte插入null而不是所需的值

时间:2014-12-25 20:16:10

标签: android sqlite android-sqlite

根据评论者的问题更新:

我正在尝试将值插入到SQLite表中,但是当我获取该值时,看起来像是插入了null。

插入行并获取/记录其上方所有现有行的调用如下:

@Override
public void onClick(View v) {
    Deck deck = new Deck(mDeckNameText.getText().toString());
    dbHandler.addDeck(deck);

    List<Deck> allDecks = dbHandler.getAllDecks();
    Log.d("Get Decks", "Getting all decks");

    for (Deck one_deck: allDecks){
        int deck_id = one_deck.getDeckId();
        String deck_id_string = ""+deck_id;
        Log.d("ID: ", deck_id_string);

        String deck_name = one_deck.getDeckName();
        if (deck_name != null) {
            Log.d("Name: ", deck_name);
        }
        else {
            Log.d("Name: ", "null");
        }
    }
}

插入行(在我的数据库处理程序中)的addDeck方法如下:

void addDeck(Deck deck) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_DECK_NAME, deck.getDeckName());

    db.insert(TABLE_DECKS,null,values);
}

Deck类定义为:

public class Deck {

    int _deck_id;
    String _deck_name;

    public Deck(){
    }

    public Deck(String deck_name) {
        this._deck_name = deck_name;
    }

    public Deck(int deck_id, String deck_name) {
        this._deck_id = deck_id;
        this._deck_name = deck_name;
    }

    //get deck_id
    public int getDeckId(){
        return this._deck_id;
    }

    //set deck_id
    public void setDeckId(int deck_id){
        this._deck_id = deck_id;
    }

    //get deck name
    public String getDeckName(){
        return this._deck_name;
    }

    //set deck name
    public void setDeckName(String deck_name){
        this._deck_name = deck_name;
    }
}

包含卡片组的表格使用以下方法创建:

public void onCreate(SQLiteDatabase db) {
    String CREATE_DECKS_TABLE = "CREATE TABLE "+TABLE_DECKS+"("
            +KEY_DECK_ID+" INTEGER PRIMARY KEY, "+KEY_DECK_NAME
            +" TEXT"+")";

    db.execSQL(CREATE_DECKS_TABLE);
}

获取套牌的方法如下:

//get single deck
public Deck getDeck(long deck_id) {
    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT * FROM " + TABLE_DECKS + " WHERE "
            + KEY_DECK_ID + " = " + deck_id;

    Log.e(LOG, selectQuery);

    Cursor c = db.rawQuery(selectQuery, null);

    if (c != null)
        c.moveToFirst();

    Deck deck = new Deck();
    deck.setDeckId(c.getInt(c.getColumnIndex(KEY_DECK_ID)));

    return deck;
}

//get all decks
public List<Deck> getAllDecks() {
    List<Deck> deck_list = new ArrayList<Deck>();
    String selectQuery = "SELECT * FROM " + TABLE_DECKS;

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {
            Deck deck = new Deck();
            deck.setDeckId(c.getInt((c.getColumnIndex(KEY_DECK_ID))));

            // adding to decks
            deck_list.add(deck);
        } while (c.moveToNext());
    }

    return deck_list;
}

当我从表格中的行中获取卡座ID和卡座名称时得到的日志输出看起来像这样(在第三次尝试插入卡座之后):
D / ID :: 1
D / Name :: null
D / ID :: 2
D / Name :: null
D / ID :: 3
D / Name :: null

知道为什么名称会以null返回?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要在获取代码中添加一行:

deck.setDeckName(c.getString((c.getColumnIndex(KEY_DECK_NAME))));

返回语句之前的 getDeck 中添加此内容

我从记忆中写道 希望能帮助到你 如果是,请评价