Guest User

DatabaseHelper.java

a guest
May 9th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package com.example.mydbtest;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7.  
  8.  
  9. public class DatabaseHelper extends SQLiteOpenHelper {
  10.  
  11.  
  12.     public static final String DATABASE_NAME = "employee_directory";
  13.     public static final String _ID = "_id";
  14.     public static final String MEMBER_NAME = "member_name";
  15.     public static final String TITLE = "title";
  16.     public static final String OFFICEPHONE = "officePhone";
  17.     public static final String CELLPHONE = "cellPhone";
  18.     public static final String EMAIL = "email";
  19.  
  20.  
  21.  
  22.     public DatabaseHelper(Context context) {
  23.         super(context, DATABASE_NAME, null, 1);
  24.     }
  25.  
  26.  
  27.     @Override
  28.     public void onCreate(SQLiteDatabase db) {
  29.         /*
  30.          * Create the employee table and populate it with sample data.
  31.          * In step 6, we will move these hardcoded statements to an XML document.
  32.          */
  33.         String sql = "CREATE TABLE IF NOT EXISTS employee (" +
  34.                 "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
  35.                 "member_name TEXT, " +
  36.                 "title TEXT, " +
  37.                 "officephone TEXT, " +
  38.                 "cellphone TEXT, " +
  39.                 "email TEXT, " +
  40.                 "managerId INTEGER)";
  41.         db.execSQL(sql);
  42.  
  43.         ContentValues values = new ContentValues();
  44.  
  45.  
  46.         values.put(MEMBER_NAME, "John");
  47.         values.put(TITLE, "CEO");
  48.         values.put(OFFICEPHONE, "617-219-2001");
  49.         values.put(CELLPHONE, "617-456-7890");
  50.         values.put(EMAIL, "[email protected]");
  51.         db.insert("employee", MEMBER_NAME , values);
  52.  
  53.  
  54.         values.put(MEMBER_NAME, "Robert");
  55.         values.put(TITLE, "VP Engineering");
  56.         values.put(OFFICEPHONE, "617-219-3333");
  57.         values.put(CELLPHONE, "781-444-2222");
  58.         values.put(EMAIL, "[email protected]");
  59.         values.put("managerId", "1");
  60.         db.insert("employee", MEMBER_NAME, values);
  61.  
  62.  
  63.  
  64.  
  65.     }
  66.  
  67.  
  68.     @Override
  69.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  70.         db.execSQL("DROP TABLE IF EXISTS employees");
  71.         onCreate(db);
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment