Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. package com.v3.database;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10.  
  11. public class DBAdapter {
  12.     // Initial Configuration
  13.     public static final String DB_NAME = "message";
  14.     private static final int DATABASE_VER = 1;
  15.     private static final String TAG = "DBAdapter";
  16.  
  17.     // Set the Tables Key Words
  18.     public static final String TABLE_USERS = "users";
  19.     public static final String TABLE_MAILS = "mails";
  20.     public static final String TABLE_DRAFTS = "drafts";
  21.     public static final String TABLE_SENT = "sent";
  22.  
  23.     private final Context context;
  24.     private DatabaseHelper DBHelper;
  25.     private SQLiteDatabase db;
  26.  
  27.     private static final String CREATE_USERS = "CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, username TEXT, password TEXT);";
  28.     private static final String CREATE_MAILS = "CREATE TABLE mails (id INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER DEFAULT '0', from_id INTEGER, to_id INTEGER, subject TEXT, body TEXT, datetime TEXT, read INTEGER);";
  29.     private static final String CREATE_DRAFTS = "CREATE TABLE drafts (id INTEGER PRIMARY KEY AUTOINCREMENT, from_id INTEGER, to_id INTEGER, subject TEXT, body TEXT, datetime TEXT);";
  30.     private static final String CREATE_SENT = "CREATE TABLE sent (id INTEGER PRIMARY KEY AUTOINCREMENT, from_id INTEGER, to_id INTEGER, subject TEXT, body TEXT, datetime TEXT);";
  31.  
  32.     // Keys
  33.     public static final String ID = "id";
  34.     public static final String KEY_NAME = "name";
  35.     public static final String KEY_USERNAME = "username";
  36.     public static final String KEY_PASSWORD = "password";
  37.     public static final String PID = "pid";
  38.     public static final String KEY_FROM = "from_id";
  39.     public static final String KEY_TO = "to_id";
  40.     public static final String KEY_SUB = "subject";
  41.     public static final String KEY_BODY = "body";
  42.     public static final String KEY_READ = "read";
  43.     public static final String KEY_DATETIME = "datetime";
  44.  
  45.     public DBAdapter(Context ctx) {
  46.         this.context = ctx;
  47.         DBHelper = new DatabaseHelper(context);
  48.     }
  49.  
  50.     private static class DatabaseHelper extends SQLiteOpenHelper {
  51.  
  52.         public DatabaseHelper(Context context) {
  53.             super(context, DB_NAME, null, DATABASE_VER);
  54.         }
  55.  
  56.         @Override
  57.         public void onCreate(SQLiteDatabase db) {
  58.             db.execSQL(CREATE_USERS);
  59.             db.execSQL(CREATE_MAILS);
  60.             db.execSQL(CREATE_DRAFTS);
  61.             db.execSQL(CREATE_SENT);
  62.  
  63.         }
  64.  
  65.         @Override
  66.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  67.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  68.                     + newVersion + ", which will destroy all old data");
  69.             db.execSQL("DROP TABLE IF EXISTS titles");
  70.             onCreate(db);
  71.         }
  72.  
  73.     }
  74.  
  75.     public DBAdapter open() throws SQLException {
  76.         db = DBHelper.getWritableDatabase();
  77.         return this;
  78.     }
  79.  
  80.     public void close() {
  81.         DBHelper.close();
  82.     }
  83.  
  84.     // Insert Users
  85.     public long insertUser(String name, String username, String password) {
  86.         ContentValues initialValues = new ContentValues();
  87.         initialValues.put(KEY_NAME, name);
  88.         initialValues.put(KEY_USERNAME, username);
  89.         initialValues.put(KEY_PASSWORD, password);
  90.         return db.insert(TABLE_USERS, null, initialValues);
  91.     }
  92.  
  93.     // Insert Mails
  94.     public long insertMails(String from, String to, String subject,
  95.             String body, String datetime, String read) {
  96.         ContentValues initialValues = new ContentValues();
  97.         initialValues.put(KEY_FROM, from);
  98.         initialValues.put(KEY_TO, to);
  99.         initialValues.put(KEY_SUB, subject);
  100.         initialValues.put(KEY_BODY, body);
  101.         initialValues.put(KEY_DATETIME, datetime);
  102.         initialValues.put(KEY_READ, read);
  103.         return db.insert(TABLE_MAILS, null, initialValues);
  104.     }
  105.  
  106.     // Insert Drafts
  107.     public long insertDrafts(String from, String to, String subject,
  108.             String body, String datetime) {
  109.         ContentValues initialValues = new ContentValues();
  110.         initialValues.put(KEY_FROM, from);
  111.         initialValues.put(KEY_TO, to);
  112.         initialValues.put(KEY_SUB, subject);
  113.         initialValues.put(KEY_BODY, body);
  114.         initialValues.put(KEY_DATETIME, datetime);
  115.         return db.insert(TABLE_DRAFTS, null, initialValues);
  116.     }
  117.  
  118.     // Insert Mails
  119.     public long insertSent(String from, String to, String subject, String body,
  120.             String datetime) {
  121.         ContentValues initialValues = new ContentValues();
  122.         initialValues.put(KEY_FROM, from);
  123.         initialValues.put(KEY_TO, to);
  124.         initialValues.put(KEY_SUB, subject);
  125.         initialValues.put(KEY_BODY, body);
  126.         initialValues.put(KEY_DATETIME, datetime);
  127.         return db.insert(TABLE_SENT, null, initialValues);
  128.     }
  129.  
  130.     // Delete Section
  131.     public boolean deleteUser(long Id) {
  132.         return db.delete(TABLE_USERS, ID + "=" + Id, null) > 0;
  133.     }
  134.  
  135.     public boolean deleteMails(long Id) {
  136.         return db.delete(TABLE_MAILS, ID + "=" + Id, null) > 0;
  137.     }
  138.  
  139.     public boolean deleteDrafts(long Id) {
  140.         return db.delete(TABLE_DRAFTS, ID + "=" + Id, null) > 0;
  141.     }
  142.  
  143.     public boolean deleteSent(long Id) {
  144.         return db.delete(TABLE_SENT, ID + "=" + Id, null) > 0;
  145.     }
  146.  
  147.     // GetALL Section
  148.     public Cursor getInbox(long toId) throws SQLException {
  149.         Cursor mCursor = db.query(true, TABLE_MAILS, new String[] { ID,
  150.                 KEY_FROM, KEY_TO, KEY_SUB, KEY_BODY, KEY_DATETIME, KEY_READ },
  151.                 KEY_TO + "=" + toId, null, null, null, null, null);
  152.         if (mCursor != null) {
  153.             mCursor.moveToFirst();
  154.         }
  155.         return mCursor;
  156.     }
  157.    
  158.     public Cursor getAllDrafts(long fromId) throws SQLException {
  159.         Cursor mCursor = db.query(true, TABLE_DRAFTS, new String[] { ID,
  160.                 KEY_FROM, KEY_TO, KEY_SUB, KEY_BODY, KEY_DATETIME, KEY_READ },
  161.                 KEY_FROM + "=" + fromId, null, null, null, null, null);
  162.         if (mCursor != null) {
  163.             mCursor.moveToFirst();
  164.         }
  165.         return mCursor;
  166.     }
  167.    
  168.     public Cursor getAllSent(long fromId) throws SQLException {
  169.         Cursor mCursor = db.query(true, TABLE_DRAFTS, new String[] { ID,
  170.                 KEY_FROM, KEY_TO, KEY_SUB, KEY_BODY, KEY_DATETIME, KEY_READ },
  171.                 KEY_FROM + "=" + fromId, null, null, null, null, null);
  172.         if (mCursor != null) {
  173.             mCursor.moveToFirst();
  174.         }
  175.         return mCursor;
  176.     }
  177.  
  178.  
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement