Android SQLite Database
Jun 23, 2015
AndroidSQLite Database
WHY DATABASES ON ANDROID?Common usage for a database
• Huge amounts of data
• Convenient query language to obtain data
• Accessed/modified by huge amount of users Applications:
• backend for multi-tier web architecture,• backend for business process architectures like SAP• areas: banks, insurance companies, ...
Android • Small scale architecture, little data, few applications ...
What is SQLite?• SQLite is Open Source Database embedded into
Mobile Devices.
• SQLite supports SQL syntax, transactions and
prepared statements.
• SQLite consumes low memory approx. 250Kbyte.
SQLite• Relational database with tables (DB schema)
• Popular embedded database, here: integrated in Android
• SQL interface + small memory footprint + decent speed
• Native API not JDBC
• Properties (from http://www.sqlite.org/different.html)• Zero configuration and serverless• Single database file & stable across platforms• Compact, public domain, readable source code• Manifest typing and variable length records
SQLite vs Content Provider• SQLite
• Persistent storage of data• Data accessible to a single application (the owner)• Often wrapped by a Content Provider
• Content Provider• Specialized type of data store to share data across apps• Exposes standardized ways to retrieve/manipulate data• Query with URI: <standard_prefix>://<authority>/<data_path>/<id>• Examples: content://browser/bookmarks, content://contacts/people
• Built in
Make a demo project
Create Database
Add that CREATE_TABLE_SQL into onCreate Method
Database Open/Close
Database Insert()
Now in Main Activity
Add Button Click
Output
Now Get Data From Database
Add a Layout to Show Data in List
Add another Layout for List Rowlistrow.xml
Add New ContactListActivity
Add Button Click On MainActivity
Add another button name Show All Contact in our activity_main.xml with id:btnAllContact
Add getContacts() in DatabaseAdapter Class
This method will return data from database in a Arraylist.
Make a Custom Adepter
As you have done before in ListView Session
Create a CustomAdapter to bind data in list adapter.
Then set the adapter in our listview.
Now back in our ContactListActivity
Output