Example usage for android.database.sqlite SQLiteDatabase OPEN_READONLY

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

Introduction

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

Prototype

int OPEN_READONLY

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

Click Source Link

Document

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

Usage

From source file:com.codebutler.farebot.transit.ovc.OVChipDBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;/*w  ww  .  j  a v  a  2  s  . co  m*/

    File file = new File(DB_PATH, DB_NAME);
    if (!file.exists()) {
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (currentVersion != VERSION) {
            Log.d(TAG, String.format("Updating OVChip database. Old: %s, new: %s", currentVersion, VERSION));
            tempDatabase.close();
            tempDatabase = null;
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:com.codebutler.farebot.core.DBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;//from w  w  w  . j  a  v  a2  s  .  c  o  m

    File file = getDBFile();
    if (!file.exists()) {
        Log.d(TAG, String.format("Database for %s does not exist, will install version %s", getDBName(),
                getDesiredVersion()));
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (allowGreaterDatabaseVersions() ? currentVersion < getDesiredVersion()
                : currentVersion != getDesiredVersion()) {
            Log.d(TAG, String.format("Updating %s database. Old: %s, new: %s", getDBName(), currentVersion,
                    getDesiredVersion()));
            tempDatabase.close();
            tempDatabase = null;
        } else {
            Log.d(TAG, String.format("Not updating %s database. Current: %s, app has: %s", getDBName(),
                    currentVersion, getDesiredVersion()));
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:com.codebutler.farebot.card.felica.DBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;//w  w w. java2s.  co m

    File file = new File(DB_PATH, DB_NAME);
    if (!file.exists()) {
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (currentVersion != VERSION) {
            Log.d(TAG, String.format("Updating Suica database. Old: %s, new: %s", currentVersion, VERSION));
            tempDatabase.close();
            tempDatabase = null;
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:com.example.ryutasakamoto.readfelica.util.DBUtil.java

private boolean hasDatabase() {
    SQLiteDatabase tempDatabase = null;/*from   w  ww  . j  av  a  2s .  c om*/

    File file = getDBFile();
    if (!file.exists()) {
        return false;
    }

    try {
        tempDatabase = SQLiteDatabase.openDatabase(file.getPath(), null, SQLiteDatabase.OPEN_READONLY);
        int currentVersion = tempDatabase.getVersion();
        if (currentVersion != VERSION) {
            Log.d(TAG, String.format("Updating Suica database. Old: %s, new: %s", currentVersion, VERSION));
            tempDatabase.close();
            tempDatabase = null;
        }
    } catch (SQLiteException ignored) {
    }

    if (tempDatabase != null) {
        tempDatabase.close();
    }

    return (tempDatabase != null);
}

From source file:com.esri.squadleader.model.GeoPackageReader.java

/**
 * Reads the tables in a GeoPackage, makes a layer from each table, and returns a list containing
 * those layers.//w  w  w  .  j av a  2 s . c  o  m
 *
 * @param gpkgPath       the full path to the .gpkg file.
 * @param sr             the spatial reference to which any raster layers should be projected, typically the
 *                       spatial reference of your map.
 * @param showVectors    if true, this method will include the GeoPackage's vector layers.
 * @param showRasters    if true, this method will include the GeoPackage's raster layer.
 * @param rasterRenderer the renderer to be used for raster layers. One simple option is an RGBRenderer.
 * @param markerRenderer the renderer to be used for point layers.
 * @param lineRenderer   the renderer to be used for polyline layers.
 * @param fillRenderer   the renderer to be used for polygon layers.
 * @return a list of the layers created for all tables in the GeoPackage.
 * @throws IOException if gpkgPath cannot be read. Possible reasons include the file not
 *                     existing, failure to request READ_EXTERNAL_STORAGE or
 *                     WRITE_EXTERNAL_STORAGE permission, or the GeoPackage containing an
 *                     invalid spatial reference.
 */
public List<Layer> readGeoPackageToLayerList(String gpkgPath, SpatialReference sr, boolean showVectors,
        boolean showRasters, RasterRenderer rasterRenderer, Renderer markerRenderer, Renderer lineRenderer,
        Renderer fillRenderer) throws IOException {
    List<Layer> layers = new ArrayList<Layer>();

    if (showRasters) {
        // Check to see if there are any rasters before loading them
        SQLiteDatabase sqliteDb = null;
        Cursor cursor = null;
        try {
            sqliteDb = SQLiteDatabase.openDatabase(gpkgPath, null, SQLiteDatabase.OPEN_READONLY);
            cursor = sqliteDb.rawQuery("SELECT COUNT(*) FROM gpkg_contents WHERE data_type = ?",
                    new String[] { "tiles" });
            if (cursor.moveToNext()) {
                if (0 < cursor.getInt(0)) {
                    cursor.close();
                    sqliteDb.close();
                    FileRasterSource src = new FileRasterSource(gpkgPath);
                    rasterSources.add(src);
                    if (null != sr) {
                        src.project(sr);
                    }
                    RasterLayer rasterLayer = new RasterLayer(src);
                    rasterLayer.setRenderer(rasterRenderer);
                    rasterLayer
                            .setName((gpkgPath.contains("/") ? gpkgPath.substring(gpkgPath.lastIndexOf("/") + 1)
                                    : gpkgPath) + " (raster)");
                    layers.add(rasterLayer);
                }
            }
        } catch (Throwable t) {
            Log.e(TAG, "Could not read raster(s) from GeoPackage", t);
        } finally {
            if (null != cursor) {
                cursor.close();
            }
            if (null != sqliteDb) {
                sqliteDb.close();
            }
        }
    }

    if (showVectors) {
        Geopackage gpkg;
        try {
            gpkg = new Geopackage(gpkgPath);
        } catch (RuntimeException ex) {
            throw new IOException(null != ex.getMessage() && ex.getMessage().contains("unknown wkt")
                    ? "Geopackage " + gpkgPath + " contains an invalid spatial reference."
                    : null, ex);
        }
        geopackages.add(gpkg);
        List<GeopackageFeatureTable> tables = gpkg.getGeopackageFeatureTables();
        if (0 < tables.size()) {
            //First pass: polygons and unknowns
            HashSet<Geometry.Type> types = new HashSet<Geometry.Type>();
            types.add(Geometry.Type.ENVELOPE);
            types.add(Geometry.Type.POLYGON);
            types.add(Geometry.Type.UNKNOWN);
            layers.addAll(getTablesAsLayers(tables, types, fillRenderer));

            //Second pass: lines
            types.clear();
            types.add(Geometry.Type.LINE);
            types.add(Geometry.Type.POLYLINE);
            layers.addAll(getTablesAsLayers(tables, types, lineRenderer));

            //Third pass: points
            types.clear();
            types.add(Geometry.Type.MULTIPOINT);
            types.add(Geometry.Type.POINT);
            layers.addAll(getTablesAsLayers(tables, types, markerRenderer));
        }
    }

    return layers;
}

From source file:com.filemanager.free.activities.DbViewer.java

private void load(final File file) {
    new Thread(new Runnable() {
        @Override/*from  w  w w  . j  a  v a  2s . co m*/
        public void run() {
            if (!file.canRead() && rootmode) {
                File file1 = getExternalCacheDir();
                if (file1 != null)
                    file1 = getCacheDir();
                RootTools.remount(file.getParent(), "RW");
                assert (file1) != null;
                RootTools.copyFile(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath(),
                        true, false);
                pathFile = new File(file1.getPath(), file.getName());
                RootHelper.runAndWait("chmod 777 " + pathFile.getPath(), true);
                delete = true;
            }
            try {
                sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null,
                        SQLiteDatabase.OPEN_READONLY);

                c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
                arrayList = getDbTableNames(c);
                arrayAdapter = new ArrayAdapter(DbViewer.this, android.R.layout.simple_list_item_1, arrayList);
            } catch (Exception e) {
                e.printStackTrace();
                finish();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    listView.setAdapter(arrayAdapter);
                }
            });
        }
    }).start();
}

From source file:com.igniva.filemanager.activities.DbViewer.java

private void load(final File file) {
    new Thread(new Runnable() {
        @Override//from   w  ww  .j  a va  2 s. co m
        public void run() {

            File file1 = getExternalCacheDir();
            if (file1 == null)
                file1 = getCacheDir();

            if (!file.canRead() && BaseActivity.rootMode) {
                RootTools.copyFile(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath(),
                        true, false);
                pathFile = new File(file1.getPath(), file.getName());
                RootHelper.runAndWait("chmod 777 " + pathFile.getPath(), true);
                RootTools.remount(pathFile.getPath(), "RW");
                delete = true;
            }
            try {
                sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null,
                        SQLiteDatabase.OPEN_READONLY);

                c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
                arrayList = getDbTableNames(c);
                arrayAdapter = new ArrayAdapter(DbViewer.this, android.R.layout.simple_list_item_1, arrayList);
            } catch (Exception e) {
                e.printStackTrace();
                finish();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    listView.setAdapter(arrayAdapter);
                }
            });
        }
    }).start();
}

From source file:com.amaze.carbonfilemanager.activities.DbViewer.java

private void load(final File file) {
    new Thread(new Runnable() {
        @Override//ww  w. ja  v a  2 s.  c  o  m
        public void run() {
            File file1 = getExternalCacheDir();

            // if the db can't be read, and we have root enabled, try reading it by
            // first copying it in cache dir
            if (!file.canRead() && BaseActivity.rootMode) {

                try {
                    RootUtils.copy(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath());
                    pathFile = new File(file1.getPath(), file.getName());
                } catch (RootNotPermittedException e) {
                    e.printStackTrace();
                }
                delete = true;
            }
            try {
                sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null,
                        SQLiteDatabase.OPEN_READONLY);

                c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
                arrayList = getDbTableNames(c);
                arrayAdapter = new ArrayAdapter(DbViewer.this, android.R.layout.simple_list_item_1, arrayList);
            } catch (Exception e) {
                e.printStackTrace();
                finish();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    listView.setAdapter(arrayAdapter);
                }
            });
        }
    }).start();
}

From source file:com.amaze.filemanager.activities.DbViewer.java

private void load(final File file) {
    new Thread(new Runnable() {
        @Override/*from  ww  w.j a va2 s  . c o m*/
        public void run() {
            if (!file.canRead() && rootMode) {
                File file1 = getExternalCacheDir();
                if (file1 != null)
                    file1 = getCacheDir();
                RootTools.remount(file.getParent(), "RW");
                RootTools.copyFile(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath(),
                        true, false);
                pathFile = new File(file1.getPath(), file.getName());
                RootHelper.runAndWait("chmod 777 " + pathFile.getPath(), true);
                delete = true;
            }
            try {
                sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null,
                        SQLiteDatabase.OPEN_READONLY);

                c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
                arrayList = getDbTableNames(c);
                arrayAdapter = new ArrayAdapter(DbViewer.this, android.R.layout.simple_list_item_1, arrayList);
            } catch (Exception e) {
                e.printStackTrace();
                finish();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    listView.setAdapter(arrayAdapter);
                }
            });
        }
    }).start();
}

From source file:com.amaze.filemanager.activities.DatabaseViewerActivity.java

private void load(final File file) {
    new Thread(() -> {
        File file1 = getExternalCacheDir();

        // if the db can't be read, and we have root enabled, try reading it by
        // first copying it in cache dir
        if (!file.canRead() && isRootExplorer()) {

            try {
                RootUtils.copy(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath());
                pathFile = new File(file1.getPath(), file.getName());
            } catch (ShellNotRunningException e) {
                e.printStackTrace();//from w ww .j  a  va 2 s .c o m
            }
            delete = true;
        }
        try {
            sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null,
                    SQLiteDatabase.OPEN_READONLY);

            c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
            arrayList = getDbTableNames(c);
            arrayAdapter = new ArrayAdapter(DatabaseViewerActivity.this, android.R.layout.simple_list_item_1,
                    arrayList);
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }
        runOnUiThread(() -> {
            listView.setAdapter(arrayAdapter);
        });
    }).start();
}