| 1 | import android.content.Intent; |
| 2 | import android.example.com.basicdemo.R; |
| 3 | import android.support.v7.app.AppCompatActivity; |
| 4 | import android.os.Bundle; |
| 5 | import android.view.View; |
| 6 | import android.widget.Button; |
| 7 | import android.widget.EditText; |
| 8 | import android.widget.Toast; |
| 9 | |
| 10 | public class ExampleSQLite extends AppCompatActivity implements View.OnClickListener { |
| 11 | |
| 12 | DbHelper db; |
| 13 | |
| 14 | EditText editTeasyd ,editTextName ,editTextEmail ,editTextMobile; |
| 15 | Button buttonInsert, buttonView, buttonDelete,buttonUpdate, buttonSearch; |
| 16 | |
| 17 | String id; |
| 18 | String name; |
| 19 | String email; |
| 20 | String mobile; |
| 21 | |
| 22 | |
| 23 | protected void onCreate(Bundle savedInstanceState) { |
| 24 | super.onCreate(savedInstanceState); |
| 25 | setContentView(R.layout.example_sqlite); |
| 26 | |
| 27 | editTeasyd = findViewById(R.id.edit_id); |
| 28 | editTextName = findViewById(R.id.edit_name); |
| 29 | editTextEmail = findViewById(R.id.edit_email); |
| 30 | editTextMobile = findViewById(R.id.edit_mobile); |
| 31 | |
| 32 | buttonInsert = findViewById(R.id.button_insert); |
| 33 | buttonView = findViewById(R.id.button_view); |
| 34 | buttonDelete = findViewById(R.id.button_delete); |
| 35 | buttonUpdate = findViewById(R.id.button_update); |
| 36 | buttonSearch = findViewById(R.id.button_search); |
| 37 | |
| 38 | buttonInsert.setOnClickListener(this); |
| 39 | buttonView.setOnClickListener(this); |
| 40 | buttonDelete.setOnClickListener(this); |
| 41 | buttonUpdate.setOnClickListener(this); |
| 42 | buttonSearch.setOnClickListener(this); |
| 43 | |
| 44 | db=new DbHelper(this); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | public void onClick(View v) { |
| 49 | |
| 50 | switch (v.getId()){ |
| 51 | |
| 52 | case R.id.button_insert: |
| 53 | name=editTextName.getText().toString(); |
| 54 | email=editTextEmail.getText().toString(); |
| 55 | mobile=editTextMobile.getText().toString(); |
| 56 | if(name.equals("") | email.equals("") | mobile.equals("")){ |
| 57 | Toast.makeText(this, "Please fill the Fields", Toast.LENGTH_SHORT).show(); |
| 58 | }else { |
| 59 | db.insertStudent(name,email,mobile); |
| 60 | editTeasyd.setText(""); |
| 61 | editTextName.setText(""); |
| 62 | editTextEmail.setText(""); |
| 63 | editTextMobile.setText(""); |
| 64 | Toast.makeText(this, "saved successfully", Toast.LENGTH_SHORT).show(); |
| 65 | } |
| 66 | break; |
| 67 | |
| 68 | case R.id.button_view: |
| 69 | Intent intent=new Intent(getApplicationContext(),ViewSQLiteData.class); |
| 70 | startActivity(intent); |
| 71 | break; |
| 72 | |
| 73 | case R.id.button_delete: |
| 74 | id = editTeasyd.getText().toString(); |
| 75 | if(id.equals("")){ |
| 76 | Toast.makeText(this, "Plase fill the Id", Toast.LENGTH_SHORT).show(); |
| 77 | }else { |
| 78 | long l = Long.parseLong(id); |
| 79 | db.deleteStudent(l); |
| 80 | editTeasyd.setText(""); |
| 81 | editTextName.setText(""); |
| 82 | editTextEmail.setText(""); |
| 83 | editTextMobile.setText(""); |
| 84 | Toast.makeText(this, "deleted successfully", Toast.LENGTH_SHORT).show(); |
| 85 | } |
| 86 | break; |
| 87 | |
| 88 | case R.id.button_update: |
| 89 | id=editTeasyd.getText().toString().trim(); |
| 90 | name=editTextName.getText().toString(); |
| 91 | email=editTextEmail.getText().toString(); |
| 92 | mobile=editTextMobile.getText().toString(); |
| 93 | if(id.equals("") | name.equals("") | email.equals("") | mobile.equals("")){ |
| 94 | Toast.makeText(this, "Please fill all the fields", Toast.LENGTH_SHORT).show(); |
| 95 | }else { |
| 96 | long l= Long.parseLong(id); |
| 97 | db.updateStudent(l,name,email,mobile); |
| 98 | editTeasyd.setText(""); |
| 99 | editTextName.setText(""); |
| 100 | editTextEmail.setText(""); |
| 101 | editTextMobile.setText(""); |
| 102 | Toast.makeText(this, "updated successfully", Toast.LENGTH_SHORT).show(); |
| 103 | } |
| 104 | break; |
| 105 | case R.id.button_search: |
| 106 | id=editTeasyd.getText().toString().trim(); |
| 107 | if(id.equals("")){ |
| 108 | Toast.makeText(this, "Please Fill the Id", Toast.LENGTH_SHORT).show(); |
| 109 | }else { |
| 110 | try { |
| 111 | long l1= Long.parseLong(id); |
| 112 | name=db.getName(l1); |
| 113 | email=db.getEmail(l1); |
| 114 | mobile=db.getMobile(l1); |
| 115 | |
| 116 | editTextName.setText(name); |
| 117 | editTextEmail.setText(email); |
| 118 | editTextMobile.setText(mobile); |
| 119 | Toast.makeText(this, "searched successfully", Toast.LENGTH_SHORT).show(); |
| 120 | |
| 121 | } |
| 122 | catch (Exception e) |
| 123 | { |
| 124 | Toast.makeText(this, "Id is not Available", Toast.LENGTH_SHORT).show(); |
| 125 | } |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 |
DbHelper.java
| 1 | import android.content.ContentValues; |
| 2 | import android.content.Context; |
| 3 | import android.database.Cursor; |
| 4 | import android.database.sqlite.SQLiteDatabase; |
| 5 | import android.database.sqlite.SQLiteOpenHelper; |
| 6 | |
| 7 | public class DbHelper extends SQLiteOpenHelper { |
| 8 | SQLiteDatabase db; |
| 9 | |
| 10 | private static final String DATABASE_NAME="database.db"; |
| 11 | private static final int DATABASE_VERSION=1; |
| 12 | |
| 13 | private static final String TABLE_STUDENT="_student"; |
| 14 | |
| 15 | private static final String KEY_ID="id"; |
| 16 | private static final String KEY_NAME="name"; |
| 17 | private static final String KEY_EMAIL="email"; |
| 18 | private static final String KEY_MOBILE="mobile"; |
| 19 | |
| 20 | public DbHelper(Context context) { |
| 21 | |
| 22 | super(context, DATABASE_NAME, null,DATABASE_VERSION); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | public void onCreate(SQLiteDatabase db){ |
| 27 | String Query_Table=" CREATE TABLE " +TABLE_STUDENT+ "(" |
| 28 | +KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 29 | +KEY_NAME+ " TEXT, " +KEY_EMAIL+ " TEXT, " +KEY_MOBILE+ " TEXT);"; |
| 30 | db.execSQL(Query_Table); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
| 35 | db.execSQL("DROP TABLE IF EXISTS"+TABLE_STUDENT); |
| 36 | onCreate(db); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | public long insertStudent(String name, String email, String mobile) { |
| 41 | db=this.getWritableDatabase(); |
| 42 | ContentValues values=new ContentValues(); |
| 43 | values.put(KEY_NAME,name); |
| 44 | values.put(KEY_EMAIL,email); |
| 45 | values.put(KEY_MOBILE,mobile); |
| 46 | return db.insert(TABLE_STUDENT,null,values); |
| 47 | } |
| 48 | |
| 49 | public String getData() { |
| 50 | db=this.getReadableDatabase(); |
| 51 | String[] columns=new String[] {KEY_ID,KEY_NAME,KEY_EMAIL,KEY_MOBILE}; |
| 52 | Cursor cursor=db.query(TABLE_STUDENT,columns,null,null,null,null,null); |
| 53 | |
| 54 | int iId= cursor.getColumnIndex(KEY_ID); |
| 55 | int iName= cursor.getColumnIndex(KEY_NAME); |
| 56 | int iEmail= cursor.getColumnIndex(KEY_EMAIL); |
| 57 | int iMobile= cursor.getColumnIndex(KEY_MOBILE); |
| 58 | String result=""; |
| 59 | |
| 60 | for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){ |
| 61 | result=result+ |
| 62 | "Id: "+cursor.getString(iId)+ "\n"+ |
| 63 | "Name: " +cursor.getString(iName)+"\n"+ |
| 64 | "Email: " +cursor.getString(iEmail)+ "\n"+ |
| 65 | "Mobile: "+cursor.getString(iMobile)+ "\n\n"; |
| 66 | } |
| 67 | db.close(); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | public void deleteStudent(long l) { |
| 72 | db=this.getWritableDatabase(); |
| 73 | db.delete(TABLE_STUDENT,KEY_ID+"="+l,null); |
| 74 | } |
| 75 | |
| 76 | public void updateStudent(long l, String name, String email, String mobile) { |
| 77 | db=this.getWritableDatabase(); |
| 78 | ContentValues values=new ContentValues(); |
| 79 | values.put(KEY_NAME,name); |
| 80 | values.put(KEY_EMAIL,email); |
| 81 | values.put(KEY_MOBILE,mobile); |
| 82 | db.update(TABLE_STUDENT,values,KEY_ID+"="+l,null); |
| 83 | db.close(); |
| 84 | } |
| 85 | |
| 86 | public String getName(long l1) { |
| 87 | db=this.getReadableDatabase(); |
| 88 | String[] columns=new String[]{KEY_ID,KEY_NAME,KEY_EMAIL,KEY_MOBILE}; |
| 89 | Cursor cursor=db.query(TABLE_STUDENT,columns,KEY_ID+"="+l1,null,null,null,null); |
| 90 | if(cursor!=null){ |
| 91 | cursor.moveToFirst(); |
| 92 | String name=cursor.getString(1); |
| 93 | return name; |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | public String getEmail(long l1) { |
| 99 | db=this.getReadableDatabase(); |
| 100 | String[] columns=new String[]{KEY_ID,KEY_NAME,KEY_EMAIL,KEY_MOBILE}; |
| 101 | Cursor cursor=db.query(TABLE_STUDENT,columns,KEY_ID+"="+l1,null,null,null,null); |
| 102 | if(cursor!=null){ |
| 103 | cursor.moveToFirst(); |
| 104 | String name=cursor.getString(2); |
| 105 | return name; |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | public String getMobile(long l1) { |
| 111 | db=this.getReadableDatabase(); |
| 112 | String[] columns=new String[]{KEY_ID,KEY_NAME,KEY_EMAIL,KEY_MOBILE}; |
| 113 | Cursor cursor=db.query(TABLE_STUDENT,columns,KEY_ID+"="+l1,null,null,null,null); |
| 114 | if(cursor!=null){ |
| 115 | cursor.moveToFirst(); |
| 116 | String name=cursor.getString(3); |
| 117 | return name; |
| 118 | } |
| 119 | return null; |
| 120 | } |
| 121 | } |
| 122 |
ViewSQLiteData.java
| 1 | import android.support.v7.app.AppCompatActivity; |
| 2 | import android.os.Bundle; |
| 3 | import android.text.method.ScrollingMovementMethod; |
| 4 | import android.widget.TextView; |
| 5 | |
| 6 | public class ViewSQLiteData extends AppCompatActivity { |
| 7 | |
| 8 | |
| 9 | protected void onCreate(Bundle savedInstanceState) { |
| 10 | super.onCreate(savedInstanceState); |
| 11 | setContentView(R.layout.view_sqlite_data); |
| 12 | |
| 13 | TextView textView = findViewById(R.id.view_data); |
| 14 | |
| 15 | DbHelper db = new DbHelper(this); |
| 16 | |
| 17 | String data = db.getData(); |
| 18 | textView.setText(data); |
| 19 | textView.setMovementMethod(new ScrollingMovementMethod()); |
| 20 | } |
| 21 | } |
| 22 |
main_activity.xml
| 1 | <LinearLayout |
| 2 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 3 | xmlns:app="http://schemas.android.com/apk/res-auto" |
| 4 | xmlns:tools="http://schemas.android.com/tools" |
| 5 | android:layout_width="match_parent" |
| 6 | android:layout_height="match_parent" |
| 7 | android:orientation="vertical" |
| 8 | android:padding="16dp" |
| 9 | tools:context=".sqliteDemo.ExampleSQLite"> |
| 10 | |
| 11 | <EditText |
| 12 | android:id="@+id/edit_id" |
| 13 | android:layout_width="match_parent" |
| 14 | android:layout_height="wrap_content" |
| 15 | android:hint="Id" |
| 16 | android:layout_margin="10dp" |
| 17 | android:inputType="number"/> |
| 18 | |
| 19 | <EditText |
| 20 | android:id="@+id/edit_name" |
| 21 | android:layout_width="match_parent" |
| 22 | android:layout_height="wrap_content" |
| 23 | android:hint="Name" |
| 24 | android:layout_margin="5dp" |
| 25 | android:inputType="text"/> |
| 26 | |
| 27 | <EditText |
| 28 | android:id="@+id/edit_email" |
| 29 | android:layout_width="match_parent" |
| 30 | android:layout_height="wrap_content" |
| 31 | android:hint="Email" |
| 32 | android:layout_margin="5dp" |
| 33 | android:inputType="textEmailAddress"/> |
| 34 | |
| 35 | <EditText |
| 36 | android:id="@+id/edit_mobile" |
| 37 | android:layout_width="match_parent" |
| 38 | android:layout_height="wrap_content" |
| 39 | android:hint="Mobile" |
| 40 | android:layout_margin="5dp" |
| 41 | android:inputType="phone"/> |
| 42 | |
| 43 | <LinearLayout |
| 44 | android:layout_width="match_parent" |
| 45 | android:layout_height="wrap_content" |
| 46 | android:orientation="horizontal"> |
| 47 | |
| 48 | <Button |
| 49 | android:id="@+id/button_insert" |
| 50 | android:layout_width="wrap_content" |
| 51 | android:layout_height="wrap_content" |
| 52 | android:text="save" |
| 53 | android:textSize="20sp" |
| 54 | android:layout_weight="1" |
| 55 | android:layout_marginTop="20dp"/> |
| 56 | |
| 57 | <Button |
| 58 | android:id="@+id/button_view" |
| 59 | android:layout_width="wrap_content" |
| 60 | android:layout_height="wrap_content" |
| 61 | android:text="view" |
| 62 | android:textSize="20sp" |
| 63 | android:layout_weight="1" |
| 64 | android:layout_marginTop="20dp"/> |
| 65 | </LinearLayout> |
| 66 | |
| 67 | <LinearLayout |
| 68 | android:layout_width="match_parent" |
| 69 | android:layout_height="wrap_content" |
| 70 | android:orientation="horizontal"> |
| 71 | |
| 72 | <Button |
| 73 | android:id="@+id/button_delete" |
| 74 | android:layout_width="wrap_content" |
| 75 | android:layout_height="wrap_content" |
| 76 | android:text="delete" |
| 77 | android:textSize="20sp" |
| 78 | android:layout_weight="1" |
| 79 | android:layout_marginTop="20dp"/> |
| 80 | |
| 81 | <Button |
| 82 | android:id="@+id/button_update" |
| 83 | android:layout_width="wrap_content" |
| 84 | android:layout_height="wrap_content" |
| 85 | android:text="update" |
| 86 | android:textSize="20sp" |
| 87 | android:layout_weight="1" |
| 88 | android:layout_marginTop="20dp"/> |
| 89 | </LinearLayout> |
| 90 | |
| 91 | <Button |
| 92 | android:id="@+id/button_search" |
| 93 | android:layout_width="match_parent" |
| 94 | android:layout_height="wrap_content" |
| 95 | android:text="search" |
| 96 | android:textSize="20sp" |
| 97 | android:layout_marginTop="20dp"/> |
| 98 | |
| 99 | </LinearLayout> |
| 100 |
view_sqlite_data.xml
| 1 | <RelativeLayout |
| 2 | xmlns:android="http://schemas.android.com/apk/res/android" |
| 3 | xmlns:app="http://schemas.android.com/apk/res-auto" |
| 4 | xmlns:tools="http://schemas.android.com/tools" |
| 5 | android:layout_width="match_parent" |
| 6 | android:layout_height="match_parent" |
| 7 | android:padding="16dp" |
| 8 | tools:context=".ViewSQLiteData"> |
| 9 | |
| 10 | <TextView |
| 11 | android:id="@+id/view_data" |
| 12 | android:layout_width="match_parent" |
| 13 | android:layout_height="match_parent" |
| 14 | android:textSize="20sp" |
| 15 | android:textColor="@android:color/black" |
| 16 | android:scrollbars="vertical"/> |
| 17 | |
| 18 | </RelativeLayout> |
| 19 |
Tidak ada komentar:
Posting Komentar