Monday, 21 April 2014

How to Create SQLite DataBase in Android

What is SQLite?

SQLite is a relational database management system (RDBMS). What makes SQLite unique is that it is considered an embedded solution. Most database management system such as Oracle, MySQL, and SQL Server are standalone server processes that run independently.

SQLite is actually a library that is linked into applications. All database operations are handled within the application through calls and functions contained in the SQLite library. This is great news while you’re learning to use SQLite because it makes it much easier to manipulate even large databases when compared to more conventional database solutions.
In case you’re interested, SQLite is actually written in C and contained within a Java-based “wrapper” provided by the Android SDK.
SQLite does rely on Structured Query Language (SQL); the same language used by most other RDBMSs. If you’re already familiar with SQL from another database system, you have a serious head start using SQLite because you will find that most query commands are structured exactly the same way.

SQLite Java Classes

Remember that SQLite is written in C and wrapped in Java by the Android SDK. This wrapper is comprised of a set of Java classes that interact directly with the SQLite database management system.
Cursor
The Cursor class provides access to the results of a database query. For example, if you use the SQL SELECT operation, you could potentially have multiple returns from within the database. Cursor allows you to step through these results which can then be accessed from within the application code.
SQLiteDatabase
This class is the actual interface between your application code and the SQL database.
It includes functions to perform SQL-based operations such as INSERT, DELETE, QUERY, and RAWQUERY (a SQL query statement that returns results in the form of a Cursor object).
You can learn more about how to use the SQLiteDatabase class in Android App Development Fundamentals II.
SQLiteOpenHelper
This helper class is designed to make creating and updating databases easier. Please note that this class must be subclassed within the code of your application and include the onCreate() and onUpgrade() callback methods.
Although these classes may seem a little confusing, this approach is certainly easier to understand than conventional RDBMS interfaces and because SQLite is so lightweight, even if your code isn’t perfect you shouldn’t have any problems creating functional databases for your Android applications.
The fact remains that unless you are making only the most basic of Android applications, you need data management. SQLite makes it easy to incorporate powerful database features that provide your users with a much more interactive experience.

 

How to DataBase and Create table in android



This Java code 


package com.rajnish.sqlitedatabasesimple;

import com.example.sqlitedatabasesimple.R;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity 
{

SQLiteDatabase db;
String CREATEDATABASE="My.db";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt=(TextView)findViewById(R.id.textView1);
db=openOrCreateDatabase(CREATEDATABASE, MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS Login (Name VARCHAR,Address VARCHAR);");
db.execSQL("INSERT INTO Login VALUES('RAJNISH','DELHI');");
Cursor cb=db.rawQuery("SELECT *FROM Login", null);
cb.moveToNext();
String name=cb.getString(0);
String Address=cb.getString(1);
txt.setText("Name is "+name+"\n Address is "+Address);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


 Program Out Put is like this 






0 comments:

Post a Comment