Android Open Source - CSCI567---Workspace D B Helper






From Project

Back to project page CSCI567---Workspace.

License

The source code is released under:

MIT License

If you think the Android project CSCI567---Workspace listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package csci567.simpledbexample;
/*from   w ww. j  a  va  2  s  . c  o  m*/
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper{
  final static String DB_NAME = "example.db";
  final static int DB_VERSION = 1;
  private final String EXAMPLE_TABLE = "configTable";
  Context context;
  
  public DBHelper(Context context){    
    super(context, DB_NAME, null, DB_VERSION);
    this.context=context;
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS " + EXAMPLE_TABLE + " (text VARCHAR);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    
  }
  
  public boolean insertText(String text){
    try{
      //DBHelper appDB = new DBHelper(context);
      SQLiteDatabase qdb = this.getWritableDatabase();
      Log.d("DB Insert: ", "INSERT OR REPLACE INTO " +
            EXAMPLE_TABLE + " (text) Values ("+ text + ");");
      qdb.execSQL("INSERT OR REPLACE INTO " +
            EXAMPLE_TABLE + " (text) Values (\""+ text + "\");");
      qdb.close();
    }
    catch(SQLiteException se){
      Log.d("DB Insert Error: ",se.toString());
      return false;
    }
    return true;
  }
  public String getText(){
    String toReturn = "";
    try{
      //DBHelper appDB = new DBHelper(context);
      SQLiteDatabase qdb = this.getReadableDatabase();
      qdb.execSQL("CREATE TABLE IF NOT EXISTS " + EXAMPLE_TABLE + " (text VARCHAR);");
      Cursor c = qdb.rawQuery("SELECT * FROM " +
            EXAMPLE_TABLE, null);
      if (c != null ) {
          if  (c.moveToFirst()) {
            do {
              String text = c.getString(c.getColumnIndex("text"));
              toReturn += text + "\n";
            }
            while (c.moveToNext());
          }
      }
      qdb.close(); 
    }
    catch(SQLiteException se){
      Log.d("DB Select Error: ",se.toString());
      return "";
    }
    return toReturn;
  }

}




Java Source Code List

com.example.linearlayoutexample.MainActivity.java
com.example.relativelayoutexample.MainActivity.java
csci567.FragmentExample.Fragmentmanageractivity.java
csci567.FragmentExample.MainActivity.java
csci567.FragmentExample.MyFragment.java
csci567.FragmentExample.MyStaticFragment.java
csci567.alarmexample.MainActivity.java
csci567.alarmexample.SampleAlarmReceiver.java
csci567.asynctaskexample.MainActivity.java
csci567.buttonexample.MainActivity.java
csci567.checkboxexample.MainActivity.java
csci567.doodleexample.MainActivity.java
csci567.doodleexample.SampleAlarmReceiver.java
csci567.doodleexample.SampleBootReceiver.java
csci567.doodleexample.SampleSchedulingService.java
csci567.eventreceiver.DataReceiver.java
csci567.eventreceiver.MainActivity.java
csci567.eventreceiver.RebootReceiver.java
csci567.helloworld.MainActivity.java
csci567.simpledbexample.DBHelper.java
csci567.simpledbexample.MainActivity.java
csci567.suggestionapp.MainActivity.java
csci567.writefile.MainActivity.java
org.ndeftools.boilerplate.AndroidNfcActivity.java
org.ndeftools.boilerplate.DefaultNfcBeamWriterActivity.java
org.ndeftools.boilerplate.DefaultNfcReaderActivity.java
org.ndeftools.boilerplate.DefaultNfcTagWriterActivity.java
org.ndeftools.boilerplate.NdefRecordAdapter.java
org.ndeftools.nfcdemo.TagViewer.java
org.ndeftools.nfcdemo.simulator.FakeTagsActivity.java
org.ndeftools.nfcdemo.simulator.MockNdefMessages.java