Android - SQLite Database
Android - SQLite Database
SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c
Database - Package
The main package is android.database.sqlite that contains the classes to manage your own databases
Database - Creation
In order to create a database you just need to call this method openOrCreateDatabase with your database name and mode as a parameter. It returns an instance of SQLite database which you have to receive in your own object.Its syntax is given belowSQLiteDatabase mydatabase = openOrCreateDatabase("your database name",MODE_PRIVATE,null);
Apart from this , there are other functions available in the database package , that does this job. They are listed below
Sr.NoMethod & Description
1
openDatabase[Math Processing Error]Stringpath,SQLiteDatabase.CursorFactoryfactory,intflags,DatabaseErrorHandlererrorHandler
This method only opens the existing database with the appropriate flag mode. The common flags mode could be OPEN_READWRITE OPEN_READONLY
2
openDatabase[Math Processing Error]Stringpath,SQLiteDatabase.CursorFactoryfactory,intflags
It is similar to the above method as it also opens the existing database but it does not define any handler to handle the errors of databases
3
openOrCreateDatabase[Math Processing Error]Stringpath,SQLiteDatabase.CursorFactoryfactory
It not only opens but create the database if it not exists. This method is equivalent to openDatabase method.
4
openOrCreateDatabase[Math Processing Error]Filefile,SQLiteDatabase.CursorFactoryfactory
This method is similar to above method but it takes the File object as a path rather then a string. It is equivalent to file.getPath[Math Processing Error]
Database - Insertion
we can create table or insert data into table using execSQL method defined in SQLiteDatabase class. Its syntax is given belowmydatabase.execSQL("CREATE TABLE IF NOT EXISTS TutorialsPoint(Username VARCHAR,Password VARCHAR);"); mydatabase.execSQL("INSERT INTO TutorialsPoint VALUES('admin','admin');");
This will insert some values into our table in our database. Another method that also does the same job but take some additional parameter is given below
Sr.NoMethod & Description
1
execSQL[Math Processing Error]Stringsql,Object[]bindArgs
This method not only insert data , but also used to update or modify already existing data in database using bind arguments
Database - Fetching
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data.Cursor resultSet = mydatbase.rawQuery("Select * from TutorialsPoint",null); resultSet.moveToFirst(); String username = resultSet.getString(0); String password = resultSet.getString(1);
There are other functions available in the Cursor class that allows us to effectively retrieve the data. That includes
Sr.NoMethod & Description
1
getColumnCount[Math Processing Error]
This method return the total number of columns of the table.
2
getColumnIndex[Math Processing Error]StringcolumnName
This method returns the index number of a column by specifying the name of the column
3
getColumnName[Math Processing Error]intcolumnIndex
This method returns the name of the column by specifying the index of the column
4
getColumnNames[Math Processing Error]
This method returns the array of all the column names of the table.
5
getCount[Math Processing Error]
This method returns the total number of rows in the cursor
6
getPosition[Math Processing Error]
This method returns the current position of the cursor in the table
7
isClosed[Math Processing Error]
This method returns true if the cursor is closed and return false otherwise
Database - Helper class
For managing all the operations related to the database , an helper class has been given and is called SQLiteOpenHelper. It automatically manages the creation and update of the database. Its syntax is given belowpublic class DBHelper extends SQLiteOpenHelper { public DBHelper(){ super(context,DATABASE_NAME,null,1); } public void onCreate(SQLiteDatabase db) {} public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {} }
I Am Not The Owner Of These Code .I Merely Have Copied Them From Various Sources. The Only Thing I Did Is That I Am Going To Present Them In More Easy Way To Understand THIS POST IS FROM TUTORIALS POINY... .
Comments
Post a Comment