2

Hi I have been working on a project that requires a database to store answers, hints, etc and I have added one row into the database using a method. I also made a getAnswer method that uses a rawQuery() to get the answer in the specified row (1) for the first and only item added in the database.

So the database class is all finished and I want to use the database for my game. I'm assuming it has to run the method once to fill the database (couldn't figure out better way to do an internal database so it will run every time the game is opened, if you know a better way I'm all ears). However in my Main Activity I can't seem to call the method that fills the database or the method to retrieve an item from the database. I have been looking and I don't understand why it is not working.

I am posting the Main Activity first and then the Game Database. Any help on how to use my database is greatly appreciated.

Main Activity Class

package tekvision.codedecrypter;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import gameInfo.GameDatabase;
public class MainActivity extends ActionBarActivity {
 //Runs before the application is created
 public Button mCampaignButton;
 //When the application is created
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //
 // I wanted to call it heat and use it in a toast to make sure its working
 //Gamedatabase. does not work to find my method
 //
 //Keeps screen on so it doesn't fall asleep
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 //Finding button by button id after application is created
 mCampaignButton = (Button)findViewById(R.id.campaignButtonID);
 //Checks if the campaign button is clicked
 mCampaignButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 //Toast pop up message
 Toast toast = Toast.makeText(getApplicationContext(),
 "campaign select",
 Toast.LENGTH_SHORT);
 toast.show();
 //Intent to go from main activity to campaign Level Select Activity
 Intent intent = new Intent(MainActivity.this, CampaignSelectLevel.class);
 startActivity(intent);
 }
 });
}
}

Game Database Class

package gameInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by User on 06/06/2015.
*/
//Extends the sql database open helper it will be error until the 2 methods are added plus the constructor
//the database is saved to a text file
public class GameDatabase extends SQLiteOpenHelper {
//Version number of the database
//Every update to the database will result in going up in the database version number
private static final int DATABASE_VERSION = 1;
//Private set of final strings, for the column names in the database
private static final String DATABASE_NAME =
 "Database",
 TABLE_1 = "Answers and Hints",
 TABLE_2 = "classical",
 TABLE_3 = "ancient",
 KEY_ID = "id",
 KEY_HINT = "hint",
 KEY_ANSWER = "answer",
 KEY_QUESTION = "question",
 KEY_INFO = "info",
 KEY_IMAGE = "image";
//Database Constructor, sets the databases named and the version of the database
public GameDatabase(Context context){
 super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//Whenever database is created
//Creating a table with each column and specify each columns type such as text or integer that is the primary key
@Override
 public void onCreate(SQLiteDatabase db){
 db.execSQL("CREATE TABLE " + TABLE_1 + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_QUESTION + " TEXT" + KEY_ANSWER + " TEXT" + KEY_IMAGE + "IMAGEVIEW" + KEY_HINT + " TEXT" + KEY_INFO + " TEXT)");
}
//When the database is upgraded
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
 db.execSQL("DROP TABLE IF EXISTS " + TABLE_MODERN);
 onCreate(db);
}
//Currently should have ONE row for level on in modern
public void fillGameDatabase(){
 SQLiteDatabase db = getWritableDatabase();
 ContentValues values = new ContentValues();
 //Fills information for the first row by a few columns
 //Modern ERA
 values.put(KEY_QUESTION, "US President");
 values.put(KEY_ANSWER, "Barack Obama");
 values.put(KEY_HINT, "He is the first African American president");
 values.put(KEY_INFO, "Barack Obama is the 44th President of the United States, and the first African American to hold the office. Born in Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where he served as president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree. He worked as a civil rights attorney and taught constitutional law at University of Chicago Law School from 1992 to 2004. He served three terms representing the 13th District in the Illinois Senate from 1997 to 2004, running unsuccessfully for the United States House of Representatives in 2000.");
 values.put(KEY_IMAGE, "R.drawable.obama.jpg");
 db.insert(TABLE_MODERN, null, values); //inserted a new row into the database
 db.close();
}
//Gets the answers based on the era nd level provided,
//db is database extension dont need to pass it
public Cursor getAnswer(String table, int level){
 SQLiteDatabase db = getReadableDatabase();
 Cursor cursor;
 //All one row of data
 String[] projections = {KEY_QUESTION, KEY_ANSWER, KEY_HINT, KEY_INFO, KEY_IMAGE};
 //Calling query method
 //Pass table name, the projections(names of columns), selection (data argument), selection arguments, group rows
 //filter by row groups, sort order, you can pass null for ones you dont want to enter
 cursor = db.rawQuery("SELECT " + KEY_ANSWER + " FROM " + TABLE_MODERN + " WHERE " + KEY_ID + "=" + level, null);
 db.close();
 return cursor;
}
/*
public Cursor getKeyHint(String era, int level{
 SQLiteDatabase db = getReadableDatabase();
}
public Cursor getKeyQuestion(String era, int level{
}
public Cursor getKeyInfo(String era, int level{
 }
public Cursor getKeyInfo(String era, int level{
}
*/}

I made the database from watching videos and reading documentation, but I have no clue how to actually "use" it. Thank you for reading, hope you can help.

adao7000
3,6702 gold badges30 silver badges37 bronze badges
asked Jun 10, 2015 at 13:33

1 Answer 1

1

I would recommend you to learn some basic SQL queries. Find some lessons like here. It shouldn't take you more than an hour or two to get the hang of it.

For your code specifically, the getAnswer() is good, it generates a valid query to access the database. You never actually use this method anywhere though. Put it in your MainActivity, probably in some onClick() method where the user asks for the answer to the question. I don't think you have started implementing this yet.

answered Jun 10, 2015 at 13:44
Sign up to request clarification or add additional context in comments.

11 Comments

That is my problem tho. I keep trying to call the method but I cannot seem to access it? Also Do I have to run the onCreate() method or the fillGameDatabase Method? Not being able to call any of the GameDatabase methods is my problem.
You need to create a GameDatabase object in your MainActivity and run the methods on that.
The object wants the "context" for the database, says it is to use to open or create the database" but not quite sure what I am suppose to put in it. Sorry if these sound like dumb questions
Haha don't worry about it, we all start somewhere! Just use the "this" keyword for the context.
Appreciate it. So I need to use both onCreate and fill database methods before I can use it correct? Is there any way I can only load the database once the first time the game is loaded therefor it does not have to refill every time the user plays or? Also why do I use the "this" keyword in this context? Thanks alot.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.