Android Studio Accessing SQLite Database Java -


hi have been working on project requires database store answers, hints, etc , have added 1 row database using method. made getanswer method uses rawquery() answer in specified row (1) first , item added in database.

so database class finished , want use database game. i'm assuming has run method once fill database (couldn't figure out better way internal database run every time game opened, if know better way i'm ears). in main activity can't seem call method fills database or method retrieve item database. have been looking , don't understand why not working.

i posting main activity first , game database. on how use database 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 application created   public button mcampaignbutton;     //when application created   @override   protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       setcontentview(r.layout.activity_main);        //       // wanted call heat , use in toast make sure   working       //gamedatabase. not work find method       //        //keeps screen on doesn't fall asleep      getwindow().addflags(windowmanager.layoutparams.flag_keep_screen_on);      //finding button button id after application created     mcampaignbutton = (button)findviewbyid(r.id.campaignbuttonid);      //checks if campaign button clicked     mcampaignbutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              //toast pop message             toast toast = toast.maketext(getapplicationcontext(),                 "campaign select",                     toast.length_short);              toast.show();               //intent go main activity 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 user on 06/06/2015. */  //extends sql database open helper error until 2 methods added plus constructor //the database saved text file public class gamedatabase extends sqliteopenhelper {  //version number of database //every update database result in going in database version number private static final int database_version = 1;  //private set of final strings, column names in database private static final string database_name =         "database",         table_1 = "answers , 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 databases named , version of database public gamedatabase(context context){      super(context, database_name, null, database_version); }   //whenever database created //creating table each column , specify each columns type such text or integer 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 database upgraded @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion){     db.execsql("drop table if exists " + table_modern);      oncreate(db); }     //currently should have 1 row level on in modern public void fillgamedatabase(){     sqlitedatabase db = getwritabledatabase();      contentvalues values = new contentvalues();      //fills information first row few columns      //modern era     values.put(key_question, "us president");     values.put(key_answer, "barack obama");     values.put(key_hint, "he first african american president");     values.put(key_info, "barack obama 44th president of united states, , first african american hold office. born in honolulu, hawaii, obama graduate of columbia university , harvard law school, served president of harvard law review. community organizer in chicago before earning law degree. worked civil rights attorney , taught constitutional law @ university of chicago law school 1992 2004. served 3 terms representing 13th district in illinois senate 1997 2004, running unsuccessfully united states house of representatives in 2000.");     values.put(key_image, "r.drawable.obama.jpg");     db.insert(table_modern, null, values); //inserted new row database      db.close();  }   //gets answers based on era nd level provided, //db database extension dont need pass public cursor getanswer(string table, int level){     sqlitedatabase db = getreadabledatabase();      cursor cursor;      //all 1 row of data     string[] projections = {key_question, key_answer, key_hint, key_info, key_image};      //calling query method     //pass table name, projections(names of columns), selection (data argument), selection arguments, group rows     //filter row groups, sort order, can pass null ones dont want enter     cursor = db.rawquery("select " + key_answer + " " + table_modern + " " + 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 database watching videos , reading documentation, have no clue how "use" it. thank reading, hope can help.

i recommend learn basic sql queries. find lessons here. shouldn't take more hour or 2 hang of it.

for code specifically, getanswer() good, generates valid query access database. never use method anywhere though. put in mainactivity, in onclick() method user asks answer question. don't think have started implementing yet.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -