Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mydbtest;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DatabaseHelper extends SQLiteOpenHelper {
- public static final String DATABASE_NAME = "employee_directory";
- public static final String _ID = "_id";
- public static final String MEMBER_NAME = "member_name";
- public static final String TITLE = "title";
- public static final String OFFICEPHONE = "officePhone";
- public static final String CELLPHONE = "cellPhone";
- public static final String EMAIL = "email";
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- /*
- * Create the employee table and populate it with sample data.
- * In step 6, we will move these hardcoded statements to an XML document.
- */
- String sql = "CREATE TABLE IF NOT EXISTS employee (" +
- "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
- "member_name TEXT, " +
- "title TEXT, " +
- "officephone TEXT, " +
- "cellphone TEXT, " +
- "email TEXT, " +
- "managerId INTEGER)";
- db.execSQL(sql);
- ContentValues values = new ContentValues();
- values.put(MEMBER_NAME, "John");
- values.put(TITLE, "CEO");
- values.put(OFFICEPHONE, "617-219-2001");
- values.put(CELLPHONE, "617-456-7890");
- db.insert("employee", MEMBER_NAME , values);
- values.put(MEMBER_NAME, "Robert");
- values.put(TITLE, "VP Engineering");
- values.put(OFFICEPHONE, "617-219-3333");
- values.put(CELLPHONE, "781-444-2222");
- values.put("managerId", "1");
- db.insert("employee", MEMBER_NAME, values);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS employees");
- onCreate(db);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment