Lab 6 Sqlite Part1

 Lab6 01 Layout

1.    Step 1: Launch Android Studio.  New Project

Fill the info as shown below :




Step 2: Select Phone and Tablet. Click Next ;
            Choose Empty Activity. Click Next
            Use default name as MainActivity. Click Finish


Step 3: Click on the activity_main.xml layout


















Drag in a linearLayout (vertical) and click infer constraint

Create the followings:
1 linearLayout  (vertical)                 
           
1 TextView tvTitle
android:text="Telephone Directory"  
1 EditText    etName
android:hint="Enter name:"          

1 EditTextetTelNum
android:hint="Enter Tel Number"


1 linearLayout  (horizontal)       
android:layout_width="match_parent"
android:layout_height="wrap_content"          
2 Buttons btnInsert and  btnViewData
                     
1 TextView tvStatus
android:text="status" 

Use proper naming convention (btn for Button, tv for TextView, et for EditText)


Test it out


 Lab6 01a Listener for Button

Step 4: Add a Sqlite Database in MainActivity.java

Place the codes in the correct sections  Here are the partial codes:

Declare the following class variables
//#1 Declare Class variables

SQLiteDatabase db;
Button btnInsert;
EditText etName, etTel;
TextView tvStatus;



Inside the onCreate method
//#2 Binding Java to XML

tvStatus = (TextView) findViewById(R.id.tvStatus);
btnInsert = (Button) findViewById(R.id.btnInsert);
etName = (EditText) findViewById(R.id.etName);
etTel = (EditText) findViewById(R.id.etTelNum);



After binding, add the listener for the 2 buttons.

Inside the onCreate method
//#3  ClickListener for btnInsert

  btnInsert.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
 

       Toast.makeText(getApplicationContext(),"Inserted",Toast.LENGTH_LONG).show();
    }

});

  //========btnInsert=========



Repeat the above for the listener for btnViewData (remember to do binding else you will encounter NullPointer exception)


Test it out

Lab6 02Insert

Step 1: Add a new Java Class called SqlLiteHelper













Step 2: //Create a executeSQL method Within the SqlLiteHelper class

public class SqlLiteHelper  {

    //Create a method executeSQL for Insert, Delete , Update

    public String executeSQL(SQLiteDatabase db,String sql) {

        try {

            db.beginTransaction();
            db.execSQL(sql);
            db.setTransactionSuccessful();
            db.endTransaction();

        } catch (Exception e) {

            System.out.println(e.toString());
            return ("Error updating DB");
        }

        return ("DB updated");

    }


}



Step 3: Edit  MainActivity.java

Declare the sqlh as class variable

 SqlLiteHelper  sqlh = new SqlLiteHelper(); //this create an object from SqlLiteHelper class
                                                                           
                                                         


Add the following codes before //#2 Binding Java to XML

Inside the onCreate method

      //#1 Create Database file name TelDirectory.db and Table name TelDirList
         db = openOrCreateDatabase("TelDirectory.db", MODE_PRIVATE, null);

        String sqlStatement = "create table if not exists TelDirList (recId integer PRIMARY KEY autoincrement, Name text, Tel text)";
        sqlh.executeSQL(db, sqlStatement);



Inside the onClick method of btnInsert, add the following codes:


  tvStatus.setText(""); //clear the text on tvStatus
  String name = etName.getText().toString(); //get text from EditText etName
  String tel = etTel.getText().toString();//get text from EditText etTel
  
  String sqlStatement = "insert into TelDirList (Name ,Tel) values( '" + name + "','" + tel + "')";

  String result =   sqlh.executeSQL(db, sqlStatement); //execute the Insert SQL command
  tvStatus.setText(result); //show the status by using setText on tvStatus



Test it out





No comments:

Post a Comment

Note: only a member of this blog may post a comment.