SQLITE DATABASE
----------------------Appcontroller class-----------------------
------------------LoginModel.class---------------------------
----------------------Appcontroller class-----------------------
package com.httpabcdefapp1123.databasenewlogin; import android.app.Application; import android.text.TextUtils; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.DiskBasedCache; import com.android.volley.toolbox.Volley; public class AppController extends Application { public static final String TAG = AppController.class.getSimpleName(); private RequestQueue mRequestQueue; private static AppController mInstance; DiskBasedCache cache; @Override public void onCreate() { super.onCreate(); mInstance = this; new LoginDataHelper(this); // new UserDetailDataHelper(this); // Create global configuration and initialize ImageLoader with this config } public DiskBasedCache getDiskCache() { if (cache == null) { cache = new DiskBasedCache(getExternalCacheDir(), 209715200); } return cache; } public static synchronized AppController getInstance() { return mInstance; } public RequestQueue getRequestQueue() { if (mRequestQueue == null) { mRequestQueue = Volley.newRequestQueue(getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req, String tag) { req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); getRequestQueue().add(req); } public <T> void addToRequestQueue(Request<T> req) { req.setTag(TAG); getRequestQueue().add(req); } public void cancelPendingRequests(Object tag) { if (mRequestQueue != null) { mRequestQueue.cancelAll(tag); } } }
-------DataManager.class---------------------
package com.httpabcdefapp1123.databasenewlogin; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DataManager extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 2; public static final String DATABASE_NAME = "CollegeLife"; public DataManager(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { LoginModel.creteTable(db); // UserDetailModel.creteTable(db); } @Override public void onUpgrade(SQLiteDatabase db, int paramInt1, int paramInt2) { LoginModel.dropTable(db); // UserDetailModel.dropTable(db);// SavedData.clear(); onCreate(db); } }
----------LoginDataHelper.class----------
package com.httpabcdefapp1123.databasenewlogin; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.View; import java.util.ArrayList; /** * Created by Meenakshi on 21/03/2017. */ public class LoginDataHelper { private static LoginDataHelper instance; private SQLiteDatabase db; private DataManager dm; Context cx; public LoginDataHelper(Context cx) { instance = this; this.cx = cx; dm = new DataManager(cx, DataManager.DATABASE_NAME, null, DataManager.DATABASE_VERSION); } public static LoginDataHelper getInstance() { return instance; } public void open() { db = dm.getWritableDatabase(); } public void close() { db.close(); } public void read() { db = dm.getReadableDatabase(); } public void delete(String email) { open(); db.delete(LoginModel.TABLE_NAME, LoginModel.KEY_USER_EMAIL + " = '" + email + "'", null); // db.execSQL("DELETE FROM " + CartModel.TABLE_NAME + " WHERE " + CartModel.KEY_PACKAGE_ID + "= '" + packageId);
close(); } public boolean isExist(String email) { Cursor clientCur = db.rawQuery("SELECT * FROM " + LoginModel.TABLE_NAME + " WHERE email = '" + email + "'", null); boolean exist = (clientCur.getCount() > 0); clientCur.close(); return exist; } public void insertClientDetail(LoginModel loginModel) { open(); ContentValues values = new ContentValues(); values.put(loginModel.KEY_USER_EMAIL, loginModel.getEmail()); values.put(loginModel.KEY_USER_PASSWORD, loginModel.getPassword()); values.put(loginModel.KEY_LOGIN_TYPE, loginModel.getLoginType()); if (!isExist(loginModel.getEmail())) db.insert(loginModel.TABLE_NAME, null, values); close(); } public void updateEntry(LoginModel loginModel) { // Define the updated row content. ContentValues updatedValues = new ContentValues(); // Assign values for each row. updatedValues.put(loginModel.KEY_USER_EMAIL, loginModel.getEmail()); updatedValues.put(loginModel.KEY_USER_PASSWORD, loginModel.getPassword()); updatedValues.put(loginModel.KEY_LOGIN_TYPE, loginModel.getLoginType()); String where="EMAIL = ?"; db.update(loginModel.TABLE_NAME,updatedValues, where, new String[]{loginModel.getEmail()}); } public void updateClientDetail(LoginModel loginModel) { open(); ContentValues updatedValues = new ContentValues(); updatedValues.put(loginModel.KEY_USER_EMAIL, loginModel.getEmail()); updatedValues.put(loginModel.KEY_USER_PASSWORD, loginModel.getPassword()); updatedValues.put(loginModel.KEY_LOGIN_TYPE, loginModel.getLoginType()); String where="EMAIL = ?"; db.update(loginModel.TABLE_NAME, updatedValues, " EMAIL = '" + loginModel.getEmail() + "'", null); close(); } public ArrayList<LoginModel> getLoginDetail() { ArrayList<LoginModel> userItem = new ArrayList<LoginModel>(); read(); Cursor clientCur = db.rawQuery("select * from LoginModel", null); if (clientCur != null && clientCur.getCount() > 0) { clientCur.moveToFirst(); do { LoginModel loginModel = new LoginModel(); loginModel.setEmail(clientCur.getString(clientCur.getColumnIndex(loginModel.KEY_USER_EMAIL))); loginModel.setPassword(clientCur.getString(clientCur.getColumnIndex(loginModel.KEY_USER_PASSWORD))); loginModel.setLoginType(clientCur.getString(clientCur.getColumnIndex(loginModel.KEY_LOGIN_TYPE))); userItem.add(loginModel); } while ((clientCur.moveToNext())); clientCur.close(); } close(); return userItem; } }
------------------LoginModel.class---------------------------
package com.httpabcdefapp1123.databasenewlogin; import android.database.sqlite.SQLiteDatabase; /** * Created by Meenakshi on 21/03/2017. */ public class LoginModel { public static final String TABLE_NAME = "LoginModel"; public static final String KEY_ID = "_id"; public static String KEY_USER_EMAIL = "email"; public static String KEY_USER_PASSWORD = "password"; public static String KEY_LOGIN_TYPE = "loginType"; public static void creteTable(SQLiteDatabase db) { String CREATE_CLIENTTABLE = "create table " + TABLE_NAME + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_USER_EMAIL + " text, "
+ KEY_USER_PASSWORD + " text, "
+ KEY_LOGIN_TYPE + " text " + ")"; db.execSQL(CREATE_CLIENTTABLE); } public static void dropTable(SQLiteDatabase db) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getLoginType() { return loginType; } public void setLoginType(String loginType) { this.loginType = loginType; } //setter gater private String email,password,loginType; }/////Thanks to Sohel Khan(Sir) for this Code.///////////
No comments:
Post a Comment