SQLite provides a structured data storage and it is lightweight and run-in process, which means the library runs as part of the application. There is no need for server installation and configuration. SQLite has 5 datatypes:
You can read more here.
SQLiteOpenHelper class provides methods to access, manage the creation and version management of the database. To create an SQLiteOpenHelper class, create a new Java class file. The class should extend the SQLiteOpenHelper class.
Press Alt + Enter and select Implement Methods. Click OK
- onCreate – is called only if the database does not exist. Here you can create the tables and add any initial data
- onUpgrade – is called when a new version of the Database is expected. Upgrade the tables if necessary.
Create public fields one for the database filename and one for the version. Now create a constructor with one parameter, Context.
I have created the SQL creation string in the contract class. Read this post. Now implement the onCreate method and create the tables using the execsql method.
- Create a GameDBhelper field
private GameDBHelper dbHelper;
- it is common to instantiate the implemented SQLiteOpenHelper class inside one Activity’s onCreate hook method
dbHelper = new GameDBHelper(this);
- remember to close the database inside one Activity’s onDestroy hook method.
- The database is not yet created until you try to access it, for example with getReadableDatabase or getWritableDatabase methods.
SQLDatabase db = dbHelper.getReadableDatabase();
- Android – Notifications
- Content Provider – Part 3 – insert, delete, update, getType
- Content Provider – Part 2 – create and query