package com.daifan.dao; /** * Created by ronghao on 6/22/13. */ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import com.daifan.domain.User; public class UserDao extends SQLiteOpenHelper { // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "51daifan"; // Login table name private static final String TABLE_USER = "user"; // Login Table Columns names private static final String KEY_ID = "id"; private static final String KEY_NAME = "name"; private static final String KEY_EMAIL = "email"; private static final String KEY_UID = "uid"; private static final String KEY_CREATED_AT = "created_at"; public UserDao(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } // Creating Tables @Override public void onCreate(SQLiteDatabase db) { String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT," + KEY_CREATED_AT + " TEXT" + ")"; db.execSQL(CREATE_LOGIN_TABLE); } // Upgrading database @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // Drop older table if existed db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER); // Create tables again onCreate(db); } /** * Storing user details in database * */ public void addUser(User user) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME, user.getName()); // Name values.put(KEY_EMAIL, user.getEmail()); // Email values.put(KEY_UID, user.getId()); // Email values.put(KEY_CREATED_AT, user.getCreatedAt()); // Created At // Inserting Row db.insert(TABLE_USER, null, values); db.close(); // Closing database connection } /** * Getting user data from database * */ public User getUser(){ User user = new User(); String selectQuery = "SELECT * FROM " + TABLE_USER; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); // Move to first row cursor.moveToFirst(); if(cursor.getCount() > 0){ user.setName(cursor.getString(1)); user.setEmail(cursor.getString(2)); user.setId(cursor.getString(3)); user.setCreatedAt(cursor.getString(4)); } cursor.close(); db.close(); // return user return user; } /** * Getting user login status * return true if rows are there in table * */ public int getRowCount() { String countQuery = "SELECT * FROM " + TABLE_USER; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int rowCount = cursor.getCount(); db.close(); cursor.close(); // return row count return rowCount; } /** * Re crate database * Delete all tables and create them again * */ public void resetTables(){ SQLiteDatabase db = this.getWritableDatabase(); // Delete All Rows db.delete(TABLE_USER, null, null); db.close(); } }