Advertisement
Guest User

database

a guest
Oct 12th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package com.example.channel;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.SQLException;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. public class Database {
  10.  
  11.     public static final String KEY_ROWID = "_id";
  12.     public static final String KEY_NAME = "p_name";
  13.     public static final String KEY_RLT = "p_rlt";
  14.  
  15.     private static final String DB_NAME = "People";
  16.     private static final String DB_TABLE = "PT";
  17.     private static final int DB_VERSION = 1;
  18.  
  19.     private static DbHelper ourHelper;
  20.     private SQLiteDatabase ourDatabase;
  21.     private static Context ourContext;
  22.  
  23.     public Database(Context c) {
  24.         ourContext = c;
  25.     }
  26.  
  27.     private static class DbHelper extends SQLiteOpenHelper {
  28.  
  29.         public DbHelper(Context context) {
  30.             super(context, DB_NAME, null, DB_VERSION);
  31.             // TODO Auto-generated constructor stub
  32.         }
  33.  
  34.         @Override
  35.         public void onCreate(SQLiteDatabase db) {
  36.             // TODO Auto-generated method stub
  37.             db.execSQL("CREATE TABLE " + DB_TABLE + " (" + KEY_ROWID
  38.                     + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
  39.                     + " TEXT NOT NULL, " + KEY_RLT + " TEXT NOT NULL);");
  40.  
  41.         }
  42.  
  43.         @Override
  44.         public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
  45.             // TODO Auto-generated method stub
  46.             db.execSQL("DROP TABLE IF EXIST " + DB_TABLE);
  47.             onCreate(db);
  48.         }      
  49.     }
  50.    
  51.     public Database open() throws SQLException
  52.     {
  53.         ourHelper = new DbHelper(ourContext);
  54.         ourHelper.getWritableDatabase();
  55.         return this;
  56.     }
  57.    
  58.     public Database close()
  59.     {
  60.         ourHelper.close();
  61.         return this;
  62.     }
  63.  
  64.     public void addThat(String data) {
  65.         // TODO Auto-generated method stub
  66.         ContentValues cv = new ContentValues();
  67.         cv.put(KEY_NAME, data);
  68.         cv.put(KEY_RLT, "brother");
  69.         ourDatabase.insert(DB_TABLE, null, cv);
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement