Advertisement
gPhoenix

MainActivity

Apr 26th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package geezy.fulcrum.database_test;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.database.sqlite.*;
  8.  
  9.  
  10. public class MainActivity extends ActionBarActivity {
  11.  
  12.     // private fields of the class
  13.     private TestDBOpenHelper tdb;
  14.     private SQLiteDatabase sdb;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         // get access to an sqlite database
  21.         tdb = new TestDBOpenHelper(this, "test.db", null, 1);
  22.         sdb = tdb.getWritableDatabase();
  23.  
  24.         // name of the table to query
  25.         String table_name = "test";
  26.         // the columns that we wish to retrieve from the tables
  27.         String[] columns = {"ID_PLAYLIST", "NAME"};
  28.         // where clause of the query. DO NOT WRITE WHERE IN THIS
  29.         String where = null;
  30.         // arguments to provide to the where clause
  31.         String where_args[] = null;
  32.         // group by clause of the query. DO NOT WRITE GROUP BY IN THIS
  33.         String group_by = null;
  34.         // having clause of the query. DO NOT WRITE HAVING IN THIS
  35.         String having = null;
  36.         // order by clause of the query. DO NOT WRITE ORDER BY IN THIS
  37.         String order_by = null;
  38.         // run the query. this will give us a cursor into the database
  39.         // that will enable us to change the table row that we are working with
  40.         Cursor c = sdb.query(table_name, columns, where, where_args, group_by,
  41.                 having, order_by);
  42.         // print out some data from the cursor to the screen
  43.         TextView tv = (TextView) findViewById(R.id.tv);
  44.         String total_text = "total number of rows: " + c.getCount() + "\n";
  45.         c.moveToFirst();
  46.         for(int i = 0; i < c.getCount(); i++) {
  47.             total_text += c.getInt(0) + " " + c.getString(1) + "\n";
  48.             c.moveToNext();
  49.         }
  50.         tv.setText(total_text);
  51.     }
  52.  
  53.  
  54.     @Override
  55.     public boolean onCreateOptionsMenu(Menu menu) {
  56.         // Inflate the menu; this adds items to the action bar if it is present.
  57.         getMenuInflater().inflate(R.menu.menu_main, menu);
  58.         return true;
  59.     }
  60.  
  61.     @Override
  62.     public boolean onOptionsItemSelected(MenuItem item) {
  63.         // Handle action bar item clicks here. The action bar will
  64.         // automatically handle clicks on the Home/Up button, so long
  65.         // as you specify a parent activity in AndroidManifest.xml.
  66.         int id = item.getItemId();
  67.  
  68.         //noinspection SimplifiableIfStatement
  69.         if (id == R.id.action_settings) {
  70.             return true;
  71.         }
  72.  
  73.         return super.onOptionsItemSelected(item);
  74.     }
  75.  
  76.     // overridden method that will clear out the contents of the database
  77.     protected void onDestroy() {
  78.         super.onDestroy();
  79.         // run a query that will delete all the rows in our database
  80.         String table = "test";
  81.         String where = null;
  82.         String where_args[] = null;
  83.         sdb.delete(table, where, where_args);
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement