Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.v3.database;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- public class DBAdapter {
- // Initial Configuration
- public static final String DB_NAME = "message";
- private static final int DATABASE_VER = 1;
- private static final String TAG = "DBAdapter";
- // Set the Tables Key Words
- public static final String TABLE_USERS = "users";
- public static final String TABLE_MAILS = "mails";
- public static final String TABLE_DRAFTS = "drafts";
- public static final String TABLE_SENT = "sent";
- private final Context context;
- private DatabaseHelper DBHelper;
- private SQLiteDatabase db;
- private static final String CREATE_USERS = "CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, username TEXT, password TEXT);";
- 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);";
- 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);";
- 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);";
- // Keys
- public static final String ID = "id";
- public static final String KEY_NAME = "name";
- public static final String KEY_USERNAME = "username";
- public static final String KEY_PASSWORD = "password";
- public static final String PID = "pid";
- public static final String KEY_FROM = "from_id";
- public static final String KEY_TO = "to_id";
- public static final String KEY_SUB = "subject";
- public static final String KEY_BODY = "body";
- public static final String KEY_READ = "read";
- public static final String KEY_DATETIME = "datetime";
- public DBAdapter(Context ctx) {
- this.context = ctx;
- DBHelper = new DatabaseHelper(context);
- }
- private static class DatabaseHelper extends SQLiteOpenHelper {
- public DatabaseHelper(Context context) {
- super(context, DB_NAME, null, DATABASE_VER);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_USERS);
- db.execSQL(CREATE_MAILS);
- db.execSQL(CREATE_DRAFTS);
- db.execSQL(CREATE_SENT);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS titles");
- onCreate(db);
- }
- }
- public DBAdapter open() throws SQLException {
- db = DBHelper.getWritableDatabase();
- return this;
- }
- public void close() {
- DBHelper.close();
- }
- // Insert Users
- public long insertUser(String name, String username, String password) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_NAME, name);
- initialValues.put(KEY_USERNAME, username);
- initialValues.put(KEY_PASSWORD, password);
- return db.insert(TABLE_USERS, null, initialValues);
- }
- // Insert Mails
- public long insertMails(String from, String to, String subject,
- String body, String datetime, String read) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_FROM, from);
- initialValues.put(KEY_TO, to);
- initialValues.put(KEY_SUB, subject);
- initialValues.put(KEY_BODY, body);
- initialValues.put(KEY_DATETIME, datetime);
- initialValues.put(KEY_READ, read);
- return db.insert(TABLE_MAILS, null, initialValues);
- }
- // Insert Drafts
- public long insertDrafts(String from, String to, String subject,
- String body, String datetime) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_FROM, from);
- initialValues.put(KEY_TO, to);
- initialValues.put(KEY_SUB, subject);
- initialValues.put(KEY_BODY, body);
- initialValues.put(KEY_DATETIME, datetime);
- return db.insert(TABLE_DRAFTS, null, initialValues);
- }
- // Insert Mails
- public long insertSent(String from, String to, String subject, String body,
- String datetime) {
- ContentValues initialValues = new ContentValues();
- initialValues.put(KEY_FROM, from);
- initialValues.put(KEY_TO, to);
- initialValues.put(KEY_SUB, subject);
- initialValues.put(KEY_BODY, body);
- initialValues.put(KEY_DATETIME, datetime);
- return db.insert(TABLE_SENT, null, initialValues);
- }
- // Delete Section
- public boolean deleteUser(long Id) {
- return db.delete(TABLE_USERS, ID + "=" + Id, null) > 0;
- }
- public boolean deleteMails(long Id) {
- return db.delete(TABLE_MAILS, ID + "=" + Id, null) > 0;
- }
- public boolean deleteDrafts(long Id) {
- return db.delete(TABLE_DRAFTS, ID + "=" + Id, null) > 0;
- }
- public boolean deleteSent(long Id) {
- return db.delete(TABLE_SENT, ID + "=" + Id, null) > 0;
- }
- // GetALL Section
- public Cursor getInbox(long toId) throws SQLException {
- Cursor mCursor = db.query(true, TABLE_MAILS, new String[] { ID,
- KEY_FROM, KEY_TO, KEY_SUB, KEY_BODY, KEY_DATETIME, KEY_READ },
- KEY_TO + "=" + toId, null, null, null, null, null);
- if (mCursor != null) {
- mCursor.moveToFirst();
- }
- return mCursor;
- }
- public Cursor getAllDrafts(long fromId) throws SQLException {
- Cursor mCursor = db.query(true, TABLE_DRAFTS, new String[] { ID,
- KEY_FROM, KEY_TO, KEY_SUB, KEY_BODY, KEY_DATETIME, KEY_READ },
- KEY_FROM + "=" + fromId, null, null, null, null, null);
- if (mCursor != null) {
- mCursor.moveToFirst();
- }
- return mCursor;
- }
- public Cursor getAllSent(long fromId) throws SQLException {
- Cursor mCursor = db.query(true, TABLE_DRAFTS, new String[] { ID,
- KEY_FROM, KEY_TO, KEY_SUB, KEY_BODY, KEY_DATETIME, KEY_READ },
- KEY_FROM + "=" + fromId, null, null, null, null, null);
- if (mCursor != null) {
- mCursor.moveToFirst();
- }
- return mCursor;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement