* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Lab
Survey
Document related concepts
Open Database Connectivity wikipedia , lookup
Oracle Database wikipedia , lookup
Serializability wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Ingres (database) wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Functional Database Model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Concurrency control wikipedia , lookup
Relational model wikipedia , lookup
Clusterpoint wikipedia , lookup
Transcript
Lab on Databases: By the end of this lab you will be able to: create and update an SQLite database, query the data you have inserted in the database and use the results. 1. Start a new Android project with the name: ContactsDB 2. Create a Contact class: a. You should include the following fields: private int _id; private String _name; private String _phone_number; b. Create setters and getters for all the fields of your class c. Create 3 constructors: public Contact(int id, String name, String phone_number) public Contact(String name, String phone_number) public Contact() 3. Create SQLite Database Handler Class named. This class handles all database CRUD (Create, Read, Update, and Delete) operations: public class DatabaseHandler extends SQLiteOpenHelper { 4. After extending your class from SQLiteOpenHelper you need to override two methods onCreate() and onUpgrade() onCreate() – These is where we need to write create table statements. This is called when database is created. onUpgrade() – This method is called when database is upgraded like modifying the table structure, adding constraints to database etc. You can define a few static variables to handle better the database queries: private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "contactsManager"; // Contacts table name private static final String TABLE_CONTACTS = "contacts"; // Contacts Table Columns names private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_PH_NO = "phone_number"; 5. Now we need to write methods for handling all database read and write operations. Here we are implementing following methods for our contacts table: public void addContact(Contact contact) {} // Getting single contact public Contact getContact(int id) {} // Getting All Contacts public List<Contact> getAllContacts() {} // Getting contacts Count public int getContactsCount() {} // Updating single contact public int updateContact(Contact contact) {} // Deleting single contact public void deleteContact(Contact contact) {} 6. Insert a new record: The addContact() method accepts Contact object as parameter. We need to build ContentValues parameters using Contact object. Once we inserted data in database we need to close the database connection. Hints: you will need to open an SQLiteDatabase to add the contact data, you will need to use the put method of the ContentValues class. 7. Reading Rows: a. The method getContact() will read single contact row. It accepts id as parameter and will return the matched row from the database. Hints: you will need to open an SQLiteDatabase to add the contact data, you will need to create a cursor, you will need to create a new contact with the data returned from the cursor b. getAllContacts() will return all contacts from database in array list format of Contact class type. You need to write a for loop to go through each contact. Hints: you will need to create a list of contacts, a cursor that returns all data from cotacts table 8. Updating a record: updateContact() will update single contact in database. This method accepts Contact class object as parameter. 9. Deleting a record: deleteContact() will delete single contact from database. 10. ContactsDBActivity: This is the main app activity. You will need to: a. Create a DatabaseHandler object named db b. Add 5 records in your db c. Print these records in the main View (you may need to add components in the main view)