Prerequisite
If you not understand this lecture. Android SQLite database Update partial row.You should also read my previous lectures Android how can add data into SQLite and Android SQLite how can retrieve data and Android SQLite get a single row from table And Delete row from android SQLite database. After reading these lecture you will also able Update a single row from database.
Introduction
In this lecture well we describe how can update partial row from Android SQLite Database.And also describe what is SQLite. why we can use it.After Reading this lecture you will understand how can update row from android SQLite Database . And i hope you will create Database of your application and also update row from database after reading this series.
What is Android SQLite database
SQLite is a embedded (RDBMS) Stand for Relation Database Management system.Most relation database such as ORA-CAL and SQL are example that are working independently and store the information .SQLite is referred to as embedded because it is provided in the form of a library that is linked into applications.The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private.There is no sever that are running in the background.All the Database function are perform internally.
Description of Update a Row
In this lecture we learn about how can update a partial row from android SQlite database.when user click on update button a new activity will open in this activity user can search a particular name. Enter a name in edit text and click on button particular information related to this name are appears And user can update like user name mobile number and email address.And click on a update button all the information will be update.So this is a work that will done this section.please read this lecture carefully all the detail is provides step by step.For more detail about android SQLite Database how can update information please click on update android SQlite database.
Layout XML BASIC
Different property that are used in layout.xml file explain here
- warp_content warp_content means height and width similar to the text that are entered.
- Match_parent means height and width similar to parent.
- Edit text The Edit Text control allows a user to enter text into an application
- Button When user click on a button show information
XML Layout
Write the following code activity_main_layout. XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".LinearLayout" android:background="#000000" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendMessage" android:text="@string/button1" android:layout_gravity="center" /> <Button android:id="@+id/view_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showData" android:text="@string/view_data" android:layout_gravity="center" /> <Button android:id="@+id/search_data" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="ViewData" android:text="@string/search_data" android:layout_gravity="center" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="NextUpdate" android:text="@string/button2" android:layout_gravity="center" /> </LinearLayout>
Now we create a new Layout
- Step 1=<- create a new name update_search_layout.xml> the ->layout folder-> select ->New option-> and click>layout resource folder->
- Step2=Step1->WriteName->Ok
- In this example we are going to create a simple UI that includes only (Text view) with Different attributes.And Write the following code into update_search_layout.xml.XML file
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#A47DED" > <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/name_search" android:layout_alignParentLeft="true" android:hint="Enter name" /> <Button android:layout_width="125dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/name_search" android:text="Search" android:onClick="SearchContant" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Update" android:id="@+id/title_name" android:layout_below="@+id/name_search" android:textAppearance="?android:textAppearanceLarge" android:gravity="center" /> <EditText android:id="@+id/new_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="@string/new_name" android:layout_alignParentLeft="true" android:layout_below="@+id/title_name" /> <EditText android:id="@+id/mobile_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="@string/mobile_name" android:layout_alignParentLeft="true" android:layout_below="@+id/new_name" /> <EditText android:id="@+id/new_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="@string/new_email" android:layout_alignParentLeft="true" android:layout_below="@+id/mobile_name" /> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:text="@string/update_button4" android:id="@+id/update_button4" android:layout_marginTop="50dp" android:onClick="UpadeContact" android:layout_below="@+id/new_email" android:layout_centerHorizontal="true" /> </RelativeLayout>
- Step3=Create new class <-UpdateSearchActivity ->Right click-> the ->package name ->select ->New option-> and click-> java class that will show in above.
- Step4=Step3->WriteName->Ok
- And Write the following code UpdateSearchActivity. java class
package customspinner.alienslab.com.adddata; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import static customspinner.alienslab.com.adddata.R.id.new_name; /** * Created by MUhammad on 7/1/2015. */ public class UpdateContantActivity extends ActionBarActivity { TextView title_name; EditText Name_Sarch,New_Name,Mobile_Name,Email_Name; String NameSearch,NewName,MobileName,EmailName; SQLiteDatabase sqLiteDatabase; UserDbHelper userDbHelper; Button UpdateButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.update_search_contant); Name_Sarch=(EditText)findViewById(R.id.name_search); New_Name=(EditText)findViewById(R.id.new_name); Mobile_Name=(EditText)findViewById(R.id.mobile_name); Email_Name=(EditText)findViewById(R.id.new_email); UpdateButton=(Button)findViewById(R.id.update_button4); title_name=(TextView)findViewById(R.id.title_name); New_Name.setVisibility(View.GONE); Mobile_Name.setVisibility(View.GONE); Email_Name.setVisibility(View.GONE); UpdateButton.setVisibility(View.GONE); title_name.setVisibility(View.GONE); } public void SearchContant(View view) { NameSearch = Name_Sarch.getText().toString(); userDbHelper = new UserDbHelper(getApplicationContext()); sqLiteDatabase = userDbHelper.getReadableDatabase(); Cursor cursor = userDbHelper.getContact(NameSearch, sqLiteDatabase); if (cursor.moveToFirst()) { MobileName = cursor.getString(0); EmailName = cursor.getString(1); NewName = NameSearch; New_Name.setText(NewName); Mobile_Name.setText(MobileName); Email_Name.setText(EmailName); New_Name.setVisibility(View.VISIBLE); Mobile_Name.setVisibility(View.VISIBLE); Email_Name.setVisibility(View.VISIBLE); UpdateButton.setVisibility(View.VISIBLE); title_name.setVisibility(View.VISIBLE); } } public void UpadeContact(View view){ userDbHelper=new UserDbHelper(getApplicationContext()); sqLiteDatabase=userDbHelper.getWritableDatabase(); String name,mobile,email; name=New_Name.getText().toString(); mobile=Mobile_Name.getText().toString(); email=Email_Name.getText().toString(); int count= userDbHelper.updateinformation(NameSearch,name,mobile,email,sqLiteDatabase); Toast.makeText(getApplication(),count+"UpdateContact",Toast.LENGTH_SHORT).show(); finish(); } }And Write the following code UserDbHandler. java class
package customspinner.alienslab.com.adddata; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; /** * Created by MUhammad zubair on 6/22/2015. */ public class UserDbHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "USERINFO.DB"; private static final int DATABASE_VERSION = 5; private static final String CREATE_QUERY = "CREATE TABLE " + UserContract.NewUserInfo.TABLE_NAME + "(id INTEGER, " + UserContract.NewUserInfo.USER_NAME + " TEXT, " + UserContract.NewUserInfo.USER_MOB + " TEXT, " + UserContract.NewUserInfo.USER_EMAIL + " TEXT );"; public UserDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); Log.e("DATABASE OPERATION", "Database created / opened....."); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_QUERY); Log.e("DATABASE OPERATION", "Table create..." + CREATE_QUERY); } public void addinnformation(String name, String mob, String email, SQLiteDatabase db) { ContentValues contentValue = new ContentValues(); contentValue.put(UserContract.NewUserInfo.USER_NAME, name); contentValue.put(UserContract.NewUserInfo.USER_MOB, mob); contentValue.put(UserContract.NewUserInfo.USER_EMAIL, email); db.insert(UserContract.NewUserInfo.TABLE_NAME, null, contentValue); Log.e("DATABASE OPERATION", "One row is insert"); } public Cursor getinformation(SQLiteDatabase db) { Cursor cursor; String[] projections = {UserContract.NewUserInfo.USER_NAME, UserContract.NewUserInfo.USER_MOB, UserContract.NewUserInfo.USER_EMAIL}; cursor = db.query(UserContract.NewUserInfo.TABLE_NAME, projections, null, null, null, null, null); return cursor; } public Cursor getContact(String user_name, SQLiteDatabase sqLiteDatabase) { String[] projections = {UserContract.NewUserInfo.USER_MOB, UserContract.NewUserInfo.USER_EMAIL}; String selection = UserContract.NewUserInfo.USER_NAME+" LIKE ?"; String[] seletion_args={user_name}; Cursor cursor=sqLiteDatabase.query(UserContract.NewUserInfo.TABLE_NAME,projections,selection,seletion_args,null,null,null); return cursor; } public void deleteinformation(String user_name,SQLiteDatabase sqLiteDatabase){ String selection = UserContract.NewUserInfo.USER_NAME+" LIKE ?"; String[] seletion_args={user_name}; sqLiteDatabase.delete(UserContract.NewUserInfo.TABLE_NAME,selection,seletion_args); } public int updateinformation(String old_name,String new_name,String mobile_name,String email_name,SQLiteDatabase sqLiteDatabase){ ContentValues contentValues= new ContentValues(); contentValues.put(UserContract.NewUserInfo.USER_NAME,new_name); contentValues.put(UserContract.NewUserInfo.USER_MOB,mobile_name); contentValues.put(UserContract.NewUserInfo.USER_EMAIL,email_name); String selection = UserContract.NewUserInfo.USER_NAME+" LIKE ?"; String[] seletion_args={old_name}; int count= sqLiteDatabase.update(UserContract.NewUserInfo.TABLE_NAME,contentValues,selection,seletion_args); return count; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }And Write the following code MainActivity. java class
package customspinner.alienslab.com.adddata; import android.app.Activity; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMessage(View view) { Intent intent = new Intent(MainActivity.this,NewContactActivity.class); startActivity(intent); } public void showData(View view) { Intent i = new Intent(MainActivity.this,DataListActivity.class); startActivity(i); } public void ViewData(View view) { Intent r = new Intent(MainActivity.this,SearchContantActivity.class); startActivity(r); } public void NextUpdate(View view) { Intent z = new Intent(MainActivity.this,UpdateContantActivity.class); startActivity(z); } @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); } }Run the Application
Image may be NSFW.
Clik here to view.
Run the application and if you click on a first button the a new activity will open. This activity consists of three Edit text and a button .When user add information in text filed and click on Save button then all the information will saved in database.And if you click on second button then a new activity will open and show all the information that are save in database.And if you click on third button a new activity will open.And user enter a particular name for search and click on button the email and phone number related to name are appears.In this activity also a delete button .And if user want to delete a information click on delete button all the information will delete from database.And if user click on last button a new activity will open in this activity you will search a particular name related to name information is appears and user want to update any of information like name phone number and email address and click on update button all the information are update in database.
Image may be NSFW.
Clik here to view.
Conclusion
In this lecture we learn about Android SqLite database how can Update row from database.. I hope you will understand this lecture.Thank you for reading this lecture Hope you got the idea.
The post Android SQLite database update partial row appeared first on The Master World.