samedi 27 décembre 2014

Android SQLite not creating table

I'm trying to create a database with a table called card, but SQLite doesn't create the table. LogCat says there's no card table when I execute a SELECT command on it.


I have a DatabaseHelper class that has this code in it (there's also a function that inserts records into the table which I omitted here since the error happens before).



public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "hssim";

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

@Override
public void onCreate(SQLiteDatabase db) {

String CREATE_TABLE_CARD = "CREATE TABLE card ("
+ "dbid INTEGER PRIMARY KEY AUTOINCREMENT," + "id TEXT,"
+ "naam TEXT," + "mana INTEGER," + "attack INTEGER,"
+ "health INTEGER," + "effect TEXT," + "zeldzaamheid TEXT,"
+ "typeId INTEGER," + "subtypeId INTEGER," + "classId INTEGER,"
+ "goud INTEGER)";
db.execSQL(CREATE_TABLE_CARD);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS card");

onCreate(db);
}
}


Please note that this last record 'goud' is actually a boolean in my java class, but since SQLite can't store booleans it is an int here.


I have this code in my activity:



public class SimulatorActivity extends Activity {

private DatabaseHelper db;

List<Card> cards = new ArrayList<Card>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simulator);

db = new DatabaseHelper(this);

cards = db.getCards();
}


This calls the getCards() function from the DatabaseHelper class:



public List<Card> getCards() {
List<Card> cards = new ArrayList<Card>();

String selectQuery = "SELECT * FROM card";
// This is probably where the error occurs.

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

if (cursor.moveToFirst()) {
do {
boolean bool;
if (cursor.getInt(11) == 1) {
bool = true;
} else {
bool = false;
}

Card card = new Card(cursor.getLong(0), cursor.getString(1),
cursor.getString(2), cursor.getInt(3),
cursor.getInt(4), cursor.getInt(5),
cursor.getString(6), cursor.getString(7),
cursor.getLong(8), cursor.getLong(9),
cursor.getLong(10), bool);
cards.add(card);
} while (cursor.moveToNext());
}

cursor.close();
db.close();
return cards;
}


This is part of the LogCat that I get back:



12-27 14:54:23.482: E/AndroidRuntime(1692): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.thomasmore.hearthstone/be.thomasmore.hearthstone.SimulatorActivity}: android.database.sqlite.SQLiteException: no such table: card (code 1): , while compiling: SELECT * FROM card


I have no idea why I can't create this database/table. I tried changing database name/version, clearing database data on my device, ... Any help regarding this would be much appreciated.


Aucun commentaire:

Enregistrer un commentaire