Guest User

Untitled

a guest
May 12th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1.  
  2. package umn.ti;
  3.  
  4. import android.content.ContentValues;
  5. import android.content.Context;
  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. public static final String KEY_USERNAME = "username";
  13. public static final String KEY_PASSWORD = "password";
  14. private static final String TAG = "DBAdapter";
  15.  
  16. private static final String DATABASE_NAME = "UMN";
  17. private static final String DATABASE_TABLE = "accounts";
  18. private static final int DATABASE_VERSION = 2;
  19.  
  20. private static final String DATABASE_CREATE =
  21. "create table accounts (username varchar(100) primary key, "
  22. + "password varchar(100) not null);";
  23.  
  24. private final Context context;
  25.  
  26. private DatabaseHelper DBHelper;
  27. private SQLiteDatabase db;
  28.  
  29. public DBAdapter(Context ctx)
  30. {
  31. this.context = ctx;
  32. DBHelper = new DatabaseHelper(context);
  33. }
  34.  
  35. private static class DatabaseHelper extends SQLiteOpenHelper
  36. {
  37. DatabaseHelper(Context context)
  38. {
  39. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  40. }
  41.  
  42. @Override
  43. public void onCreate(SQLiteDatabase db)
  44. {
  45. try {
  46. db.execSQL(DATABASE_CREATE);
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51.  
  52. @Override
  53. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  54. {
  55. Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  56. + newVersion + ", which will destroy all old data");
  57. db.execSQL("DROP TABLE IF EXISTS contacts");
  58. onCreate(db);
  59. }
  60. }
  61.  
  62. //---opens the database---
  63. public DBAdapter open() throws SQLException
  64. {
  65. db = DBHelper.getWritableDatabase();
  66. return this;
  67. }
  68.  
  69. //---closes the database---
  70. public void close()
  71. {
  72. DBHelper.close();
  73. }
  74.  
  75. //---insert a contact into the database---
  76. public long insertAccount(String username, String password)
  77. {
  78. ContentValues initialValues = new ContentValues();
  79. initialValues.put(KEY_USERNAME, username);
  80. initialValues.put(KEY_PASSWORD, password);
  81. return db.insert(DATABASE_TABLE, null, initialValues);
  82. }
  83. }
Add Comment
Please, Sign In to add comment