How to store the values in database in Android using SQLite from the Best IT Training Institutes in Coimbatore

How to store the values in database in Android using SQLite from the Best IT Training Institutes in Coimbatore

Create an Android Application name as DbDemo.

Open activity_main.xml insert the two TextView and EditText wise Name, Last Name and One Button as Submit.

After that come into MainActivity.java page. Create a global object for name and lastname edittetxt, submit button.

onCreate method connect with the widget function to the java page like follow

name = (EditText) findViewById(R.id.edt_name);

 

lastname = (EditText) findViewById(R.id.edt_lastname);

 

submit = (Button) findViewById(R.id.btn_submit);

then set the onClickListener for the submit button for when user click the submit button do some action behind the code on this event.

Next get the values in the edittext to store the values.

String s_name = name.getText().toString();

 

String s_lastname = lastname.getText().toString();

The values are get and stored in s_name and s_lastname String object.

Now create the DataBaseHelper Class for storing the values in SQLite DB.

Open the DataBaseHelper.java page extends SQLiteOpenHelper and create a constructor for DataBaseHelper class with Context. And add unimplemented methods like onCreate( ) and Onupgrade( ) methods. Then create a local fields for dbname(DataBase Name), version (version code).

In MainActivity.java create SQLiteDatabase object assign DataBaseHelper object with getWritableDatabase( ).

DataBaseHelper dbh = new DataBaseHelper(MainActivity.this);

 

SQLiteDatabase db = dbh.getWritableDatabase();

Create object for ContentValues.

ContentValues cv = new ContentValues();

Insert the value using name value pair so value based on key.

cv.put(DataBaseHelper.name, s_name);

enter the required files to put and use Database object for insert the corresponding values.

db.insert(DataBaseHelper.registertable, null, cv);

Display the Toast message to successfully insert values in database.

IN DataBaseHelper.java page create a fields name and table name. And write query for create table in both onCreate and onUpgrade mehods.

String register = “create table ” + registertable + “(” + id + ” integer primary key,” + name + ” text,” + lname + ” text)”;

db.execSQL(register);

After completing above procedures run the Android Application.

The Emulator shows the activity screen.

Enter the name and last name to the edit text fields.

 

 

 

When you click the submit button both values are stored in SQLite Database.

Hope this blog inspires you to embrace change and grow. Whether you are an XPLORER or not, feel free to glance through our blogs and help us guide and support you in your voyage to success. Happy Learning!

 

0 responses on "How to store the values in database in Android using SQLite from the Best IT Training Institutes in Coimbatore"

Leave a Message

Your email address will not be published. Required fields are marked *

© Xplore IT Corp 2017. All rights reserved. Designed by ProPlus Logics
Xplore WhatsApp Chat
Send via WhatsApp
X