SQL
SQL
This website uses cookies to ensure you get the best experience on our website
Android provides many ways to store data, SQLite Database is one of them that is
already include in android OS. We have to just simply use it according to our need.
Here, we will take a simple example to store shops and their addresses. Here I�m
taking only one simple table �Shops� with following columns id (INT), name (TEXT),
shop_address(TEXT).
Full Name
Email Address
Unlock Content
Table Structure:
Table Structure
Table Structure
Now, first, create a new Android project. And create a class �Shop�, to refer a
shop as an object in our application which just has the same fields as defined in
Shops table.
Table Structure
Table Structure
Here is Shop code with getter and setter
package com.mobilesiri.sqliteexample;
public class Shop {
private int id;
private String name;
private String address;
public Shop()
{
}
public Shop(int id,String name,String address)
{
this.id=id;
this.name=name;
this.address=address;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
Read More: Develop your first android app using Android Studio � Tutorial
Read More: Android Recycler View and Card View Tutorial
package com.mobilesiri.sqliteexample;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = �shopsInfo�;
// Contacts table name
private static final String TABLE_SHOPS = �shops�;
// Shops Table Columns names
private static final String KEY_ID = �id�;
private static final String KEY_NAME = �name�;
private static final String KEY_SH_ADDR = �shop_address�;
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = �CREATE TABLE � + TABLE_SHOPS + �(�
+ KEY_ID + � INTEGER PRIMARY KEY,� + KEY_NAME + � TEXT,�
+ KEY_SH_ADDR + � TEXT� + �)�;
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL(�DROP TABLE IF EXISTS � + TABLE_SHOPS);
// Creating tables again
onCreate(db);
}
}
Insert Record:
First, start with insert, we add a method addShop() which take Shop as a parameter
and map our shop values with table�s column using ContentValues object.
getWritableDatabase is used for creating and/or opening database. So, after
inserting data into the database table, we need to close the database connection.
Read Record(s):
We write a method that will read only a recode (Shop) and take primary key (shop
id) as parameter
To get all record from the table we write a method getAllShops() that return list
of all shops.
Updating Record(s):
Write a updateShop() to update a shop/record . It requires updated shop object as a
parameter.
// Updating a shop
public int updateShop(Shop shop) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, shop.getName());
values.put(KEY_SH_ADDR, shop.getAddress());
// updating row
return db.update(TABLE_SHOPS, values, KEY_ID + " = ?",
new String[]{String.valueOf(shop.getId())});
}
Delete Record:
Write method deleteShop to delete single record/Shop from the table. It requires
shop I.d to be deleted.
// Deleting a shop
public void deleteShop(Shop shop) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SHOPS, KEY_ID + " = ?",
new String[] { String.valueOf(shop.getId()) });
db.close();
}
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
// Inserting Row
db.insert(TABLE_SHOPS, null, values);
db.close(); // Closing database connection
}
// Getting one shop
public Shop getShop(int id) {
SQLiteDatabase db = this.getReadableDatabase();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// return count
return cursor.getCount();
}
// Updating a shop
public int updateShop(Shop shop) {
SQLiteDatabase db = this.getWritableDatabase();
// updating row
return db.update(TABLE_SHOPS, values, KEY_ID + � = ?�,
new String[]{String.valueOf(shop.getId())});
}
// Deleting a shop
public void deleteShop(Shop shop) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_SHOPS, KEY_ID + � = ?�,
new String[] { String.valueOf(shop.getId()) });
db.close();
}
}
Using DBHandler
As we have written our DBHandler class completely let use it in our application.
package com.mobilesiri.sqliteexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Inserting Shop/Rows
Log.d(�Insert: �, �Inserting ..�);
db.addShop(new Shop(�Dockers�, � 475 Brannan St #330, San Francisco, CA 94107,
United States�));
db.addShop(new Shop(�Dunkin Donuts�, �White Plains, NY 10601�));
db.addShop(new Shop(�Pizza Porlar�, �North West Avenue, Boston , USA�));
db.addShop(new Shop(�Town Bakers�, �Beverly Hills, CA 90210, USA�));
Output:
sqldb-mobilesiri4