Example usage for android.database.sqlite SQLiteDatabase isOpen

List of usage examples for android.database.sqlite SQLiteDatabase isOpen

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase isOpen.

Prototype

public boolean isOpen() 

Source Link

Document

Returns true if the database is currently open.

Usage

From source file:Main.java

public static void close(SQLiteDatabase db) {
    if (db != null && db.isOpen()) {
        db.close();
    }
}

From source file:org.cgiar.ilri.odk.pull.backend.DataHandler.java

/**
 * Deletes data from the local database for the specified form
 *
 * @param context   Context e.g activity calling this method
 * @param formName  The name of the form data is being deleted for
 *
 * @return  TRUE if data successfully deleted
 *//*from  w  ww.  ja  v a2 s. c o m*/
public static boolean deleteFormData(Context context, String formName) {
    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    SQLiteDatabase writableDB = databaseHelper.getWritableDatabase();
    if (writableDB.isOpen()) {
        databaseHelper.runDeleteQuery(writableDB, DatabaseHelper.TABLE_FORM, "name", new String[] { formName });
        Log.i(TAG, "Deleted data on " + formName);
        return true;
    } else {
        Log.e(TAG, "Was unable to open a writable database to read form preferences");
    }

    return false;
}

From source file:org.cgiar.ilri.odk.pull.backend.DataHandler.java

/**
 * Gets data on the specified form from the local database
 *
 * @param context   Context e.g activity calling this method
 * @param formName  The name of the form preferences are being gotten for
 *
 * @return  {@link org.cgiar.ilri.odk.pull.backend.carriers.Form} containing the form data
 *//*from  w ww  .j  a  v  a2 s. c  o m*/
public static Form getForm(Context context, String formName) {
    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    SQLiteDatabase readableDB = databaseHelper.getReadableDatabase();

    if (readableDB.isOpen()) {
        if (formName != null) {
            String query = "name=?";
            String[] columns = new String[] { "name", "pref_pull_freq", "pref_pull_internet_on",
                    "pref_pull_odk_launches", "last_time_fetched" };
            String[][] results = databaseHelper.runSelectQuery(readableDB, DatabaseHelper.TABLE_FORM, columns,
                    query, new String[] { formName }, null, null, null, null);

            if (results.length == 1) {
                boolean pullWhenInternetConnection = false;
                if (results[0][2].equals("1"))
                    pullWhenInternetConnection = true;

                boolean pullWhenODKLaunched = false;
                if (results[0][3].equals("1"))
                    pullWhenODKLaunched = true;

                Form form = new Form(results[0][0], results[0][1], pullWhenInternetConnection,
                        pullWhenODKLaunched);
                form.setLastPull(results[0][4]);

                return form;
            } else if (results.length == 0) {
                Log.w(TAG, "No form with name " + formName + " found in database");
            } else {
                Log.e(TAG, "More than one form with the name " + formName
                        + " found in the database. Unable to fetch preferences for any");
            }
        } else {
            Log.e(TAG, "provided form name is null. Unable to get data on the form");
        }
    } else {
        Log.e(TAG, "Was unable to open a readable database to read form preferences");
    }
    return null;
}

From source file:org.cgiar.ilri.odk.pull.backend.DataHandler.java

/**
 * Saves preferences for the specified {@link org.cgiar.ilri.odk.pull.backend.carriers.Form}
 *
 * @param context   Context e.g activity calling this method
 * @param form      {@link org.cgiar.ilri.odk.pull.backend.carriers.Form} containing the preferences
 *
 * @return  TRUE if able to save the preferences
 *//*  w  ww .ja v a2  s .co m*/
public static boolean saveFormPreferences(Context context, Form form) {
    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    SQLiteDatabase writableDB = databaseHelper.getWritableDatabase();

    if (writableDB.isOpen()) {
        int pullInternetOnVal = 0;
        if (form.isPullWhenInternetOn())
            pullInternetOnVal = 1;

        int pullODKLaunchesVal = 0;
        if (form.isPullWhenODKLaunched())
            pullODKLaunchesVal = 1;

        String query = "name=?";
        String[] columns = new String[] { "id" };

        String[][] results = databaseHelper.runSelectQuery(writableDB, DatabaseHelper.TABLE_FORM, columns,
                query, new String[] { form.getName() }, null, null, null, null);

        if (results.length == 1) {
            databaseHelper.runDeleteQuery(writableDB, DatabaseHelper.TABLE_FORM, "id",
                    new String[] { String.valueOf(results[0][0]) });
        } else if (results.length > 1) {
            Log.e(TAG, "It appears like there exists more than one form in the database with the name " + form
                    + ". Unable to update preference for any of them");
            return false;
        }

        //(id INTEGER PRIMARY KEY, name TEXT, pref_sync_freq TEXT, pref_pull_internet_on TEXT, pref_pull_odk_launches TEXT, last_time_fetched INT);");
        columns = new String[] { "name", "pref_pull_freq", "pref_pull_internet_on", "pref_pull_odk_launches",
                "last_time_fetched" };
        String[] values = new String[columns.length];

        Log.d(TAG, "New value for form name :" + form.getName());
        Log.d(TAG, "New value for form pull freq :" + form.getPullFrequency());
        Log.d(TAG, "New value for form pullwheninternetavailable :" + String.valueOf(pullInternetOnVal));
        Log.d(TAG, "New value for form pullwhenodklaunched :" + String.valueOf(pullODKLaunchesVal));

        values[0] = form.getName();
        values[1] = form.getPullFrequency();
        values[2] = String.valueOf(pullInternetOnVal);
        values[3] = String.valueOf(pullODKLaunchesVal);
        values[4] = String.valueOf(form.getLastPull());

        databaseHelper.runInsertQuery(DatabaseHelper.TABLE_FORM, columns, values, -1, writableDB);
        return true;
    } else {
        Log.e(TAG, "Was unable to open the writable database ");
    }

    return false;
}

From source file:org.cgiar.ilri.odk.pull.backend.DataHandler.java

/**
 * Gets a list of all the forms registered in the local database. If a form is registered, its data
 * is being pulled from the server.//from www . ja va2s  . co m
 *
 * @param context Context e.g activity calling this method
 *
 * @return  A {@link java.util.List} of {@link java.lang.String} with the form names register in the local database
 */
public static List<Form> getAllForms(Context context) {
    DatabaseHelper databaseHelper = new DatabaseHelper(context);
    SQLiteDatabase readableDB = databaseHelper.getReadableDatabase();
    List<Form> allForms = new ArrayList<Form>();
    if (readableDB.isOpen()) {
        //get all the form names
        String[] columns = new String[] { "name" };
        String[][] results = databaseHelper.runSelectQuery(readableDB, DatabaseHelper.TABLE_FORM, columns, null,
                null, null, null, null, null);

        if (results.length > 0) {
            for (int formIndex = 0; formIndex < results.length; formIndex++) {
                Form currForm = getForm(context, results[formIndex][0]);
                allForms.add(currForm);
            }
        } else {
            Log.w(TAG, "No forms in database");
        }
    } else {
        Log.e(TAG, "Was unable to open a readable database to read form preferences");
    }

    return allForms;
}

From source file:com.example.sergey.currentweather.ui.activity.MainActivity.java

public boolean doesTableExist(SQLiteDatabase db, String tableName) {
    if (tableName == null || db == null || !db.isOpen()) {
        return false;
    }/*w ww  . ja v  a 2 s  .c  om*/
    Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + tableName, null);
    if (!cursor.moveToFirst()) {
        return false;
    }
    int count = cursor.getInt(0);
    cursor.close();
    return count > 0;
}

From source file:io.vit.vitio.Managers.ConnectDatabase.java

public List<Course> getCoursesList() {
    List<Course> courses = new ArrayList<>();
    String selectQuery = "SELECT  * FROM " + TABLE_COURSES;
    try {/* ww  w.  j  a  v  a2 s  . c  o  m*/
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Course course = new Course();
                course.setCLASS_NUMBER(cursor.getString(0));
                course.setCOURSE_TITLE(cursor.getString(1));
                course.setCOURSE_SLOT(cursor.getString(2));
                course.setCOURSE_TYPE(cursor.getString(3));
                course.setCOURSE_TYPE_SHORT(cursor.getString(4));
                course.setCOURSE_LTPC(cursor.getString(5));
                course.setCOURSE_CODE(cursor.getString(6));
                course.setCOURSE_MODE(cursor.getString(7));
                course.setCOURSE_OPTION(cursor.getString(8));
                course.setCOURSE_VENUE(cursor.getString(9));
                course.setCOURSE_FACULTY(cursor.getString(10));
                course.setCOURSE_REGISTRATIONSTATUS(cursor.getString(11));
                course.setCOURSE_BILL_DATE(cursor.getString(12));
                course.setCOURSE_BILL_NUMBER(cursor.getString(13));
                course.setCOURSE_PROJECT_TITLE(cursor.getString(14));
                course.setJson(new JSONObject(cursor.getString(15)));
                course.setCOURSE_ATTENDANCE(ParseCourses.getAttendance(new JSONObject(cursor.getString(16))));
                course.setCOURSE_TIMING(ParseCourses.getTimings(new JSONArray(cursor.getString(17))));
                course.setCOURSE_MARKS(ParseCourses.getCouseMarks(new JSONObject(cursor.getString(18))));
                courses.add(course);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
    } catch (Exception e) {
        e.printStackTrace();
        SQLiteDatabase _db = this.getWritableDatabase();
        if (_db != null && _db.isOpen()) {
            _db.close();
        }
    }
    return courses;
}

From source file:io.vit.vitio.Managers.ConnectDatabase.java

public Course getCourse(String classNmbr) {
    Course course = new Course();
    try {/*from  www .  j av  a 2 s .co m*/
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.query(TABLE_COURSES, COLUMNS, KEY_CLASNBR + "=?",
                new String[] { String.valueOf(classNmbr) }, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        course.setCLASS_NUMBER(cursor.getString(0));
        course.setCOURSE_TITLE(cursor.getString(1));
        course.setCOURSE_SLOT(cursor.getString(2));
        course.setCOURSE_TYPE(cursor.getString(3));
        course.setCOURSE_TYPE_SHORT(cursor.getString(4));
        course.setCOURSE_LTPC(cursor.getString(5));
        course.setCOURSE_CODE(cursor.getString(6));
        course.setCOURSE_MODE(cursor.getString(7));
        course.setCOURSE_OPTION(cursor.getString(8));
        course.setCOURSE_VENUE(cursor.getString(9));
        course.setCOURSE_FACULTY(cursor.getString(10));
        course.setCOURSE_REGISTRATIONSTATUS(cursor.getString(11));
        course.setCOURSE_BILL_DATE(cursor.getString(12));
        course.setCOURSE_BILL_NUMBER(cursor.getString(13));
        course.setCOURSE_PROJECT_TITLE(cursor.getString(14));
        course.setJson(new JSONObject(cursor.getString(15)));
        course.setCOURSE_ATTENDANCE(ParseCourses.getAttendance(new JSONObject(cursor.getString(16))));
        course.setCOURSE_TIMING(ParseCourses.getTimings(new JSONArray(cursor.getString(17))));
        course.setCOURSE_MARKS(ParseCourses.getCouseMarks(new JSONObject(cursor.getString(18))));

        cursor.close();
        db.close();

    } catch (Exception e) {
        e.printStackTrace();
        SQLiteDatabase _db = this.getWritableDatabase();
        if (_db != null && _db.isOpen()) {
            _db.close();
        }
    }

    return course;
}

From source file:io.vit.vitio.Managers.ConnectDatabase.java

public void saveCourses(List<Course> courses) {
    try {//www  . j a v  a2 s  .  c o m
        SQLiteDatabase db = this.getWritableDatabase();

        for (int i = 0; i < courses.size(); i++) {
            Course course = courses.get(i);

            ContentValues values = new ContentValues();

            values.put(COLUMNS[0], course.getCLASS_NUMBER());
            values.put(COLUMNS[1], course.getCOURSE_TITLE());
            values.put(COLUMNS[2], course.getCOURSE_SLOT());
            values.put(COLUMNS[3], course.getCOURSE_TYPE());
            values.put(COLUMNS[4], course.getCOURSE_TYPE_SHORT());
            Log.d("type", course.getCOURSE_TYPE_SHORT());
            values.put(COLUMNS[5], course.getCOURSE_LTPC().toString());
            values.put(COLUMNS[6], course.getCOURSE_CODE());
            values.put(COLUMNS[7], course.getCOURSE_MODE());
            values.put(COLUMNS[8], course.getCOURSE_OPTION());
            values.put(COLUMNS[9], course.getCOURSE_VENUE());
            values.put(COLUMNS[10], course.getCOURSE_FACULTY().toString());
            values.put(COLUMNS[11], course.getCOURSE_REGISTRATIONSTATUS());
            values.put(COLUMNS[12], course.getCOURSE_BILL_DATE());
            values.put(COLUMNS[13], course.getCOURSE_BILL_NUMBER());
            values.put(COLUMNS[14], course.getCOURSE_PROJECT_TITLE());
            values.put(COLUMNS[15], course.getCOURSE_JSON().toString());
            values.put(COLUMNS[16], course.getCOURSE_ATTENDANCE().getJson().toString());
            values.put(COLUMNS[17], course.getCOURSE_JSON().getJSONArray("timings").toString());
            values.put(COLUMNS[18], course.getCOURSE_JSON().getJSONObject("marks").toString());
            //db.insertWithOnConflict(TABLE_COURSES, null, values, SQLiteDatabase.CONFLICT_REPLACE);
            if (check()) {
                Log.d("update", "check()");
                //onUpgrade(db,db.getVersion(),192564);
                db.replace(TABLE_COURSES, null, values);
                //db.update(TABLE_COURSES, values, null, null);
            } else {
                Log.d("insert", "check()");
                db.insert(TABLE_COURSES, null, values);
            }
        }
        db.close();
    } catch (Exception e) {
        e.printStackTrace();
        SQLiteDatabase _db = this.getWritableDatabase();
        if (_db != null && _db.isOpen()) {
            _db.close();
        }
    }

}

From source file:com.alley.android.ppi.app.DetailFragment.java

private void delete() {
    SQLiteDatabase db = new PropertyDbHelper(getActivity()).getWritableDatabase();
    Log.i(LOG_TAG, "database is open - " + db.isOpen());

    int i = db.delete(PropertyContract.PropertyEntry.TABLE_NAME,
            PropertyContract.PropertyEntry.COLUMN_ADDRESS + " like ?", new String[] { "336%" });
    Log.i(LOG_TAG, "deleted - " + i + " records");
}