C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android SQLite TutorialSQLite is an open-source relational database i.e. used to perform database operations on android devices such as storing, manipulating or retrieving persistent data from the database. It is embedded in android bydefault. So, there is no need to perform any database setup or administration task. Here, we are going to see the example of sqlite to store and fetch the data. Data is displayed in the logcat. For displaying data on the spinner or listview, move to the next page. SQLiteOpenHelper class provides the functionality to use the SQLite database. SQLiteOpenHelper classThe android.database.sqlite.SQLiteOpenHelper class is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class. Constructors of SQLiteOpenHelper classThere are two constructors of SQLiteOpenHelper class.
Methods of SQLiteOpenHelper classThere are many methods in SQLiteOpenHelper class. Some of them are as follows:
SQLiteDatabase classIt contains methods to be performed on sqlite database such as create, update, delete, select etc. Methods of SQLiteDatabase classThere are many methods in SQLiteDatabase class. Some of them are as follows:
Example of android SQLite databaseLet's see the simple example of android sqlite database. File: Contact.java
package example.TheDeveloperBlog.com.sqlitetutorial; public class Contact { int _id; String _name; String _phone_number; public Contact(){ } public Contact(int id, String name, String _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } public Contact(String name, String _phone_number){ this._name = name; this._phone_number = _phone_number; } public int getID(){ return this._id; } public void setID(int id){ this._id = id; } public String getName(){ return this._name; } public void setName(String name){ this._name = name; } public String getPhoneNumber(){ return this._phone_number; } public void setPhoneNumber(String phone_number){ this._phone_number = phone_number; } } File: DatabaseHandler.java
Now, let's create the database handler class that extends SQLiteOpenHelper class and provides the implementation of its methods. package example.TheDeveloperBlog.com.sqlitetutorial; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; import java.util.List; public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "contactsManager"; private static final String TABLE_CONTACTS = "contacts"; private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_PH_NO = "phone_number"; public DatabaseHandler(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); //3rd argument to be passed is CursorFactory instance } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")"; db.execSQL(CREATE_CONTACTS_TABLE); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); // Create tables again onCreate(db); } // code to add the new contact void addContact(Contact contact) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, contact.getName()); // Contact Name values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone // Inserting Row db.insert(TABLE_CONTACTS, null, values); //2nd argument is String containing nullColumnHack db.close(); // Closing database connection } // code to get the single contact Contact getContact(int id) { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_NAME, KEY_PH_NO }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null); if (cursor != null) cursor.moveToFirst(); Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)); // return contact return contact; } // code to get all contacts in a list view public List File: MainActivity.java
package example.TheDeveloperBlog.com.sqlitetutorial; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DatabaseHandler db = new DatabaseHandler(this); // Inserting Contacts Log.d("Insert: ", "Inserting .."); db.addContact(new Contact("Ravi", "9100000000")); db.addContact(new Contact("Srinivas", "9199999999")); db.addContact(new Contact("Tommy", "9522222222")); db.addContact(new Contact("Karthik", "9533333333")); // Reading all contacts Log.d("Reading: ", "Reading all contacts.."); List Output:How to view the data stored in sqlite in android studio?Follow the following steps to view the database and its data stored in android sqlite:
Next TopicAndroid Sqlite Example With Spinner
|