Advertisement
Guest User

GANDA

a guest
Apr 30th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. package com.dash1.attendancemonitoring.sql;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. import com.dash1.attendancemonitoring.model.User;
  10.  
  11. /**
  12. * Created by Vi Anne on 29/04/2017.
  13. */
  14. public class DatabaseHelper extends SQLiteOpenHelper{
  15.  
  16. private static final int DATABASE_VERSION=1;
  17. private static final String DATABASE_NAME= "UserManager.db";
  18. private static final String TABLE_USER= "user";
  19. private static final String COLUMN_USER_ID = "user_id";
  20. private static final String COLUMN_USER_NAME = "user_name";
  21. private static final String COLUMN_USER_USERNAME = "user_username";
  22. private static final String COLUMN_USER_PASSWORD = "user_password";
  23.  
  24. private String CREATE_USER_TABLE="CREATE TABLE" + TABLE_USER + "{"
  25. + COLUMN_USER_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_USER_NAME + "TEXT,"
  26. + COLUMN_USER_USERNAME + "TEXT," + COLUMN_USER_PASSWORD + "}";
  27.  
  28. private String DROP_USER_TABLE= "DROP TABLE IF EXISTS" + TABLE_USER;
  29.  
  30. public DatabaseHelper(Context context){
  31. super(context, DATABASE_NAME , null, DATABASE_VERSION);
  32.  
  33. }
  34.  
  35. @Override
  36. public void onCreate(SQLiteDatabase db){
  37. db.execSQL(CREATE_USER_TABLE);
  38. }
  39.  
  40. @Override
  41. public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
  42. db.execSQL(DROP_USER_TABLE);
  43. onCreate(db);
  44.  
  45. }
  46.  
  47. public void addUser (User user){
  48. SQLiteDatabase db= this.getWritableDatabase();
  49. ContentValues values= new ContentValues();
  50. values.put(COLUMN_USER_NAME,user.getName());
  51. values.put(COLUMN_USER_USERNAME,user.getUsername());
  52. values.put(COLUMN_USER_PASSWORD, user.getPassword());
  53. db.insert(TABLE_USER, null, values);
  54. db.close();
  55. }
  56.  
  57.  
  58. public boolean checkUser (String username, String password){
  59. String[] columns= {
  60. COLUMN_USER_ID
  61. };
  62. SQLiteDatabase db= this.getWritableDatabase();
  63. String selection = COLUMN_USER_USERNAME + "= ?" ;
  64. String[] selectionArgs ={username};
  65.  
  66. Cursor cursor = db.query(TABLE_USER,
  67. columns,
  68. selection,
  69. selectionArgs,
  70. null,
  71. null,
  72. null);
  73. int cursorCount = cursor.getCount();
  74. cursor.close();
  75. db.close();
  76.  
  77. if (cursorCount> 0){
  78. return true;
  79. }
  80. return false;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement