Last active
June 11, 2017 22:21
-
-
Save Bahaaib/de3d94cb1e2a5bd3f87a8496fb4b04da to your computer and use it in GitHub Desktop.
Create a basic SQLite Database and store simple data into.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.robpercival.databasedemo; | |
| import android.app.Activity; | |
| import android.database.Cursor; // new lib. | |
| import android.database.sqlite.SQLiteDatabase; // new lib. | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| public class MainActivity extends Activity { | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| try { // must be surrounded by try/catch just in case of Database crash | |
| SQLiteDatabase eventsDB = this.openOrCreateDatabase("Events", MODE_PRIVATE, null); // Initialize A Database | |
| eventsDB.execSQL("CREATE TABLE IF NOT EXISTS events (event VARCHAR, year INT(4))"); // Create the Database and initialize it with a VARCHAR & INT values | |
| eventsDB.execSQL("INSERT INTO events (event, year) VALUES ('End Of WW2', 1945)"); // Insert Values into the cells created | |
| eventsDB.execSQL("INSERT INTO events (event, year) VALUES ('Wham split up', 1986)"); // insert another values | |
| Cursor c = eventsDB.rawQuery("SELECT * FROM events", null); // Launch the Database cursor | |
| int eventIndex = c.getColumnIndex("event"); // get the column "event" index and store into eventIndex | |
| int yearIndex = c.getColumnIndex("year"); // get the column "year" index and store into yearIndex | |
| c.moveToFirst(); // let the Database cursor move to first column | |
| while (c != null) { // while the DB cursor doesn't refer to an empty column, then excute.. | |
| Log.i("Results - event", c.getString(eventIndex)); | |
| Log.i("Results - year", Integer.toString(c.getInt(yearIndex))); | |
| c.moveToNext(); // let the Database cursor move to the next column | |
| } | |
| } | |
| catch (Exception e) { | |
| e.printStackTrace(); //General Exception | |
| } | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.menu_main, menu); | |
| return true; | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| // Handle action bar item clicks here. The action bar will | |
| // automatically handle clicks on the Home/Up button, so long | |
| // as you specify a parent activity in AndroidManifest.xml. | |
| int id = item.getItemId(); | |
| //noinspection SimplifiableIfStatement | |
| if (id == R.id.action_settings) { | |
| return true; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment