Dear All
" Happy Holi......"
What is SQLite :
SQLite is an in-process Library used to work with Database for Mobile OS(Mostly).
@ It's Server-Less
@ Already Configured
@ Follow ACID Property
@ Structured Database
@ No Dependency
How to work with Database in Android :
Step 1 : Define Schema
" Means Define Database name , table name , column name ..........."
Step 2: Create Database
a. Create a Class by extending SQLiteOpenHelper
b. Override onCreate(SQLiteDatabase ob) method -
Like ob.execSQL(" String query")
c. Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
if any alter in Table
Step 3: Execute Query
a. Create object of SQLiteDatabase either by using getWritableDatabase() or
getReadableDatabase() with the help of Helper class object
b. Use ContentValues class object to put like key-value pair data
c. Execute query like insert() , update().... by using SQLiteDatabase object
Just See the example :
A. Output will be like
a. First Screen - Launched Activity
XML Code for this
b. Second Screen - When you click on Register
c. Third Screen - All registered User
XML Code for this layout
B. Java Code
a. MainActivity.java code is
b. Register.java code is as
c. To See all Registered User is as
d. DataBase Helper class is as
" Happy Holi......"
What is SQLite :
SQLite is an in-process Library used to work with Database for Mobile OS(Mostly).
@ It's Server-Less
@ Already Configured
@ Follow ACID Property
@ Structured Database
@ No Dependency
How to work with Database in Android :
Step 1 : Define Schema
" Means Define Database name , table name , column name ..........."
Step 2: Create Database
a. Create a Class by extending SQLiteOpenHelper
b. Override onCreate(SQLiteDatabase ob) method -
Like ob.execSQL(" String query")
c. Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
if any alter in Table
Step 3: Execute Query
a. Create object of SQLiteDatabase either by using getWritableDatabase() or
getReadableDatabase() with the help of Helper class object
b. Use ContentValues class object to put like key-value pair data
c. Execute query like insert() , update().... by using SQLiteDatabase object
Just See the example :
A. Output will be like
a. First Screen - Launched Activity
XML Code for this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ravigodara.sql2017ex1.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp"> <Button android:id="@+id/button"
android:layout_width="368dp"
android:layout_height="48dp"
android:layout_alignEnd="@+id/button2"
android:layout_alignParentTop="true"
android:layout_marginTop="62dp"
android:text="Register"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="45dp"
android:onClick="reg"/> <Button android:id="@+id/button2"
android:layout_width="368dp" android:layout_height="48dp"
android:text="Registered User"
tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="151dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="109dp"
android:onClick="showData"/></RelativeLayout>
b. Second Screen - When you click on Register
XML Code for this layout
<?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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="UserName" android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Password" android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginTop="44dp" /> <EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userN" android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/textView" /> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/userP"
android:inputType="textPassword"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/textView2" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" /></RelativeLayout>
c. Third Screen - All registered User
XML Code for this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ravigodara.sql2017ex1.ViewData"> <ListView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" /> </RelativeLayout>
Single Row Layout File
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="match_parent"android:layout_height="match_parent"android:weightSum="1"> <TextViewandroid:layout_width="wrap_content" android:layout_height="39dp"android:text="New Text" android:gravity="center"android:id="@+id/text_id" android:layout_weight="0.06" /> <TextView android:layout_width="wrap_content"android:layout_height="41dp"android:text="New Text" android:gravity="center"android:id="@+id/text_name" android:layout_weight="0.90" /><TextView android:layout_width="159dp"android:layout_height="37dp"android:text="New Text" android:gravity="center"android:id="@+id/text_pass" /> </LinearLayout>
B. Java Code
a. MainActivity.java code is
package com.example.ravigodara.sql2017ex1; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void reg(View view) { startActivity(new Intent(this, Register.class)); } public void showData(View view) { Intent iob = new Intent(this,ViewData.class); startActivity(iob); }}
b. Register.java code is as
package com.example.ravigodara.sql2017ex1; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; /** * Created by Ravi Godara on 3/10/2017. */ public class Register extends AppCompatActivity { EditText etus, etpass; String struser, strpass; Button reg; Context cob = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.regist); etus = (EditText) findViewById(R.id.userN); etpass = (EditText) findViewById(R.id.userP); reg = (Button) findViewById(R.id.button); reg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { struser = etus.getText().toString(); strpass = etpass.getText().toString(); DbHelper myob = new DbHelper(cob); myob.putInfo(myob, struser, strpass); Toast.makeText(getBaseContext(),
"Registered Successfully", Toast.LENGTH_LONG).show(); finish(); } }); }}
c. To See all Registered User is as
package com.example.ravigodara.sql2017ex1; import android.content.Context; import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class ViewData extends AppCompatActivity { Context context = this; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_data); showListView(); } private void showListView() { DbHelper myHelper = new DbHelper(context); Cursor cursorOb = myHelper.getAllRows(myHelper); String[] fromDB = new String[]{DbHelper.KEY_ID, DbHelper.USER_NAME,
DbHelper.USER_PAS}; int[] toShow = new int[]{R.id.text_id, R.id.text_name, R.id.text_pass}; SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter
(getBaseContext(), R.layout.onerow, cursorOb, fromDB, toShow, 0); ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(simpleCursorAdapter); }}
d. DataBase Helper class is as
package com.example.ravigodara.sql2017ex1; 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; public class DbHelper extends SQLiteOpenHelper { public static final String KEY_ID="_id"; public static final String USER_NAME="user_name"; public static final String USER_PAS="user_pass"; private static final String DB_NAME="mydb.db"; private static final String TAB_NAME="myinfo"; private static final int DB_VER=1; private static final String CREATE_QUERY = "create table " + TAB_NAME + " ( "
+ KEY_ID + " integer primary key autoincrement, " + USER_NAME + " text not null, " + USER_PAS + " text not null );"; public DbHelper(Context context) { super(context,DB_NAME,null,1); } @Override public void onCreate(SQLiteDatabase sdb) { sdb.execSQL(CREATE_QUERY); Log.d("rrrr", "oncreate"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{ db.execSQL("DROP TABLE IF EXISTS " + TAB_NAME); Log.d("hello", "onupgrade"); onCreate(db); } public void putInfo(DbHelper mob,String name,String pass) { SQLiteDatabase SQ= mob.getWritableDatabase(); ContentValues CV=new ContentValues(); CV.put(USER_NAME, name); CV.put(USER_PAS, pass); SQ.insert(TAB_NAME, null, CV); } public Cursor getAllRows(DbHelper mob) { SQLiteDatabase SQ = mob.getReadableDatabase(); Cursor cob = SQ.query(TAB_NAME,null,null,null,null,null,null); if(cob!=null) { cob.moveToFirst(); } return cob; }}
I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Bangalore
Digital Marketing Training in Pune
Your story is truly inspirational and I have learned a lot from your blog. Much appreciated.
ReplyDeleteAWS Training in chennai
AWS Training in bangalore
All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Thank you for an additional great post. Exactly where else could anybody get that kind of facts in this kind of a ideal way of writing? I have a presentation next week, and I’m around the appear for this kind of data.
ReplyDeletejava online training | java training in pune
java training in chennai | java training in bangalore
I am happy to find this post very useful for me, as it contains a lot of information. I always prefer to read the quality content I found in you post. Thanks for sharing
ReplyDeleteSelenium Training in Chennai
Best Selenium Training Institute in Chennai
ios developer training in chennai
Digital Marketing Training in Chennai
Best DOT NET Training institute in Chennai
asp.net training in chennai
dot net courses in chennai
Thanks a lot for sharing this page admin, I learned a lot about android.
ReplyDeleteUiPath Training in Chennai
UiPath Training
RPA Training in Chennai
Blue Prism Training in Chennai
AWS Training in Chennai
Angularjs Training in Chennai
Data Science Course in Chennai
This is my 1st visit to your web... But I'm so impressed with your content. Good Job!
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
We are sure all of you must have seen or at least heard about the biggest, academic award winner movie “Jurassic Park”. Those big gigantic dinosaurs with razor-sharp teeth’s just as big as your legs and the claws which can crush anyone in milli-seconds. Wasn’t that scary? At least we were!Do Visit jurassic-world-mod-apk
ReplyDeleteThanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.If you are looking for Mobile Application Development In Singapore, .Net Core Applications In New Zealand, iOt Applications In Singapore and .Net Applications In UAE information please visit our website Sidhyati Technology Solutions Pvt Ltd
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice Blog. this blog is easily clarify the Queries for the learners. every concept of this blog is very Unique.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
Thanks for an interesting blog. What else may I get that sort of info written in such a perfect approach? I have an undertaking that I am just now operating on, and I have been on the lookout for such info.keep update al ot.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
"It's really good. Thank you for your information. Keep Blogging! .
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
This is one amazing piece of article. Helped a lot in increasing my knowledge.
ReplyDeleteDigital Marketing Training in Chennai | Certification | SEO Training Course | Digital Marketing Training in Bangalore | Certification | SEO Training Course | Digital Marketing Training in Hyderabad | Certification | SEO Training Course | Digital Marketing Training in Coimbatore | Certification | SEO Training Course | Digital Marketing Online Training | Certification | SEO Online Training Course
Great blog. You put Good stuff. All the topics were explained briefly.so quickly understand for media am waiting for your next fantastic blog. Thanks for sharing. Any course related details learn...Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this websiteData Science Training In Chennai
ReplyDeleteData Science Online Training In Chennai
Data Science Training In Bangalore
Data Science Training In Hyderabad
Data Science Training In Coimbatore
Data Science Training
Data Science Online Training
Thank you for sharing such useful information. I really enjoyed while reading your article and it is good to know the latest updates. Do post more. And also read about Mobile App Development Services
ReplyDelete