eclipse - how do i use database for my program in android -


i new android. tried few codes n read sqlite bit. couldn't understand much. still having difficulty in using database created using sqlite browser in program.

my project app should display content in database 1 after other. database consisting of 2 columns. id , description.

package com.example.singlepop;  import java.util.calendar;  import android.os.bundle; import android.app.activity; import android.view.gravity; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.linearlayout; import android.widget.popupwindow; import android.widget.textview; import android.widget.linearlayout.layoutparams;  public class single extends activity {      popupwindow popup;     linearlayout layout;     textview tv;     layoutparams params;     linearlayout mainlayout;     button but;     boolean click = true;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.single);          final calendar cld = calendar.getinstance();          int time = cld.get(calendar.hour_of_day);             if(time==16)             {                 popup = new popupwindow(this);                 layout = new linearlayout(this);                 mainlayout = new linearlayout(this);                 tv = new textview(this);                 = new button(this);                 but.settext("click me");                 but.setonclicklistener(new onclicklistener() {                      public void onclick(view v) {                         if (click) {                             popup.showatlocation(mainlayout, gravity.bottom, 10, 10);                             popup.update(50, 50, 300, 80);                             click = false;                         } else {                             popup.dismiss();                             click = true;                         }                     }                    });                 params = new layoutparams(layoutparams.wrap_content,                         layoutparams.wrap_content);                 layout.setorientation(linearlayout.vertical);                  // here single tuple in database should displayed everyday @ 16hrs                   tv.settext("hi sample text popup window");                 //                  layout.addview(tv, params);                 popup.setcontentview(layout);                 // popup.showatlocation(layout, gravity.bottom, 10, 10);                 mainlayout.addview(but, params);                 setcontentview(mainlayout);             }                          }       @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.single, menu);         return true;     }  } 

above code have tried. shows pop-up want content in database displayed. how do it? have copied database assets folder. thank u in advance

i have tried following code databasehelperclass.

import java.io.file; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream;  import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper;  public class databasehelperclass extends sqliteopenhelper {      //the android's default system path of application database.     private static string db_path = "/data/data/package_name/databases/";     // data base name.     private static final string database_name = "db.sqlite";     // data base version.     private static final int database_version = 1;     // table names of data base.     static final string table_name = "tlist";      public context context;     static sqlitedatabase sqlitedatabase;      public databasehelperclass(context context) {                super(context, database_name, null ,database_version);         this.context = context;     }      public void createdatabase() throws ioexception{         //check if database exists         boolean databaseexist = checkdatabase();          if(databaseexist){             // nothing.         }else{             this.createdatabase();                      copydatabase();          }// end if else dbexist     } // end createdatabase().       public boolean checkdatabase(){         file databasefile = new file(db_path + database_name);         return databasefile.exists();             }      private void copydatabase() throws ioexception{          //open local db input stream         inputstream myinput = context.getassets().open(database_name);          // path created empty db         string outfilename = db_path + database_name;          //open empty db output stream         outputstream myoutput = new fileoutputstream(outfilename);          //transfer bytes input file output file         byte[] buffer = new byte[1024];         int length;         while ((length = myinput.read(buffer))>0){             myoutput.write(buffer, 0, length);         }        //close streams         myoutput.flush();         myoutput.close();         myinput.close();       }      /**      * method opens data base connection.      * first create path till data base of device.      * create connection data base.      */     public void opendatabase() throws sqlexception{               //open database         string mypath = db_path + database_name;         sqlitedatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite);       }      /**      * method used close data base connection.      */     public synchronized void close() {          if(sqlitedatabase != null)             sqlitedatabase.close();          super.close();      }      public string getusernamefromdb(){         string query = "select desc "+table_name;         cursor cursor = sqlitedatabase.rawquery(query, null);         string description = null;         if(cursor.getcount()>0){             if(cursor.movetofirst()){         do{                     description = cursor.getstring(0);                 }while (cursor.movetonext());             }         }         return description;     }      public void oncreate(sqlitedatabase db) {         // no need write create table query.         // using pre built data base.         // readonly.     }      public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // no need write update table query.         // using pre built data base.         // readonly.         // should not update requirements of application.     }    } 

got code 1 of links found easy understand. shows no errors too. when try calling getusernamefromdb() method in single.java asks me create getusernamefromdb() method. y so? can't call methods different java class??

i need popup display result of below query. "select description table id=1" how can working??

if want create sqlite database should @ sqliteopenhelper class

in oncreate() method have create tables want use.

@override public void oncreate(sqlitedatabase db) {     db.execsql("create table mytable (_id integer primary key autoincrement,      stuff varchar(100))"); } 

if want insert data table can create method this:

public long insertobject(object o) {     contentvalues cv = new contentvalues();     cv.put("stuff", o.getsomething());      return getwritabledatabase().insert("mytable", null, cv); } 

than can information in form of cursor. in case search specific entry, take first cursor result. if set third , forth entry null, every entry.

public house queryhouse(long id) {     cursor wrapped = getreadabledatabase().query(table_house, null, "_id=?",      new string[]{""+id}, null, null, null);      mycursor c = new mycursor(wrapped);      c.movetofirst();     object o = c.getobject();     c.close();     return o; } 

the mycursor class can like e.g.

public static class mycursor extends cursorwrapper {      public mycursor(cursor cursor) {         super(cursor);     }      public person getobject() {         if (isbeforefirst() || isafterlast())             return null;          object o = new object();          o.setid(getlong(getcolumnindex("column_abc")));         //more           return p;     }  } 

i made a little sqlite test app , put on github. here complete version of sqliteopenhelper class.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -