Reputation: 13
Updated per commenter questions:
I am trying to insert a value into a SQLite table, but when I fetch that value, it looks like null is getting inserted instead.
The call to insert the row and fetch/log all the existing rows above it is given as:
@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");
}
}
}
The addDeck method in to insert the row (which is in my database handler) is as follows:
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);
}
Where the Deck class is defined as:
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;
}
}
The table that contains the decks is created in the following method:
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);
}
The methods to fetch the decks are as follows:
//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;
}
The log output I get when I fetch the deck ids and deck names from the rows in my table looks something like this (after the third attempt at inserting a deck):
D/ID:﹕ 1
D/Name:﹕ null
D/ID:﹕ 2
D/Name:﹕ null
D/ID:﹕ 3
D/Name:﹕ null
Any idea why the name is coming back as null?
Thank you in advance.
Upvotes: 1
Views: 727
Reputation: 3416
You need to add a line in the fetch code:
deck.setDeckName(c.getString((c.getColumnIndex(KEY_DECK_NAME))));
Add this in getDeck right before the return statement
I wrote from memory Hope it helps Please rate if yes
Upvotes: 1