In this application, we will learn how to use SQLite database in android to save values and retrieve back from it. SQLite database does not support complex operators or function, so it does not support join, group by, no data type integrity etc. other wise it is same as other databases. There are three data types in SQLite:
1) TEXT: to store any value as a text
2) Integer: to store integer type value
3) Real: to store floating type value
But as we said it does not support data type integrity so we can use any data type and we can store any type of value in it. So let's start our project, create new project and here we are using two buttons, first button is used to take value from edit text boxes and save them to SQLite database, The second button is used to retrieve all data from database and display them on text view which is scrollable. The code of android XML file is given below:
Now run your project and test this application. Complete project on SQLite are also available here. If you have doubts please comment. Thanks... :)
1) TEXT: to store any value as a text
2) Integer: to store integer type value
3) Real: to store floating type value
But as we said it does not support data type integrity so we can use any data type and we can store any type of value in it. So let's start our project, create new project and here we are using two buttons, first button is used to take value from edit text boxes and save them to SQLite database, The second button is used to retrieve all data from database and display them on text view which is scrollable. The code of android XML file is given below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#abc"
>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLines="1"
android:hint="Name"
android:layout_marginTop="28dp"
android:ems="10"
>
<requestFocus
/>
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:hint="Sur
Name"
android:maxLines="1"
android:ems="10"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignRight="@+id/editText2"
android:layout_below="@+id/editText2"
android:text="Insert
Values"
android:onClick="insert"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:onClick="display"
android:text="Display
all Values"
/>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now open your Java file and initialize all objects.
-> To create new database or open existed database use: openOrCreataDatabase() and pass name of the database and open it in private mode.
-> To run any query except select query use: execSQL()
-> To select values from database use: rawQuery()
The code of android Java file is given below with explanation:
Now open your Java file and initialize all objects.
-> To create new database or open existed database use: openOrCreataDatabase() and pass name of the database and open it in private mode.
-> To run any query except select query use: execSQL()
-> To select values from database use: rawQuery()
The code of android Java file is given below with explanation:
package
com.example.checkblogapp; //your
package name
import
android.os.Bundle;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
android.app.Activity;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
public
class
MainActivity extends
Activity {
SQLiteDatabase
db;
TextView
tv;
EditText
et1,et2;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize
all view objects
tv=(TextView)findViewById(R.id.textView1);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
//create
database if not already exist
db=
openOrCreateDatabase("Mydb",
MODE_PRIVATE,
null);
//create
new table if not already exist
db.execSQL("create
table if not exists mytable(name varchar, sur_name varchar)");
}
//This
method will call on when we click on insert button
public
void
insert(View v)
{
String
name=et1.getText().toString();
String
sur_name=et2.getText().toString();
et1.setText("");
et2.setText("");
//insert
data into able
db.execSQL("insert
into mytable values('"+name+"','"+sur_name+"')");
//display
Toast
Toast.makeText(this,
"values
inserted successfully.",
Toast.LENGTH_LONG).show();
}
//This
method will call when we click on display button
public
void
display(View v)
{
//use
cursor to keep all data
//cursor
can keep data of any data type
Cursor
c=db.rawQuery("select
* from mytable",
null);
tv.setText("");
//move
cursor to first position
c.moveToFirst();
//fetch
all data one by one
do
{
//we
can use c.getString(0) here
//or
we can get data using column index
String
name=c.getString(c.getColumnIndex("name"));
String
surname=c.getString(1);
//display
on text view
tv.append("Name:"+name+"
and SurName:"+surname+"\n");
//move
next position until end of the data
}while(c.moveToNext());
}
}
Now run your project and test this application. Complete project on SQLite are also available here. If you have doubts please comment. Thanks... :)
wow nice!!! thanks sir....
ReplyDeleteNice....
ReplyDeletenice
ReplyDeleteIs this code enough or I should go Mysql and create database and tables? Please help me out. I'm new to connecting Android to database.
ReplyDeleteNo need to go anywhere..just use it and finish...:)
DeleteThis is good one...Bhai I have a project in final year of B.Tech based on android.Tha neme of project is Android based traffic challaan monitoring application and in this project my first module is Log in and registration module,now i want that all the information added by a police personnel should be store in the cloud,please tell me the way and links where i can link how to store information in cloud database.Also tell who provide free cloud service.
ReplyDeleteyou can find login and registration form projects in android label. For free cloud database use Google database.
Deletesir i check your code from android label for login in which you use images as a show and hide but i face a problem when open xml file login and signin it show an error the error is "@+id/rl is not sibling in the same relative layout" plz help me
Deleteplz reply me as soon as possible
DeleteThis comment has been removed by the author.
Deleteerror remove from login.xml not from siginin.xml
DeletePlease give me resource to deal with RSS feed. I am learning android. I want to develop quiz app
ReplyDeletehello sir,
ReplyDeletecan you show more example of android project connect with php and mysql.
check all projects in Android Lablel(above) or check it: http://www.coders-hub.com/p/android.html
DeleteHow can you call buttons without intializing of them?
ReplyDeleteCheck XML file. I have declared a method in onClick tag in XML file which will call in Java.
Deleteyou should use Listener for this.
DeleteHow to create two or more than two table in android app
ReplyDeleteuse this code same given in above post:
Deletedb.execSQL("create table if not exists table_name(name varchar, sur_name varchar)");
db.execSQL("create table if not exists mytable(Latitude rel, Longitude real"
Delete+ "Locality varchar, Postcode varchar, Country varchar)");
Please I want to store and retrieve location address by following the above example, part of it works but i am getting an error when the store and retrieve button is pressed. above is the line of code i implemented.
Please help out.
Thanks
hi sir thank you , can u tell me how to get data from sqlite database and showing it in list view
ReplyDeletehave u got the soulution for that...?
DeleteExample of listview and sqlite database fetching both tutorials is are given on this site..merge both code and make easily.
DeleteCan anybody Tell where is the Code for Button OnClickListener Coding,
ReplyDeletei used onClick in XML File bro.
DeleteThis comment has been removed by the author.
ReplyDeletebro i am a android beginner.i am developing one simple app my source code has no error but it not run.it provide activity not found exception and null pointer exception in console.why was the error accure,how can solve this?
ReplyDeletecan you help on my project mobile wallet
ReplyDeletewhat project??may i know?
DeleteSir nice coding ....I have started android since last august..now i am studying database portion..and faced many problem..sir.., So please post update,and delete coding..
ReplyDeletesir how to clear up the text after clicking display names
ReplyDeletehow can i set image background color as white
ReplyDeletethank you that help me about SqlitDatabase for android please post class that extent from SQL....
ReplyDeletehow to display the details in message box? please..
ReplyDeleteafter i created my sql database and enter and save my data , how i can use it in my app. in another java activity...
ReplyDeletethis is good. but any idea how to assign auto increment ID on an existing table?
ReplyDeletePlease Share Code to Add Button '' Delete " and Clear All Data in database,,,
ReplyDeleteCheck incoming and outgoing call project in android label.
DeleteI am making a contact Application where i have tp show all saved contact onto a Listview from sqlLite..help me in that
ReplyDeletesir,i want to display the database table as such like all columns and rows,can u help me for that
ReplyDeleteHello Sir,
ReplyDeleteI'm used your app.It works properly but when i'm force to stop and clear it cache of this app and restart it again ,during that time direct press "Display all values" it unfortunately stopped.please give me suggestion on it sir.
good, and it is running thanks 4 code
ReplyDeletesir ... can u just help that how can i get the location of another person ...
ReplyDeleteSir i hve instlled unsigned apk to my smart ph by using above code snippet, after installing it showing "unfortunately, mypppication has stopped."
ReplyDeletePlz help me.. ?..
ReplyDeleteHow to pass parameter in select query, please help
ReplyDeleteex:- "select * from mytable2 where coloum1 =Parameter1 and coloum2 =Parameter2 "
Deletehi how to store list view value to sqlite data base...
ReplyDeletehi !!!
ReplyDeleteplease help for my android project, its based on keyboard using IME, i have database also and in database i have 3 tables from these 3 table i need to retrieve data for different different activity so please help me because i m new to sqlite databse
Really superb this is first time i can able to understand it clearly
ReplyDeletesir i need to show only recently added data.it show all the data..plz give me a sln..
ReplyDeleteThank you ! This's amazing ;)
ReplyDeleteIt's working only for once, when I try to run it again, the error shows "MyApp has stopped working". Please Help !!!
ReplyDeleteplease how can i update and retrive the value in table form please help me
ReplyDeleteand we can see the table in our project ??????
http://stackoverflow.com/questions/32027513/android-fetch-data-from-database-error-application-crash
ReplyDeletePlease answer anyone
Nice and quick tutorial, but how would you edit it if the database is online?
ReplyDeleteI am working with wamp server for accessing mysql and PHP for online app...i wanted get interact with db in it as server...so plz help me establish connection b/w android studio to server(mysql wit PHP)
ReplyDeletehow we can store the gridview text when user select ..into sqlitedatabase
ReplyDeleteHow about if i try for 3 edittexts ??
ReplyDeleteiam trying but its not running
plz help me out
Very good thank zou a lot.
ReplyDeleteits awesome, thanks!!!
ReplyDeleteHi, thanks a lot.
ReplyDeleteSo isn't it a good choice if I have to implement even a simple join?
If it's not, then what is your suggestion? Remote db like mysql?
my name is marita.. i am new to android development but i am sure your code is 100% working but i did not clearly understand how to use the code. i mean should i paste this code into SQLite or android studio or eclipse? i am using android studio for developing my final year project. please help me out and let me know soon how exactly to use this :( totally new to android coding.
ReplyDeleteThanks!
Hi Marita, you can use this code in android studio or eclipse.
Deletehi,nice tutorial. i want to create an android app that provides short tutorials or lessons.How do i go about? Can SQLITE contain all the data or other options.thanks
ReplyDeleteIt doesnt works app crashes!! all syntax is correct
ReplyDeleteAppreciable !!!
ReplyDeletewen i click display ,, my launcher stop working
ReplyDeletei need ur help
ReplyDeletenice sir.thank you
ReplyDeleteHello everyone,i want to create android app that provides all agricultural information and this is my final year projects please help how to get code
ReplyDeletewow! really useful, bro! thanks sooo much
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteVery simple............
ReplyDeleteEasy for new learners......
good job
ReplyDeleteCode for auto increment? let if i want to start from 2016 2017 2018 ......so on.
ReplyDeleteSame as MySQL. Read more about SQLite in my android app: https://play.google.com/store/apps/details?id=coders.hub.android.master
Deleteits directing to google play store...... i have coded like this (user_id INTEGER AUTO INCREMENT NOT NULL).
Deleteand in class :mydb.execSQL("insert into tablename values(" , )");. i kept the id place blank, but i want to assign starting no.
how to fetch all record show in new activity using sqlite database
ReplyDeleteThe is very help full to me.but i need a code to display name and surname separately in two textview.
ReplyDeleteand additional age contact number i need to display it separatly
How to create database in SQL Lite? DO I need to purchase from any company
ReplyDeletecan u plz snd me the source codes of any mini projects so that i can make any app and can impose it to my resume..my email id-atul.verma6387@gmail.com
ReplyDeletenice work bro
ReplyDeletethanks for this but i have a question. How can i show these data in another activity in a text view?
ReplyDeleteplease Bro make a tutorial on save image and text in SQLite data base and retrieve it in Gridview.....Please....
ReplyDeleteHow Can i Delete record ???
ReplyDeletehii sir in i am new for android developer please support me sir, in which platform i will run the program code please give me details about the project sir please...
ReplyDeletehi sir, i am developing a bible app where is want to read an entire chapter of the bible stored in the database on click of a button. i have created my table in the following format
ReplyDeleteCOLUMN_BOOK(string)
COLUMN_CHAPTER(integer)
COLUMN_VERSE(integer)
COLUMN_WORDINGS (string)
can you help me as to how to print/read the data from the database into a new textview onClick of a button in the previous activity.
nice.. thank you sir!!
ReplyDeleteBtw, I am new to android programming. And i'm looking for a way to insert data such as location(lat,long), fish type, remarks,date, and fish image into my fishing app, which acts as a fish diary and load it back, by type and searching the fish type.. Can you tell me how to figure this out using sqlite3 and android studio? pls help thank you..
application crash can u plz help
ReplyDeleteCan u help me I have to print data in another activity
ReplyDeletefor ex.my profile acticity
I got error like application stopped unfortunately...what I do..please help me
ReplyDelete