android database mini app


Create a new class called DataManager and add the following member variables:

public class DataManager {
    // This is the actual database    
private SQLiteDatabase db;

/* Next we have a public static final string for  each row/table that we need to refer to both inside and outside this class    */
public static final String TABLE_ROW_ID = "_id";    
public static final String TABLE_ROW_NAME = "name";    
public static final String TABLE_ROW_AGE = "age";

/*Next we have a private static final strings for each row/table that we need to refer to just        inside this class   */
private static final String DB_NAME = "address_book_db";    
private static final int DB_VERSION = 1;    
private static final String TABLE_N_AND_A = "names_and_addresses"; 

}

/*Add following constructor that will create an instance of our custom version of SQLiteOpenHelper. */

public DataManager(Context context) {  /

//Create an instance of our internal CustomSQLiteOpenHelper class  
CustomSQLiteOpenHelper helper =      new CustomSQLiteOpenHelper(context);

  // Get a writable database  
db = helper.getWritableDatabase(); } 

/*Now, we can add the helper methods that we will access from MainActivity. First, the insert method that executes an INSERT SQL query based on the name and age parameters that are passed into the method. */


//Add the insert method to the DataManager class:


 public void insert(String name, String age){
  // Add all the details to the table  
String query = "INSERT INTO " + TABLE_N_AND_A + " (" +      TABLE_ROW_NAME + ", " +      TABLE_ROW_AGE + ") " +      "VALUES (" +      "'" + name + "'" + ", " +      "'" + age + "'" +      ");      ";

  Log.i("insert() = ", query);
  db.execSQL(query);
} 

//Add the delete method to the DataManager class:

public void delete(String name){  // Delete the details from the table if already exists  

String query = "DELETE FROM " + TABLE_N_AND_A +      " WHERE " + TABLE_ROW_NAME +      " = '" + name + "';";
  Log.i("delete() = ", query);
  db.execSQL(query);


// To get all the records 

public Cursor selectAll() {  

Cursor c = db.rawQuery("SELECT *" +" from " +      TABLE_N_AND_A, null);
  return c; 
}


//To search specific record

 public Cursor searchName(String name) {  
String query = "SELECT " +      TABLE_ROW_ID + ", " +      TABLE_ROW_NAME +      ", " + TABLE_ROW_AGE +      " from " +      TABLE_N_AND_A + " WHERE " +      TABLE_ROW_NAME + " = '" + name + "';";
  Log.i("searchName() = ", query);
  Cursor c = db.rawQuery(query, null);
  return c; 
}

/*
Finally, for the DataManager class, we create an inner class that will be our implementation of SQLiteOpenHelper 
*/

Add the inner CustomSQLiteOpenHelper class to the DataManager class:

// This class is created when our DataManager is initialized 

private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {  public CustomSQLiteOpenHelper(Context context) {    
super(context, DB_NAME, null, DB_VERSION);  }

  // This method only runs the first time the database is created  

@Override  
public void onCreate(SQLiteDatabase db) {
  
  // Create a table for photos and all their details    

String newTableQueryString = "create table "        + TABLE_N_AND_A + " ("        + TABLE_ROW_ID        + " integer primary key autoincrement not null,"        + TABLE_ROW_NAME        + " text not null,"        + TABLE_ROW_AGE        + " text not null);";
    db.execSQL(newTableQueryString);      }
  
// This method only runs when we increment DB_VERSION  

@Override  
public void onUpgrade(SQLiteDatabase db,   int oldVersion, int newVersion) {
  }
}



Coding MainActivity


public class MainActivity extends AppCompatActivity    implements View.OnClickListener{

    // For all our buttons and edit text    

Button btnInsert;    
Button btnDelete;    
Button btnSelect;    
Button btnSearch;
EditText editName;    
EditText editAge;    
EditText editDelete;    
EditText editSearch;

    // This is our DataManager instance    
DataManager dm;
    @Override    
protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);  }  
dm=new Database(Context:this);
...

  // get a reference to the UI item  
btnInsert = (Button) findViewById(R.id.btnInsert);  
btnDelete = (Button) findViewById(R.id.btnDelete);  
btnSelect = (Button) findViewById(R.id.btnSelect);  
btnSearch = (Button) findViewById(R.id.btnSearch);
editName = (EditText) findViewById(R.id.editName);  
editAge = (EditText) findViewById(R.id.editAge);  
editDelete = (EditText) findViewById(R.id.editDelete);  
editSearch = (EditText) findViewById(R.id.editSearch);
  
// Register MainActivity as a listener  
btnSelect.setOnClickListener(this);  
btnInsert.setOnClickListener(this);  
btnDelete.setOnClickListener(this);  
btnSearch.setOnClickListener(this); 


/*Add the showData method to MainActivity*/


// Output the cursor contents to the log 

public void showData(Cursor c){
  while (c.moveToNext()){    
Log.i(c.getString(1), c.getString(2));  
}

Add the onClick method to the MainActivity class

@Override 
public void onClick(View v){
  switch (v.getId()){    
case R.id.btnInsert:      
dm.insert(editName.getText().toString(),
editAge.getText().toString());
      break;
    case R.id.btnSelect:      showData(dm.selectAll());      break;
    case R.id.btnSearch:      showData(dm.searchName(editSearch.getText().toString()));      break;
    case R.id.btnDelete:      dm.delete(editDelete.getText().toString());      break;
  }
}




Reactions:

0 comments:

Post a Comment