
SqlLite database is an integral part of an application that is used for local storage. The advantage SqlLite database offers are:
In android, SqlLite is most commonly used database. Here we are going to create a sqllite database and table.
Account database:
Let’s think about an account database. Generally a database contain several tables to fullfil its purpose. Here we will create a database and make one table.
Let, our table will store account no and account name of the account holder. Account no is primary key. Look at the structure of the table given below:
Database name: accountDB
Table name: accountInfo
Table structure:
Create new application:
We are going to create a new application using eclipse editor. Go to New->project->android application project. Name the application and complete other configurations. Set launcher activity name to MainActivity.
Look at our MainActivity class.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDBHandler db = new MyDBHandler(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Consider this line:
MyDBHandler db = new MyDBHandler(this);
It is used to make a database handler for our program. In the following section, we are going to discuss about the database handler class.
Create a database handler class named MyDBHandler. All the information about database, table, and attributes of the table will be specified here. In onCreate function, table accountInfo will be created. In onUpgrade function, database will be upgraded to new version.
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "accountDB";
// Contacts table name
private static final String TABLE_ACCOUNT = "accountInfo";
// Contacts Table Columns names
private static final String KEY_ACNO = "accountNo";
private static final String KEY_NAME = "accountName";
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_ACCOUNT_TABLE = "CREATE TABLE " + TABLE_ACCOUNT + "("
+ KEY_ACNO + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" + ")";
db.execSQL(CREATE_ACCOUNT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNT);
// Create tables again
onCreate(db);
}
}
Create a class named Account. It will be used to set account information and retrieve the information. This class is used to perform CRUD (Create, Read, Update and Delete) operations in database. For now, this operation is out of scope for us in this article. We are just going to look at the class.
public class Account {
int accountNo;
String accountName;
// empty constructor
public Account(){
}
// constructor
public Account(int accountNo, String accountName){
this.accountNo = accountNo;
this.accountName = accountName;
}
// get account no
public int getAccNo(){
return this.accountNo;
}
// set account no
public void setAccNo(int accNo){
this.accountNo = accNo;
}
// get account name
public String getAccName(){
return this.accountName;
}
// set account name
public void setAccName(String accName){
this.accountName = accName;
}
}
Here is a brief about the stated function.
Account(): Constructor, set account no and name.
setAccNo(): Set the account no
getAccNo(): get the account no
setAccName(): Set the account name
getAccName(): Get the account name.
From this article, we have learnt how to create SqlLite database and table. But if we want to use the database, we would have to learn how to perform CRUD(create, read, update, delete) operations.
Also know android project structure to start developing Apps and game in android and Working with bitmap image to see how Guess Who? image apps are created in Android.
- Simple and easy to administer
- Small and reliable.
- Can be embedded into a large program
In android, SqlLite is most commonly used database. Here we are going to create a sqllite database and table.
Account database:
Let’s think about an account database. Generally a database contain several tables to fullfil its purpose. Here we will create a database and make one table.
Let, our table will store account no and account name of the account holder. Account no is primary key. Look at the structure of the table given below:
Database name: accountDB
Table name: accountInfo
Table structure:
Create new application:
We are going to create a new application using eclipse editor. Go to New->project->android application project. Name the application and complete other configurations. Set launcher activity name to MainActivity.
Look at our MainActivity class.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDBHandler db = new MyDBHandler(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Consider this line:
MyDBHandler db = new MyDBHandler(this);
It is used to make a database handler for our program. In the following section, we are going to discuss about the database handler class.
Create a database handler class named MyDBHandler. All the information about database, table, and attributes of the table will be specified here. In onCreate function, table accountInfo will be created. In onUpgrade function, database will be upgraded to new version.
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "accountDB";
// Contacts table name
private static final String TABLE_ACCOUNT = "accountInfo";
// Contacts Table Columns names
private static final String KEY_ACNO = "accountNo";
private static final String KEY_NAME = "accountName";
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_ACCOUNT_TABLE = "CREATE TABLE " + TABLE_ACCOUNT + "("
+ KEY_ACNO + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" + ")";
db.execSQL(CREATE_ACCOUNT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNT);
// Create tables again
onCreate(db);
}
}
Create a class named Account. It will be used to set account information and retrieve the information. This class is used to perform CRUD (Create, Read, Update and Delete) operations in database. For now, this operation is out of scope for us in this article. We are just going to look at the class.
public class Account {
int accountNo;
String accountName;
// empty constructor
public Account(){
}
// constructor
public Account(int accountNo, String accountName){
this.accountNo = accountNo;
this.accountName = accountName;
}
// get account no
public int getAccNo(){
return this.accountNo;
}
// set account no
public void setAccNo(int accNo){
this.accountNo = accNo;
}
// get account name
public String getAccName(){
return this.accountName;
}
// set account name
public void setAccName(String accName){
this.accountName = accName;
}
}
Here is a brief about the stated function.
Account(): Constructor, set account no and name.
setAccNo(): Set the account no
getAccNo(): get the account no
setAccName(): Set the account name
getAccName(): Get the account name.
From this article, we have learnt how to create SqlLite database and table. But if we want to use the database, we would have to learn how to perform CRUD(create, read, update, delete) operations.
Also know android project structure to start developing Apps and game in android and Working with bitmap image to see how Guess Who? image apps are created in Android.