Tutorial by Examples

public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "Example.db"; private static final int DATABASE_VERSION = 3; // For all Primary Keys _id should be used as column name public static final String COLUMN_ID = "_id&q...
// You need a writable database to insert data final SQLiteDatabase database = openHelper.getWritableDatabase(); // Create a ContentValues instance which contains the data for each column // You do not need to specify a value for the PRIMARY KEY column. // Unique values for these are automatic...
SQLiteOpenHelper is a helper class to manage database creation and version management. In this class, the onUpgrade() method is responsible for upgrading the database when you make changes to the schema. It is called when the database file already exists, but its version is lower than the one spec...
Here is an example of a method that would live inside a SQLiteOpenHelper subclass. It uses the searchTerm String to filter the results, iterates through the Cursor's contents, and returns those contents in a List of Product Objects. First, define the Product POJO class that will be the container f...
DBContract.java //Define the tables and columns of your local database public final class DBContract { /*Content Authority its a name for the content provider, is convenient to use the package app name to be unique on the device */ public static final String CONTENT_AUTHORITY =...
// You need a writable database to update a row final SQLiteDatabase database = openHelper.getWritableDatabase(); // Create a ContentValues instance which contains the up to date data for each column // Unlike when inserting data you need to specify the value for the PRIMARY KEY column as well ...
Transactions can be used to make multiple changes to the database atomically. Any normal transaction follows this pattern: // You need a writable database to perform transactions final SQLiteDatabase database = openHelper.getWritableDatabase(); // This call starts a transaction database.beginT...
To delete all rows from the table //get writable database SQLiteDatabase db = openHelper.getWritableDatabase(); db.delete(TABLE_NAME, null, null); db.close(); To delete all rows from the table and get the count of the deleted row in return value //get writable database SQLiteDatabase db =...
Setting Up the database public class DatabaseHelper extends SQLiteOpenHelper { // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "database_name"; // Table Names private static...
Put your dbname.sqlite or dbname.db file in assets folder of your project. public class Databasehelper extends SQLiteOpenHelper { public static final String TAG = Databasehelper.class.getSimpleName(); public static int flag; // Exact Name of you db file that you put in as...
You might want to import and export your database for bacukups for example. Dont forget about the permissions. public void exportDatabase(){ try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); String currentD...
Here is an example of inserting large chunks of data at once. All the data you want to insert is gathered inside of a ContentValues array. @Override public int bulkInsert(Uri uri, ContentValues[] values) { int count = 0; String table = null; int uriType = IChatContract.MessageColu...

Page 1 of 1