Changes

An Android Studio SQLite Database Tutorial

29 bytes removed, 15:51, 18 July 2014
no edit summary
The name of the database will be productID.db which, in turn, will contain a single table named products. Each record in the database table will contain a unique product ID, a product description and the quantity of that product item currently in stock, corresponding to column names of “productid”, “productname” and “productquantity” respectively. The productid column will act as the primary key and will be automatically assigned and incremented by the database management system.
The database schema for the products table is outlined in Table 4042-1:
<table border="1">
</table>
Table 4042-1
== Creating the Data Model ==
Once completed, the application will consist of an activity and a database handler class. The database handler will be a subclass of SQLiteOpenHelper and will provide an abstract layer between the underlying SQLite database and the activity class, with the activity calling on the database handler to interact with the database (adding, removing and querying database entries). In order to implement this interaction in a structured way, a third class will need to be implemented to hold the database entry data as it is passed between the activity and the handler. This is actually a very simple class capable of holding product ID, product name and product quantity values, together with getter and setter methods for accessing these values. Instances of this class can then be created within the activity and database handler and passed back and forth as needed. Essentially, this class can be thought of as representing the database model.
Within Android Studio, navigate within the Project tool window to Database -> app -> src -> main -> java and right-click on the package name. From the popup menu, choose the New -> Java Class option and, in the Create New Class dialog, name the class Product before clicking on the OK button.
Once created the Product.java source file will automatically load into the Android Studio editor. Once loaded, modify the code to add the appropriate data members and methods:
<pre>
package com.ebookfrenzy.database.database;
public class Product {
<pre>
package com.ebookfrenzy.database.database;
import android.database.sqlite.SQLiteDatabase;
<pre>
package com.ebookfrenzy.database.database;
import android.database.sqlite.SQLiteDatabase;
<pre>
package com.ebookfrenzy.database.database;
import android.support.v7.app.ActionBarActivity;