Android Guessing Game | SQL Lite Problem :(

0 Scott Davies · August 16, 2015
Hey Guys,

First of all I want to say thanks to bucky and all the other contributers here, the tutorials here gave me a good kick start to learning C++ a few years ago and now im moving onto Java/Android dev, I have watched most of the tutorials here for android studio and I thought I would try and create my own guessing game to use the new skills I have learnt, now the game works just fine but now I have tried to incorperate a settings menu that store settings in a SQL Lite DB now I think my code is creating/inserting data to the database but when i try read it, it hangs on a black screen, Im wondering if someone would take a look at this code and tell me if they see anything out of the norm, any help would be apreciated, if need be I can post the entire source


EDIT: I changed the insert statement to return a boolean value so now Im pretty sure its inserting data to be read.

public class MyDBHandler extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1; // update if we update class
private static final String DATABASE_NAME = "Settings.db"; // Database Name
private static final String TABLE_SETTINGS = "Settings"; // Table Name
public static final String COLUMN_ID = "_id";
public static final String COLUMN_MIN = "min";
public static final String COLUMN_MAX = "max";
public static final String COLUMN_DIFFICULTY = "Difficulty";
public static final String COLUMN_CHEAT = "cheat";

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String query = "CREATE TABLE IF NOT EXISTS Settings" +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, min INTEGER, max INTEGER, Difficulty INTEGER, cheat INTEGER);";

db.execSQL(query);
}

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

//Add new row to database
public boolean addSettings(Settings setting){

SQLiteDatabase db = getWritableDatabase();

//Delete table so we create new record with just 1 row (change to update statement soon!!!)
db.execSQL("DROP TABLE IF EXISTS Settings");
onCreate(db);

ContentValues values = new ContentValues();

values.put(COLUMN_MIN, setting.get_min_value());
values.put(COLUMN_MAX, setting.get_max_value());
values.put(COLUMN_DIFFICULTY, setting.get_difficulty());
values.put(COLUMN_CHEAT, setting.get_cheat());

if(db.insert(TABLE_SETTINGS, null, values) > 0)
{
db.close();
return true;
}else{
db.close();
return false;
}

}

//Update settings
public void updateSettings(Settings setting){

ContentValues values = new ContentValues();

values.put(COLUMN_MIN, setting.get_min_value());
values.put(COLUMN_MAX, setting.get_max_value());
values.put(COLUMN_DIFFICULTY, setting.get_difficulty());
values.put(COLUMN_CHEAT, setting.get_cheat());

SQLiteDatabase db = getWritableDatabase();
db.update(TABLE_SETTINGS, values, null, new String[]{String.valueOf(setting.get_id())});
db.close();
}

//Read database
public int[] ReadSettings(){

//Create array to store data from database
int [] ary = new int[4];

String query = "SELECT * FROM Settings"; // query
SQLiteDatabase db = getWritableDatabase(); // DB object

//pointer to location in results
Cursor c = db.rawQuery(query, null);

//Move to first row
c.moveToFirst();

//Loop throug the DB and retrieve data and store in array
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("min")) != null){
ary[0] = c.getInt(c.getColumnIndex("min"));
ary[1] = c.getInt(c.getColumnIndex("max"));
ary[2] = c.getInt(c.getColumnIndex("Difficulty"));
ary[3] = c.getInt(c.getColumnIndex("cheat"));

}

}

db.close();
return ary;

}
}



Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · August 17, 2015
I have a theory. This is just speculation, but the problem is this line:

SQLiteDatabase db = getWritableDatabase(); // DB object

That should be done within the actual constructor. So in other words, you are attempting to get a non-existent database for writing and then using it. The database actually is not initialized until you first attempt to grab data from it. I would also suggest making the database local. You don't want connection leaks.

It is bad form to initialize variables like that on the outside instead of within the constructor unless they are simple classes like strings or integers or other values that are just using a default constructor like ArrayList m_int=new ArrayList<Integer>() or things that actually depend on external values. Truthfully, I would also put those inside the constructor if their values will change.
  • 1

Java / Android Development

107,072 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator