Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calymayor.chihuahuatic.model;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by miguel on 10/21/16.
- */
- public class DBHandlerCTic extends SQLiteOpenHelper {
- private static final String LOG = "DatabaseHelperCTic";
- private static final int DATABASE_VERSION = 1;
- private static final String DATABASE_NAME = "DBCTic";
- //Table names
- private static final String TABLE_USERS = "users";
- private static final String TABLE_INSPECTIONS = "inspections";
- private static final String TABLE_INSPECTIONS_INCIDENTS = "inspectionsincidents";
- //Column names
- private static final String KEY_USERNAME_ID = "idusername";
- private static final String KEY_CREATED = "created";
- private static final String KEY_EXPIRES = "expires";
- private static final String KEY_USERNAME = "username";
- private static final String KEY_PASSWORD = "password";
- private static final String KEY_INSPECTION_ID = "idinspection";
- private static final String KEY_INSPECTION_DATE = "date";
- private static final String KEY_TYPE = "type";
- private static final String KEY_RESP = "resp";
- private static final String KEY_DETAIL = "detail";
- private static final String KEY_INSPECTION_INCIDENTS_ID = "idinspectionsincidents";
- private static final String KEY_INCIDENT_STANDAR = "standar";
- private static final String KEY_INCIDENT_DATE = "date";
- private static final String KEY_LOCATION = "location";
- private static final String KEY_STATIONING = "stationing";
- private static final String KEY_JUNCTION = "junction";
- private static final String KEY_LOOP = "loop";
- private static final String KEY_WAY = "way";
- private static final String KEY_DISREPAIR = "disrepair";
- private static final String KEY_DISREPAIR_TYPE = "disrepairtype";
- private static final String KEY_DISREPAIR_LENGTH = "disrepairlength";
- private static final String KEY_DISREPAIR_DIMENSIONS = "disrepairdimensions";
- private static final String KEY_DISREPAIR_PHOTO = "disrepairphoto";
- /*
- CREATE TABLE users (idusername INTEGER PRIMARY KEY,created DATETIME, expires TEXT, username TEXT, password TEXT)
- * */
- //Table statements
- private static final String CREATE_TABLE_USERS =
- "CREATE TABLE " + TABLE_USERS
- + "(" + KEY_USERNAME_ID + " INTEGER PRIMARY KEY,"
- + KEY_CREATED + " TEXT,"
- + KEY_EXPIRES + " TEXT,"
- + KEY_USERNAME + " TEXT,"
- + KEY_PASSWORD + " TEXT" + ")";
- private static final String CREATE_TABLE_INSPECTIONS =
- "CREATE TABLE " + TABLE_INSPECTIONS
- + "(" + KEY_INSPECTION_ID + " INTEGER PRIMARY KEY,"
- + KEY_INSPECTION_DATE + " TEXT,"
- + KEY_TYPE + " TEXT,"
- + KEY_RESP + " TEXT,"
- + KEY_DETAIL + " TEXT" + ")";
- private static final String CREATE_TABLE_INSPECTIONS_INCIDENTS =
- "CREATE TABLE " + TABLE_INSPECTIONS_INCIDENTS
- + "(" + KEY_INSPECTION_INCIDENTS_ID + " INTEGER PRIMARY KEY,"
- + KEY_INCIDENT_STANDAR + " TEXT,"
- + KEY_INCIDENT_DATE + " TEXT,"
- + KEY_LOCATION + " TEXT,"
- + KEY_STATIONING + " TEXT,"
- + KEY_JUNCTION + " TEXT,"
- + KEY_LOOP + " TEXT,"
- + KEY_WAY + " TEXT,"
- + KEY_DISREPAIR + " TEXT,"
- + KEY_DISREPAIR_TYPE + " TEXT,"
- + KEY_DISREPAIR_LENGTH + " TEXT,"
- + KEY_DISREPAIR_DIMENSIONS + " TEXT,"
- + KEY_DISREPAIR_PHOTO + " TEXT" + ")";
- // public DBHandlerCTic(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
- public DBHandlerCTic(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_TABLE_USERS);
- db.execSQL(CREATE_TABLE_INSPECTIONS);
- db.execSQL(CREATE_TABLE_INSPECTIONS_INCIDENTS);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXIST" + TABLE_USERS);
- db.execSQL("DROP TABLE IF EXIST" + TABLE_INSPECTIONS);
- db.execSQL("DROP TABLE IF EXIST" + TABLE_INSPECTIONS_INCIDENTS);
- onCreate(db);
- }
- //CRUD Operations
- /*
- public long createToDo(Todo todo, long[] tag_ids) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(KEY_TODO, todo.getNote());
- values.put(KEY_STATUS, todo.getStatus());
- values.put(KEY_CREATED_AT, getDateTime());
- // insert row
- long todo_id = db.insert(TABLE_TODO, null, values);
- // assigning tags to todo
- for (long tag_id : tag_ids) {
- createTodoTag(todo_id, tag_id);
- }
- return todo_id;
- }
- * */
- //Insert Values
- public void addUser(Users users){
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(KEY_CREATED, users.getCreated());
- values.put(KEY_EXPIRES, users.getExpires());
- values.put(KEY_USERNAME, users.getUsername());
- values.put(KEY_PASSWORD, users.getPassword());
- db.insert(TABLE_USERS, null, values);
- db.close();
- }
- public void addInspection(Inspections inspections){
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(KEY_INSPECTION_DATE, inspections.getDate());
- values.put(KEY_TYPE, inspections.getType());
- values.put(KEY_RESP, inspections.getResp());
- values.put(KEY_DETAIL, inspections.getDetail());
- db.insert(TABLE_INSPECTIONS, null, values);
- db.close();
- }
- public void addInspectionIncidents(InspectionsIncidents inspectionsIncidents){
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(KEY_INCIDENT_STANDAR, inspectionsIncidents.getStandar());
- values.put(KEY_INCIDENT_DATE, inspectionsIncidents.getDate());
- values.put(KEY_LOCATION, inspectionsIncidents.getLocation());
- values.put(KEY_STATIONING, inspectionsIncidents.getStationing());
- values.put(KEY_JUNCTION, inspectionsIncidents.getJunction());
- values.put(KEY_LOOP, inspectionsIncidents.getLoop());
- values.put(KEY_WAY, inspectionsIncidents.getWay());
- values.put(KEY_DISREPAIR, inspectionsIncidents.getDisrepair());
- values.put(KEY_DISREPAIR_TYPE, inspectionsIncidents.getDisrepairtype());
- values.put(KEY_DISREPAIR_LENGTH, inspectionsIncidents.getDisrepairlength());
- values.put(KEY_DISREPAIR_DIMENSIONS, inspectionsIncidents.getDisrepairdimensions());
- values.put(KEY_DISREPAIR_PHOTO, inspectionsIncidents.getDisrepairphoto());
- db.insert(TABLE_INSPECTIONS_INCIDENTS, null, values);
- db.close();
- }
- //Read values
- public Users getUser(int id){
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor =
- db.query(TABLE_USERS,
- new String[]{KEY_USERNAME_ID, KEY_CREATED, KEY_EXPIRES, KEY_USERNAME, KEY_PASSWORD},
- KEY_USERNAME_ID +"=?",
- new String[] {String.valueOf(id)}, null, null, null, null);
- if(cursor != null)
- cursor.moveToFirst();
- Users user = new Users(
- Integer.parseInt(cursor.getString(0)),
- cursor.getString(1), cursor.getString(2),
- cursor.getString(3), cursor.getString(4));
- return user;
- }
- public List<Users> getAllUsers(){
- List<Users> usersList = new ArrayList<Users>();
- String selectQuery = "SELECT * FROM "+TABLE_USERS;
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- if(cursor.moveToFirst()){
- do{
- Users users = new Users();
- users.setIdusername(Integer.parseInt(cursor.getString(0)));
- users.setCreated(cursor.getString(1));
- users.setExpires(cursor.getString(2));
- users.setUsername(cursor.getString(3));
- users.setPassword(cursor.getString(4));
- usersList.add(users);
- }while(cursor.moveToNext());
- }
- return usersList;
- }
- public int getUsersCount(){
- String countQuery = "SELECT * FROM " +TABLE_USERS;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
- cursor.close();
- return cursor.getCount();
- }
- public Inspections getInspections(int id){
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor =
- db.query(TABLE_INSPECTIONS,
- new String[]{KEY_INSPECTION_ID, KEY_INSPECTION_DATE, KEY_TYPE, KEY_RESP, KEY_DETAIL},
- KEY_INSPECTION_ID +"=?",
- new String[] {String.valueOf(id)}, null, null, null, null);
- if(cursor != null)
- cursor.moveToFirst();
- Inspections inspection = new Inspections(
- Integer.parseInt(cursor.getString(0)),
- cursor.getString(1), cursor.getString(2),
- cursor.getString(3), cursor.getString(4));
- return inspection;
- }
- public List<Inspections> getAllInspections(){
- List<Inspections> inspectionList = new ArrayList<Inspections>();
- String selectQuery = "SELECT * FROM "+TABLE_INSPECTIONS;
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- if(cursor.moveToFirst()){
- do{
- Inspections inspection = new Inspections();
- inspection.setIdinspection(Integer.parseInt(cursor.getString(0)));
- inspection.setDate(cursor.getString(1));
- inspection.setType(cursor.getString(2));
- inspection.setResp(cursor.getString(3));
- inspection.setDetail(cursor.getString(4));
- inspectionList.add(inspection);
- }while(cursor.moveToNext());
- }
- return inspectionList;
- }
- public List<InspectionsIncidents> getAllInspectionsIncidents(){
- List<InspectionsIncidents> inspectionIncidentsList = new ArrayList<InspectionsIncidents>();
- String selectQuery = "SELECT * FROM "+TABLE_INSPECTIONS_INCIDENTS;
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- if(cursor.moveToFirst()){
- do{
- InspectionsIncidents inspectionIncidents = new InspectionsIncidents();
- inspectionIncidents.setIdinspectionsincidents(Integer.parseInt(cursor.getString(0)));
- inspectionIncidents.setStandar(cursor.getString(1));
- inspectionIncidents.setDate(cursor.getString(2));
- inspectionIncidents.setLocation(cursor.getString(3));
- inspectionIncidents.setStationing(cursor.getString(4));
- inspectionIncidents.setJunction(cursor.getString(5));
- inspectionIncidents.setLoop(cursor.getString(6));
- inspectionIncidents.setWay(cursor.getString(7));
- inspectionIncidents.setDisrepair(cursor.getString(8));
- inspectionIncidents.setDisrepairtype(cursor.getString(9));
- inspectionIncidents.setDisrepairlength(cursor.getString(10));
- inspectionIncidents.setDisrepairdimensions(cursor.getString(11));
- inspectionIncidents.setDisrepairphoto(cursor.getString(12));
- inspectionIncidentsList.add(inspectionIncidents);
- }while(cursor.moveToNext());
- }
- return inspectionIncidentsList;
- }
- public int getInspectionsCount(){
- String countQuery = "SELECT * FROM " +TABLE_INSPECTIONS;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
- cursor.close();
- return cursor.getCount();
- }
- //Updating
- public int updateUsers(Users users){
- SQLiteDatabase db =this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(KEY_CREATED, users.getCreated());
- values.put(KEY_EXPIRES, users.getExpires());
- values.put(KEY_USERNAME, users.getUsername());
- values.put(KEY_PASSWORD, users.getPassword());
- return db.update(TABLE_USERS, values, KEY_USERNAME_ID + " = ?",
- new String[]{String.valueOf(users.getIdusername())});
- }
- public int updateInspections(Inspections inspections){
- SQLiteDatabase db =this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(KEY_INSPECTION_DATE, inspections.getDate());
- values.put(KEY_TYPE, inspections.getType());
- values.put(KEY_RESP, inspections.getResp());
- values.put(KEY_DETAIL, inspections.getDetail());
- return db.update(TABLE_INSPECTIONS, values, KEY_INSPECTION_ID + " = ?",
- new String[]{String.valueOf(inspections.getIdinspection())});
- }
- //Deleting
- public void deleteUsers(Users users){
- SQLiteDatabase db = this.getWritableDatabase();
- db.delete(TABLE_USERS, KEY_USERNAME_ID + " = ?",
- new String[]{String.valueOf(users.getIdusername())});
- db.close();
- }
- public void deleteInspections(Inspections inspection){
- SQLiteDatabase db = this.getWritableDatabase();
- db.delete(TABLE_INSPECTIONS, KEY_INSPECTION_ID + " = ?",
- new String[]{String.valueOf(inspection.getIdinspection())});
- db.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement