Advertisement
PomozMi

SQLiteHandler

Nov 25th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.00 KB | None | 0 0
  1.  
  2. package filipkarol.zadania.helper;
  3.  
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10.  
  11. import java.io.IOException;
  12. import java.util.HashMap;
  13.  
  14. public class SQLiteHandler extends SQLiteOpenHelper {
  15.  
  16.     private static final String TAG = SQLiteHandler.class.getSimpleName();
  17.  
  18.     // All Static variables
  19.     // Database Version
  20.     private static final int DATABASE_VERSION = 1;
  21.  
  22.     // Database Name
  23.     private static final String DATABASE_NAME = "filipkarol_android_bd";// androidh_bdapp
  24.  
  25.     // Login table name
  26.     private static final String TABLE_USER = "user";
  27.  
  28.     // Login Table Columns names
  29.     private static final String KEY_ID = "id";
  30.     private static final String KEY_NAME = "name";
  31.     private static final String KEY_EMAIL = "email";
  32.     private static final String KEY_UID = "uid";
  33.     private static final String KEY_CREATED_AT = "created_at";
  34.  
  35.  
  36.     private static final String TABLE_PERSON = "Person";
  37.  
  38.     private static final String KEY_PERSON_ID = "id";
  39.     private static final String KEY_PERSON_EMAIL = "email";
  40.     private static final String KEY_PERSON_NAME = "name";
  41.     private static final String KEY_PERSON_SURNAME = "surname";
  42.  
  43.  
  44.     private static final String TABLE_ACTION = "Action";
  45.  
  46.     private static final String KEY_ACTION_ID = "id";
  47.     private static final String KEY_ACTION_NAME = "name";
  48.     private static final String KEY_ACTION_ISDONE = "isDone";
  49.     private static final String KEY_ACTION_TYPE = "type";
  50.     private static final String KEY_ACTION_EXPECTEDTIME = "expectedTime";
  51.     private static final String KEY_ACTION_IDGROUP = "idGroup";
  52.  
  53.  
  54.     private static final String TABLE_PERSONACTION = "PersonAction";
  55.  
  56.     private static final String KEY_PERSONACTION_ID = "id";
  57.     private static final String KEY_PERSONACTION_IDPERSON = "idPerson";
  58.     private static final String KEY_PERSONACTION_IDACTION = "idAction";
  59.     private static final String KEY_PERSONACTION_DATEFROM = "dateFrom";
  60.     private static final String KEY_PERSONACTION_DATETO = "dateTo";
  61.  
  62.     private static final String TABLE_ACTIONDETAILS = "ActionDetails";
  63.  
  64.     private static final String KEY_ACTIONDETAILS_ID = "id";
  65.     private static final String KEY_ACTIONDETAILS_IDPLACE = "idPlace";
  66.     private static final String KEY_ACTIONDETAILS_DESCR = "descr";
  67.     private static final String KEY_ACTIONDETAILS_DATEFROM = "dateFrom";
  68.     private static final String KEY_ACTIONDETAILS_DATETO = "dateTo";
  69.  
  70.     private static final String TABLE_PLACE = "Place";
  71.  
  72.     private static final String KEY_PLACE_ID = "id";
  73.     private static final String KEY_PLACE_NAME = "name";
  74.     private static final String KEY_PLACE_ADDRESS = "address";
  75.     private static final String KEY_PLACE_LAT = "lat";
  76.     private static final String KEY_PLACE_LNG = "lng";
  77.     private static final String KEY_PLACE_TYPE = "type";
  78.  
  79.     private static final String TABLE_GROUP = "Group";
  80.  
  81.     private static final String KEY_GROUP_ID = "id";
  82.     private static final String KEY_GROUP_NAME ="name";
  83.  
  84.     private static final String TABLE_PERSONGROUP = "PersonGroup";
  85.  
  86.     private static final String KEY_PERSONGROUP_ID = "id";
  87.     private static final String KEY_PERSONGROUP_IDPERSON = "idPerson";
  88.     private static final String KEY_PERSONGROUP_IDGROUP = "idGroup";
  89.  
  90.  
  91.     public SQLiteHandler(Context context) {
  92.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  93.     }
  94.  
  95.     // Creating Tables
  96.     @Override
  97.     public void onCreate(SQLiteDatabase db) {
  98.  
  99.         String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
  100.                 + KEY_ID +          " INTEGER PRIMARY KEY,"
  101.                 + KEY_NAME +        " TEXT,"
  102.                 + KEY_EMAIL +       " TEXT UNIQUE,"
  103.                 + KEY_UID +         " TEXT,"
  104.                 + KEY_CREATED_AT +  " TEXT"
  105.                 + ")";
  106.         db.execSQL(CREATE_LOGIN_TABLE); // Ka:w sumie to można wywalic tą tabelkę
  107.  
  108.  
  109.         String CREATE_PERSON_TABLE = "CREATE TABLE "
  110.                 + TABLE_PERSON + "("
  111.                 + KEY_PERSON_ID +    " INTEGER PRIMARY KEY,"
  112.                 + KEY_PERSON_EMAIL + " TEXT UNIQUE,"
  113.                 + KEY_PERSON_NAME +  " TEXT,"
  114.                 + KEY_PERSON_SURNAME +" TEXT"
  115.                 + ")";
  116.         db.execSQL(CREATE_PERSON_TABLE );
  117.  
  118.  
  119.         String CREATE_ACTION_TABLE = "CREATE TABLE "
  120.                 + TABLE_ACTION + "("
  121.                 + KEY_ACTION_ID +           " INTEGER PRIMARY KEY,"
  122.                 + KEY_ACTION_NAME +         " TEXT,"
  123.                 + KEY_ACTION_ISDONE +       " INTEGER,"
  124.                 + KEY_ACTION_TYPE +         " TEXT,"
  125.                 + KEY_ACTION_EXPECTEDTIME + " INTEGER,"  // Ka:time  - >  min
  126.                 + KEY_ACTION_IDGROUP +      " INTEGER "
  127.                 + ")";
  128.         db.execSQL(CREATE_ACTION_TABLE);
  129.  
  130.  
  131.         String CREATE_PERSONACTION_TABLE = "CREATE TABLE "
  132.                 + TABLE_PERSONACTION + "("
  133.                 + KEY_PERSONACTION_ID +       " INTEGER PRIMARY KEY,"
  134.                 + KEY_PERSONACTION_IDPERSON + " INTEGER, "
  135.                 + KEY_PERSONACTION_IDACTION + " INTEGER, "
  136.                 + KEY_PERSONACTION_DATEFROM + " DATETIME,"
  137.                 + KEY_PERSONACTION_DATETO +   " DATETIME"
  138.                 + ")";
  139.         db.execSQL(CREATE_PERSONACTION_TABLE);
  140.  
  141.  
  142.         String CREATE_ACTIONDETAILS_TABLE = "CREATE TABLE "
  143.                 + TABLE_ACTIONDETAILS + "("
  144.                 + KEY_ACTIONDETAILS_ID +        " INTEGER PRIMARY KEY,"
  145.                 + KEY_ACTIONDETAILS_IDPLACE +   " INTEGER, "
  146.                 + KEY_ACTIONDETAILS_DESCR +     " TEXT, "
  147.                 + KEY_ACTIONDETAILS_DATEFROM +  " DATETIME,"
  148.                 + KEY_ACTIONDETAILS_DATETO +    " DATETIME"
  149.                 + ")";
  150.         db.execSQL(CREATE_ACTIONDETAILS_TABLE);
  151.  
  152.  
  153.         String CREATE_PLACE_TABLE = "CREATE TABLE "
  154.                 + TABLE_PLACE+ "("
  155.                 + KEY_PLACE_ID +        " INTEGER PRIMARY KEY,"
  156.                 + KEY_PLACE_NAME +      " TEXT,"
  157.                 + KEY_PLACE_ADDRESS +   " TEXT,"
  158.                 + KEY_PLACE_LAT +       " REAL," // Ka:to real to niby na nasze float  http://www.sqlite.org/datatype3.html
  159.                 + KEY_PLACE_LNG +       " REAL,"
  160.                 + KEY_PLACE_TYPE +      " TEXT"
  161.                 + ")";
  162.         db.execSQL(CREATE_PLACE_TABLE);
  163.  
  164.  
  165.         String CREATE_GROUP_TABLE = "CREATE TABLE "
  166.                 + TABLE_GROUP+ "("
  167.                 + KEY_GROUP_ID +        " INTEGER PRIMARY KEY,"
  168.                 + KEY_GROUP_NAME +      " TEXT"
  169.                 + ")";
  170.         db.execSQL(CREATE_GROUP_TABLE);
  171.  
  172.  
  173.         String CREATE_PERSONGROUP_TABLE = "CREATE TABLE "
  174.                 + TABLE_PERSONGROUP+ "("
  175.                 + KEY_PERSONGROUP_ID +      " INTEGER PRIMARY KEY,"
  176.                 + KEY_PERSONGROUP_IDGROUP + " INTEGER, "
  177.                 + KEY_PERSONGROUP_IDPERSON +" INTEGER"
  178.                 + ")";
  179.         db.execSQL(CREATE_PERSONGROUP_TABLE);
  180.  
  181.  
  182.         Log.d(TAG, "Database tables created");
  183.     }
  184.  
  185.     // Upgrading database
  186.     @Override
  187.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  188.         // Drop older table if existed
  189.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
  190.  
  191.         // Ka:tutaj se zrobimy jak będzie Upgrade
  192.  
  193.         // Create tables again
  194.         onCreate(db);
  195.     }
  196.  
  197.     /**
  198.      * Storing user details in database
  199.      * */
  200.     public void addUser(String name, String email, String uid, String created_at) {
  201.         SQLiteDatabase db = this.getWritableDatabase();
  202.  
  203.         ContentValues values = new ContentValues();
  204.         values.put(KEY_NAME, name); // Name
  205.         values.put(KEY_EMAIL, email); // Email
  206.         values.put(KEY_UID, uid); // Email
  207.         values.put(KEY_CREATED_AT, created_at); // Created At
  208.  
  209.         // Inserting Row
  210.         long id = db.insert(TABLE_USER, null, values);
  211.         db.close(); // Closing database connection
  212.  
  213.         Log.d(TAG, "New user inserted into sqlite: " + id);
  214.     }
  215.     public void addPerson(String email, String name, String surname ) {
  216.         SQLiteDatabase db = this.getWritableDatabase();
  217.  
  218.         ContentValues values = new ContentValues();
  219.         values.put(KEY_PERSON_EMAIL, email);
  220.         values.put(KEY_PERSON_NAME, name);
  221.         values.put(KEY_PERSON_SURNAME, surname);
  222.  
  223.         long id = db.insert(TABLE_PERSON, null, values);
  224.         db.close();
  225.  
  226.         Log.d(TAG, "New Person inserted into sqlite: " + id);
  227.     }
  228.     public void addAction(String name, int isdone, String type, int expectedtime, int idgroup) {
  229.         SQLiteDatabase db = this.getWritableDatabase();
  230.  
  231.         ContentValues values = new ContentValues();
  232.         values.put(KEY_ACTION_NAME, name);
  233.         values.put(KEY_ACTION_ISDONE, isdone);
  234.         values.put(KEY_ACTION_TYPE, type);
  235.         values.put(KEY_ACTION_EXPECTEDTIME, expectedtime);
  236.         values.put(KEY_ACTION_IDGROUP, idgroup);
  237.  
  238.         long id = db.insert(TABLE_ACTION, null, values);
  239.         db.close();
  240.  
  241.         Log.d(TAG, "New Action inserted into sqlite: " + id);
  242.     }
  243.     // add PersonACTION hmmm
  244.     public void addActionDetails( int idplace, String descr,String datefrom, String dateto) {
  245.         SQLiteDatabase db = this.getWritableDatabase();
  246.  
  247.         ContentValues values = new ContentValues();
  248.         values.put(KEY_ACTIONDETAILS_IDPLACE, idplace );
  249.         values.put(KEY_ACTIONDETAILS_DESCR, descr );
  250.         values.put(KEY_ACTIONDETAILS_DATEFROM, datefrom );
  251.         values.put(KEY_ACTIONDETAILS_DATETO, dateto );
  252.  
  253.  
  254.         long id = db.insert(TABLE_ACTIONDETAILS, null, values);
  255.         db.close();
  256.  
  257.         Log.d(TAG, "New ACTIONDETAILS inserted into sqlite: " + id);
  258.     }
  259.     public void addPlace( String name,String address,float lat, float lng, String type) {
  260.         SQLiteDatabase db = this.getWritableDatabase();
  261.  
  262.         ContentValues values = new ContentValues();
  263.         values.put(KEY_PLACE_NAME, name);
  264.         values.put(KEY_PLACE_ADDRESS, address);
  265.         values.put(KEY_PLACE_LAT, lat);
  266.         values.put(KEY_PLACE_LNG, lng);
  267.         values.put(KEY_PLACE_TYPE, type);
  268.  
  269.         long id = db.insert(TABLE_PLACE, null, values);
  270.         db.close();
  271.  
  272.         Log.d(TAG, "New PLACE inserted into sqlite: " + id);
  273.     }
  274.     public void addGroup( String name) {
  275.         SQLiteDatabase db = this.getWritableDatabase();
  276.  
  277.         ContentValues values = new ContentValues();
  278.         values.put(KEY_GROUP_NAME, name);
  279.         long id = db.insert(TABLE_GROUP, null, values);
  280.         db.close();
  281.  
  282.         Log.d(TAG, "New GROUP inserted into sqlite: " + id);
  283.     }
  284.  
  285.  
  286.     public void addPersonToGroup(String email, String nameGroup) {
  287.         int idGroup = 0;
  288.         int idPerson = 0;
  289.  
  290.         SQLiteDatabase db1 = this.getReadableDatabase();
  291.  
  292.         Cursor cursor = db1.query(
  293.                 TABLE_PERSON,                            // The table to query
  294.                 new String[]{KEY_PERSON_ID},            // The columns to return
  295.                 "email = " + email,                    // The columns for the WHERE clause
  296.                 null,                                    // The values for the WHERE clause
  297.                 null,                                   // don't group the rows
  298.                 null,                                    // don't filter by row groups
  299.                 null                                    // The sort order
  300.         );  // http://stackoverflow.com/questions/1243199/how-to-perform-an-sqlite-query-within-an-android-application
  301.         // http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow
  302.  
  303.         if (cursor != null && cursor.moveToFirst()) { // http://www.android4devs.pl/2011/07/sqlite-androidzie-kompletny-poradnik-dla-poczatkujacych/
  304.             idPerson = Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_PERSON_ID)));
  305.         }
  306.  
  307.  
  308.         Cursor cursor2 = db1.query(
  309.                 TABLE_GROUP,                            // The table to query
  310.                 new String[]{KEY_GROUP_ID},            // The columns to return
  311.                 "name = " + nameGroup,                    // The columns for the WHERE clause
  312.                 null,                                    // The values for the WHERE clause
  313.                 null,                                   // don't group the rows
  314.                 null,                                    // don't filter by row groups
  315.                 null                                    // The sort order
  316.         );  // http://stackoverflow.com/questions/1243199/how-to-perform-an-sqlite-query-within-an-android-application
  317.         // http://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow
  318.  
  319.         if (cursor != null && cursor.moveToFirst()) { // http://www.android4devs.pl/2011/07/sqlite-androidzie-kompletny-poradnik-dla-poczatkujacych/
  320.             idGroup = Integer.parseInt(cursor.getString(cursor.getColumnIndex(KEY_GROUP_ID)));
  321.         }
  322.  
  323.         if (idPerson == 0 || idGroup == 0) {
  324.             Log.e(TAG, "addPersonToGroup  idPerson, idGroup " + idPerson + " " + idGroup );
  325.             return;
  326.         }
  327.  
  328.         SQLiteDatabase db = this.getWritableDatabase();
  329.  
  330.         ContentValues values = new ContentValues();
  331.         values.put(KEY_PERSONGROUP_IDGROUP, idGroup);
  332.         values.put(KEY_PERSONGROUP_IDPERSON, idPerson);
  333.  
  334.         long id = db.insert(TABLE_PERSONGROUP, null, values);
  335.         db.close();
  336.  
  337.         Log.d(TAG, "New PERSONGROUP inserted into sqlite: " + id);
  338.  
  339.     }
  340.  
  341.     /**
  342.      * Getting user data from database
  343.      * */
  344.     public HashMap<String, String> getUserDetails() {
  345.         HashMap<String, String> user = new HashMap<String, String>();
  346.         String selectQuery = "SELECT  * FROM " + TABLE_USER;
  347.  
  348.         SQLiteDatabase db = this.getReadableDatabase();
  349.         Cursor cursor = db.rawQuery(selectQuery, null);
  350.         // Move to first row
  351.         cursor.moveToFirst();
  352.         if (cursor.getCount() > 0) {
  353.             user.put("name", cursor.getString(1));
  354.             user.put("email", cursor.getString(2));
  355.             user.put("uid", cursor.getString(3));
  356.             user.put("created_at", cursor.getString(4));
  357.         }
  358.         cursor.close();
  359.         db.close();
  360.         // return user
  361.         Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
  362.  
  363.         return user;
  364.     }
  365.  
  366.     /**
  367.      * Re crate database Delete all tables and create them again
  368.      * */
  369.     public void deleteUsers() {
  370.         SQLiteDatabase db = this.getWritableDatabase();
  371.         // Delete All Rows
  372.         db.delete(TABLE_USER, null, null);
  373.         db.close();
  374.  
  375.         Log.d(TAG, "Deleted all user info from sqlite");
  376.     }
  377.  
  378. }
  379.  
  380. /**
  381.  * Autor Karol
  382.  * Thanks Ravi Tamada
  383.  * URL: www.androidhive.info
  384.  * twitter: http://twitter.com/ravitamada
  385.  * */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement