Example usage for android.database.sqlite SQLiteDatabase OPEN_READWRITE

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

Introduction

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

Prototype

int OPEN_READWRITE

To view the source code for android.database.sqlite SQLiteDatabase OPEN_READWRITE.

Click Source Link

Document

Open flag: Flag for #openDatabase to open the database for reading and writing.

Usage

From source file:com.androzic.location.LocationService.java

private void openDatabase() {
    Androzic application = Androzic.getApplication();
    if (application.dataPath == null) {
        Log.e(TAG, "Data path is null");
        errorMsg = "Data path is null";
        errorTime = System.currentTimeMillis();
        updateNotification();//from   w  w w  . jav a 2  s. c  om
        return;
    }
    File dir = new File(application.dataPath);
    if (!dir.exists() && !dir.mkdirs()) {
        Log.e(TAG, "Failed to create data folder");
        errorMsg = "Failed to create data folder";
        errorTime = System.currentTimeMillis();
        updateNotification();
        return;
    }
    File path = new File(dir, "myTrack.db");
    try {
        trackDB = SQLiteDatabase.openDatabase(path.getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE
                | SQLiteDatabase.CREATE_IF_NECESSARY | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        Cursor cursor = trackDB.rawQuery("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name = 'track'",
                null);
        if (cursor.getCount() == 0) {
            trackDB.execSQL(
                    "CREATE TABLE track (_id INTEGER PRIMARY KEY, latitude REAL, longitude REAL, code INTEGER, elevation REAL, speed REAL, track REAL, accuracy REAL, datetime INTEGER)");
        }
        cursor.close();
    } catch (SQLiteException e) {
        trackDB = null;
        Log.e(TAG, "openDatabase", e);
        errorMsg = "Failed to open DB";
        errorTime = System.currentTimeMillis();
        updateNotification();
    }
}

From source file:com.markuspage.android.atimetracker.Tasks.java

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case ADD_TASK:
        return openNewTaskDialog();
    case EDIT_TASK:
        return openEditTaskDialog();
    case DELETE_TASK:
        return openDeleteTaskDialog();
    case CHANGE_VIEW:
        return openChangeViewDialog();
    case HELP:/*from   ww  w  . ja  va 2s  .  co  m*/
        return openAboutDialog();
    case SUCCESS_DIALOG:
        operationSucceed = new AlertDialog.Builder(Tasks.this).setTitle(R.string.success)
                .setIcon(android.R.drawable.stat_notify_sdcard).setMessage(exportMessage)
                .setPositiveButton(android.R.string.ok, null).create();
        return operationSucceed;
    case ERROR_DIALOG:
        operationFailed = new AlertDialog.Builder(Tasks.this).setTitle(R.string.failure)
                .setIcon(android.R.drawable.stat_notify_sdcard).setMessage(exportMessage)
                .setPositiveButton(android.R.string.ok, null).create();
        return operationFailed;
    case PROGRESS_DIALOG:
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Copying records...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setCancelable(false);
        return progressDialog;
    case MORE:
        return new AlertDialog.Builder(Tasks.this)
                .setItems(R.array.moreMenu, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        DBBackup backup;
                        System.err.println("IN CLICK");
                        switch (which) {
                        case 0: // CHANGE_VIEW:
                            showDialog(CHANGE_VIEW);
                            break;
                        case 1: // EXPORT_VIEW:
                            String fname = export();
                            perform(fname, R.string.export_csv_success, R.string.export_csv_fail);
                            break;
                        case 2: // COPY DB TO SD
                            showDialog(Tasks.PROGRESS_DIALOG);
                            if (new File(dbBackup).exists()) {
                                // Find the database
                                SQLiteDatabase backupDb = SQLiteDatabase.openDatabase(dbBackup, null,
                                        SQLiteDatabase.OPEN_READWRITE);
                                SQLiteDatabase appDb = SQLiteDatabase.openDatabase(dbPath, null,
                                        SQLiteDatabase.OPEN_READONLY);
                                backup = new DBBackup(Tasks.this, progressDialog);
                                backup.execute(appDb, backupDb);
                            } else {
                                InputStream in = null;
                                OutputStream out = null;

                                try {
                                    in = new BufferedInputStream(new FileInputStream(dbPath));
                                    out = new BufferedOutputStream(new FileOutputStream(dbBackup));
                                    for (int c = in.read(); c != -1; c = in.read()) {
                                        out.write(c);
                                    }
                                } catch (Exception ex) {
                                    Logger.getLogger(Tasks.class.getName()).log(Level.SEVERE, null, ex);
                                    exportMessage = ex.getLocalizedMessage();
                                    showDialog(ERROR_DIALOG);
                                } finally {
                                    try {
                                        if (in != null) {
                                            in.close();
                                        }
                                    } catch (IOException ignored) {
                                    }
                                    try {
                                        if (out != null) {
                                            out.close();
                                        }
                                    } catch (IOException ignored) {
                                    }
                                }
                            }
                            break;
                        case 3: // RESTORE FROM BACKUP
                            showDialog(Tasks.PROGRESS_DIALOG);
                            SQLiteDatabase backupDb = SQLiteDatabase.openDatabase(dbBackup, null,
                                    SQLiteDatabase.OPEN_READONLY);
                            SQLiteDatabase appDb = SQLiteDatabase.openDatabase(dbPath, null,
                                    SQLiteDatabase.OPEN_READWRITE);
                            backup = new DBBackup(Tasks.this, progressDialog);
                            backup.execute(backupDb, appDb);
                            break;
                        case 4: // PREFERENCES
                            Intent intent = new Intent(Tasks.this, Settings.class);
                            startActivityForResult(intent, PREFERENCES);
                            break;
                        case 5: // HELP:
                            showDialog(HELP);
                            break;
                        default:
                            break;
                        }
                    }
                }).create();
    }
    return null;
}

From source file:com.uproot.trackme.LocationActivity.java

private void initDatabase() {
    db = openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);

    db.execSQL("CREATE TABLE IF NOT EXISTS " + POINTS_TABLE_NAME
            + " (LATITUDE REAL, LONGITUDE REAL,GMTTIMESTAMP BIGINT, ACCURACY INT);");

    db.close();//from   w  w w .j av  a 2s.  c  o  m

}

From source file:com.uproot.trackme.LocationActivity.java

private void insertDb(Location location) {
    try {// w  w w  . j  a  v  a  2  s  .  co  m
        StringBuffer queryBuf = new StringBuffer();
        double lat = (location.getLatitude()) * PI_BY_180;
        double lng = (location.getLongitude()) * PI_BY_180;
        long timeStamp = System.currentTimeMillis();
        long acc = (long) location.getAccuracy();

        queryBuf.append(
                "INSERT INTO " + POINTS_TABLE_NAME + "(LATITUDE,LONGITUDE,GMTTIMESTAMP,ACCURACY) VALUES (" + lat
                        + "," + lng + "," + timeStamp + "," + acc + ");");

        db = openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);

        db.execSQL(queryBuf.toString());

    } catch (Exception e) {
    } finally {
        if (db.isOpen())
            db.close();
    }
}

From source file:com.uproot.trackme.LocationActivity.java

public void showData() throws IOException {

    SQLiteDatabase db = null;/*from  ww  w  . java  2s .  c  om*/
    Cursor cursor = null;
    db = openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);
    cursor = db.rawQuery("SELECT * " + " FROM " + POINTS_TABLE_NAME + " ORDER BY GMTTIMESTAMP ASC", null);

    StringBuffer str = new StringBuffer(
            "<session id=\"chetan123\" userid=\"" + userid + "\" passkey=\"" + passkey + "\">");

    int latitudeColumnIndex = cursor.getColumnIndexOrThrow("LATITUDE");
    int longitudeColumnIndex = cursor.getColumnIndexOrThrow("LONGITUDE");
    int TSColumnIndex = cursor.getColumnIndexOrThrow("GMTTIMESTAMP");
    int ACCColumnIndex = cursor.getColumnIndexOrThrow("ACCURACY");
    if (cursor.moveToFirst()) {
        do {
            double latitude = cursor.getDouble(latitudeColumnIndex);
            double longitude = cursor.getDouble(longitudeColumnIndex);
            long timestamp = (long) cursor.getDouble(TSColumnIndex);
            long accuracy = (long) cursor.getDouble(ACCColumnIndex);
            str.append("<location latitude=\"");
            str.append(latitude);
            str.append("\" longitude=\"");
            str.append(longitude);
            str.append("\" accuracy=\"");
            str.append(accuracy);
            str.append("\" timestamp=\"");
            str.append(timestamp);
            str.append("\" />");
        } while (cursor.moveToNext());
        str.append("</session>");
        fileContents = str.toString();
    }
    db.close();
}

From source file:com.uproot.trackme.LocationActivity.java

public void clearDB(long ts) {
    String whereClause = "GMTTIMESTAMP < ?";// + ts;
    String[] val = new String[] { "" + ts };
    db = openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);
    int stat = db.delete(POINTS_TABLE_NAME, whereClause, val);
    db.close();//from w  w w. ja  va  2  s.  c om
}

From source file:com.modestmaps.providers.connection.TileBaseHelper.java

public void openDatabase() throws SQLException {
    System.out.println("1. openDatabase()..");
    tileDB = SQLiteDatabase.openDatabase(DBPATH + DBNAME, null, SQLiteDatabase.OPEN_READWRITE);

}

From source file:org.lastmilehealth.collect.android.tasks.BluetoothService.java

private void clearForms() {
    SQLiteDatabase db = SQLiteDatabase.openDatabase(Collect.FORMS_DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
    db.execSQL("delete from forms");
    db.close();//w  w  w.j  av a 2s .  c om

    deletePreferences();
    try {
        org.apache.commons.io.FileUtils.cleanDirectory(new File(Collect.FORMS_PATH));
        org.apache.commons.io.FileUtils.cleanDirectory(new File(Collect.ROLES_PATH));
    } catch (Exception e) {
    }
}