Android SQLite example

I am new to Android and I need to create an application where I need to use a SQLite database. I am not very well equipped with SQLite.

Could you tell me what the purpose and use of the SQLiteOpenHelper class is and provide a simple example of creating and entering a database?

+47
android database sqlite sqliteopenhelper
Aug 18 2018-12-18T00:
source share
4 answers

The Sqlite helper class helps us manage database creation and versioning. SQLiteOpenHelper takes care of all database management activities. To use it,
1.Override onCreate(), onUpgrade() SQLiteOpenHelper methods. It is not necessary to override the onOpen () method.
2. Use this subclass to create either a readable or writable database and use the four API methods SQLiteDatabase insert(), execSQL(), update(), delete() to create, read, update, and delete rows in your table.

An example of creating a table MyEmployees and to select and insert records:

 public class MyDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "DBName"; private static final int DATABASE_VERSION = 2; // Database creation sql statement private static final String DATABASE_CREATE = "create table MyEmployees ( _id integer primary key,name text not null);"; public MyDatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Method is called during creation of the database @Override public void onCreate(SQLiteDatabase database) { database.execSQL(DATABASE_CREATE); } // Method is called during an upgrade of the database, @Override public void onUpgrade(SQLiteDatabase database,int oldVersion,int newVersion){ Log.w(MyDatabaseHelper.class.getName(), "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); database.execSQL("DROP TABLE IF EXISTS MyEmployees"); onCreate(database); } } 

Now you can use this class as shown below.

 public class MyDB{ private MyDatabaseHelper dbHelper; private SQLiteDatabase database; public final static String EMP_TABLE="MyEmployees"; // name of table public final static String EMP_ID="_id"; // id value for employee public final static String EMP_NAME="name"; // name of employee /** * * @param context */ public MyDB(Context context){ dbHelper = new MyDatabaseHelper(context); database = dbHelper.getWritableDatabase(); } public long createRecords(String id, String name){ ContentValues values = new ContentValues(); values.put(EMP_ID, id); values.put(EMP_NAME, name); return database.insert(EMP_TABLE, null, values); } public Cursor selectRecords() { String[] cols = new String[] {EMP_ID, EMP_NAME}; Cursor mCursor = database.query(true, EMP_TABLE,cols,null , null, null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; // iterate to get each value. } } 

Now you can use the MyDB class in your activity to have all database operations. The created records will help you insert values ​​in the same way, you can have your own functions for updating and deleting.

+87
Aug 18 2018-12-12T00:
source share

The following links will help you.

1. Android Sqlite Database

2. Study Guide 1

Database Helper Class:

Helper class for managing database creation and versioning.

You create a subclass that implements onCreate(SQLiteDatabase) , onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase) , and this class takes care of opening the database if it exists, creating it if it is not, and updating it if necessary. Transactions are used to ensure that the database is always in a reasonable state.

This class simplifies the implementation of ContentProvider to delay the opening and updating of a database until its first use, in order to avoid blocking application launches during long-term database updates.

You need to link more to this link Sqlite Helper

+11
Aug 18
source share

The DBHelper class is what handles opening and closing sqlite databases, as well as creating and updating, and a decent article on how it all works, here . When I started android, it was very useful (however, I have been objective-c recently and forgot that most of them will be used.

+1
Aug 18 2018-12-12T00:
source share

Using the Helper class, you can access the SQLite database and perform various operations on it by overriding the onCreate () and onUpgrade () methods.

+1
Aug 18 '12 at 4:16
source share



All Articles