Advertisement
Guest User

DBHelper

a guest
Jun 3rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. package ru.startandroid.p0030_firstproject;
  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. import android.util.Log;
  9.  
  10. import static android.content.ContentValues.TAG;
  11.  
  12. public class DBHelper extends SQLiteOpenHelper {
  13.  
  14.     public static final int DATABASE_VERSION = 1;
  15.     public static final String DATABASE_NAME = "contactDb";
  16.  
  17.     public static final String TABLE_USERS = "users";
  18.  
  19.     public static final String KEY_LOGIN = "login";
  20.     public static final String KEY_PASSWORD = "password";
  21.  
  22.     public static final String TABLE_CONTACTS = "contacts";
  23.  
  24.     public static final String KEY_ID = "_id";
  25.     public static final String KEY_ID0 = "id0";
  26.     public static final String KEY_FIRSTNAME = "firstname";
  27.     public static final String KEY_LASTNAME = "lastname";
  28.     public static final String KEY_PHONE = "phone";
  29.     public static final String KEY_ADDRESS = "address";
  30.     public static final String KEY_ZAMETKI = "zametki";
  31.  
  32.     public DBHelper(Context context) {
  33.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  34.     }
  35.  
  36.     @Override
  37.     public void onCreate(SQLiteDatabase db) {
  38.         db.execSQL("create table " + TABLE_CONTACTS + "(" + KEY_ID
  39.                 + " integer primary key autoincrement," + KEY_ID0+ " text" + KEY_FIRSTNAME
  40.                 + " text," + KEY_LASTNAME + " text" + KEY_PHONE + " text" + KEY_ADDRESS
  41.                 + " text" + KEY_ZAMETKI + " text" + ")");
  42.  
  43.         db.execSQL("create table " + TABLE_USERS + "(" + KEY_LOGIN
  44.                 + " text" + KEY_PASSWORD + " text" + ")");
  45.  
  46.     }
  47.  
  48.     @Override
  49.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  50.         db.execSQL("drop table if exists " + TABLE_CONTACTS);
  51.  
  52.         db.execSQL("drop table if exits " + TABLE_USERS);
  53.  
  54.         onCreate(db);
  55.  
  56.     }
  57.  
  58.     public void addContact(Contact contact) {
  59.         SQLiteDatabase db = this.getWritableDatabase();
  60.         ContentValues values = new ContentValues();
  61.  
  62.         values.put(KEY_ID0, contact.getID0());
  63.         values.put(KEY_FIRSTNAME, contact.getFirstName());
  64.         values.put(KEY_LASTNAME, contact.getLastName());
  65.         values.put(KEY_PHONE, contact.getPhone());
  66.         values.put(KEY_ADDRESS, contact.getAddress());
  67.         values.put(KEY_ZAMETKI, contact.getZametki());
  68.  
  69.         db.insert(TABLE_CONTACTS, null, values);
  70.         db.close();
  71.     }
  72.  
  73.     public void addUser(User user) {
  74.         Log.d(TAG, "Add user");
  75.  
  76.         SQLiteDatabase db = this.getWritableDatabase();
  77.         ContentValues values = new ContentValues();
  78.  
  79.         values.put(KEY_LOGIN, user.getLogin());
  80.         values.put(KEY_PASSWORD, user.getPassword());
  81.  
  82.         Log.d(TAG, "Prep insert");
  83.         db.insert(TABLE_USERS, null, values);
  84.         Log.d(TAG, "inserted");
  85.         db.close();
  86.     }
  87.  
  88.     public int updateContact(Contact contact) {
  89.         SQLiteDatabase db = this.getWritableDatabase();
  90.  
  91.         ContentValues values = new ContentValues();
  92.  
  93.         values.put(KEY_ID0, contact.getID0());
  94.         values.put(KEY_FIRSTNAME, contact.getFirstName());
  95.         values.put(KEY_LASTNAME, contact.getLastName());
  96.         values.put(KEY_PHONE, contact.getPhone());
  97.         values.put(KEY_ADDRESS, contact.getAddress());
  98.         values.put(KEY_ZAMETKI, contact.getZametki());
  99.  
  100.         return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
  101.                 new String[] { String.valueOf(contact.getID()) });
  102.     }
  103.  
  104.     public void delContact(Contact contact) {
  105.  
  106.         SQLiteDatabase db = this.getWritableDatabase();
  107.         db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { String.valueOf(contact.getID()) });
  108.         db.close();
  109.     }
  110.  
  111.     public Contact getContact(int id) {
  112.         SQLiteDatabase db = this.getReadableDatabase();
  113.  
  114.         Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, KEY_ID0,
  115.                         KEY_FIRSTNAME, KEY_LASTNAME, KEY_PHONE, KEY_ADDRESS,
  116.                         KEY_ZAMETKI }, KEY_ID + "=?",
  117.                 new String[] { String.valueOf(id) }, null, null, null, null);
  118.  
  119.         if (cursor != null){
  120.             cursor.moveToFirst();
  121.         }
  122.  
  123.         Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
  124.                 cursor.getString(1),
  125.                 cursor.getString(2),
  126.                 cursor.getString(3),
  127.                 cursor.getString(4),
  128.                 cursor.getString(5),
  129.                 cursor.getString(6));
  130.  
  131.         return contact;
  132.     }
  133.  
  134.     public User getUser(String login) {
  135.         SQLiteDatabase db = this.getReadableDatabase();
  136.  
  137.         Cursor cursor = db.query(TABLE_USERS, new String[] {KEY_LOGIN, KEY_PASSWORD }, KEY_LOGIN + "=?",
  138.                 new String[] { String.valueOf(login) }, null, null, null, null);
  139.  
  140.         if (cursor != null){
  141.             cursor.moveToFirst();
  142.         }
  143.  
  144.         User user = new User(cursor.getString(0), cursor.getString(1));
  145.  
  146.         return user;
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement