Example usage for android.database.sqlite SQLiteDatabase openDatabase

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

Introduction

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

Prototype

public static SQLiteDatabase openDatabase(@NonNull String path, @Nullable CursorFactory factory,
        @DatabaseOpenFlags int flags) 

Source Link

Document

Open the database according to the flags #OPEN_READWRITE #OPEN_READONLY #CREATE_IF_NECESSARY and/or #NO_LOCALIZED_COLLATORS .

Usage

From source file:Main.java

private static SQLiteDatabase getDataBase() {
    return SQLiteDatabase.openDatabase(getDBPath(), null, SQLiteDatabase.OPEN_READONLY);
}

From source file:Main.java

public static boolean exists() {
    SQLiteDatabase checkDB = null;/*from  w  w  w .  ja v a  2s.c o m*/
    try {
        checkDB = SQLiteDatabase.openDatabase(CHIRSTMAIS_IS_COMING, null, SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException ex) {
        // database doesn't exist yet.
    }
    return checkDB != null ? true : false;
}

From source file:Main.java

private static boolean openDatabase() {
    String path = DB_PATH + DB_NAME;
    database = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    return database != null;
}

From source file:Main.java

/**
 * Check if the database exist//from   w  w  w . j  a v  a 2 s .c  o m
 *
 * @return true if it exists, false if it doesn't
 */
public static boolean checkDataBase(Context context) {
    SQLiteDatabase checkDB = null;
    int count = 0;
    try {
        if (android.os.Build.VERSION.SDK_INT >= 17) {
            DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
        } else {
            DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
        }
        checkDB = SQLiteDatabase.openDatabase(DB_PATH + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);

        String selectRegistros = "SELECT COUNT(*) FROM Grupo_Gastos";

        Cursor mCursor = checkDB.query(true, "Grupo_Gastos", new String[] { "_id" }, null, null, null, null,
                null, null);
        count = mCursor.getCount();

        checkDB.close();
    } catch (SQLiteException e) {
        // database doesn't exist yet.
        return false;
    }
    return count > 0;
}

From source file:com.awt.supark.CarListFragment.java

@Nullable
@Override// w  ww. j  a  v  a 2  s . c  o m
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_list_layout, container, false);
    listview = (ListView) view.findViewById(R.id.listview);

    // Creating database if it's not exist yet
    db = SQLiteDatabase.openDatabase(getContext().getFilesDir().getPath() + "/carDB.db", null,
            SQLiteDatabase.CREATE_IF_NECESSARY);

    //------------------------ Creating the car list ---------------------------
    Cursor d = db.rawQuery("SELECT * FROM cars", null);

    for (d.moveToFirst(); !d.isAfterLast(); d.moveToNext()) {
        Car car = new Car();

        car.setName(d.getString(d.getColumnIndex("car_name"))); // set name
        car.setLicens(d.getString(d.getColumnIndex("car_license"))); // set license
        car.setSqlid(d.getInt(d.getColumnIndex("car_id")));
        car.setGeneric(d.getInt(d.getColumnIndex("isgeneric")));

        // Retrieves the parking state of the car
        switch (d.getInt(d.getColumnIndex("parkedstate"))) {
        case 0:
            car.setState(getResources().getString(R.string.free));
            break;
        case 1:
            car.setState(getResources().getString(R.string.parked));
            break;
        }
        car.setRemaining(
                (int) (d.getLong(d.getColumnIndex("parkeduntil")) - ((System.currentTimeMillis() / 1000L))));
        carArray.add(car);
    }

    d.close();
    db.close();

    // List adapter
    adapter = new CarListAdapter(getActivity(), carArray);

    // Turning off dividers
    listview.setDivider(null);
    listview.setDividerHeight(0);
    listview.setClipToPadding(false);
    listview.setPadding(0, 10, 0, 100);
    listview.setAdapter(adapter);

    return view;
}

From source file:com.panoskrt.dbadapter.DBAdapter.java

public void copyDB() {
    try {//from w w w .j a v  a 2 s  . co  m
        try {
            database = SQLiteDatabase.openDatabase(dbPath + dbName, null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
            // Checking if required dirs exist as sometimes they do not and
            // they need to be created.
            File dbDir = new File(dbPath);
            if (!dbDir.exists()) {
                dbDir.mkdirs();
            }
            // Copying db from assets to db directory.
            InputStream in = context.getAssets().open(dbName);
            OutputStream out = new FileOutputStream(dbPath + dbName);
            bufCopy(in, out);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    if (database != null) {
        database.close();
    }
}

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

public SQLiteDatabase openDatabase() throws SQLException, IOException {
    if (mDatabase != null) {
        return mDatabase;
    }/*from   w w w .  j av  a2  s.c  o  m*/

    if (!this.hasDatabase()) {
        this.copyDatabase();
    }

    mDatabase = SQLiteDatabase.openDatabase(getDBFile().getPath(), null, SQLiteDatabase.OPEN_READONLY);
    return mDatabase;
}

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

public SQLiteDatabase openDatabase() throws SQLException, IOException {
    if (mDatabase != null) {
        return mDatabase;
    }//from  w ww  .j a  va2s . c  o m

    if (!this.hasDatabase()) {
        this.copyDatabase();
    }

    mDatabase = SQLiteDatabase.openDatabase(new File(DB_PATH, DB_NAME).getPath(), null,
            SQLiteDatabase.OPEN_READONLY);
    return mDatabase;
}

From source file:io.jawg.osmcontributor.utils.core.database.DatabaseUpgradeTest.java

@Test
public void upgrade() throws Exception {
    System.out.println(new File(".").getAbsolutePath());
    OsmSqliteOpenHelper openHelper = new OsmSqliteOpenHelper(Robolectric.application);

    SQLiteDatabase newDb = SQLiteDatabase.openOrCreateDatabase(newDbFile, null);
    openHelper.onCreate(newDb);/*from   w  w  w. j av a 2  s  .c  o m*/

    SQLiteDatabase upgradedDb = SQLiteDatabase.openDatabase(updatedDbFile.getAbsolutePath(), null,
            SQLiteDatabase.OPEN_READWRITE);
    openHelper.onUpgrade(upgradedDb, 1, OsmSqliteOpenHelper.CURRENT_VERSION);

    openHelper.close();

    Class.forName("org.sqlite.JDBC");
    String baseJdbcUrl = "jdbc:sqlite:";

    Set<String> newContents = extractContent(baseJdbcUrl + newDbFile.getAbsolutePath());
    Set<String> updatedContents = extractContent(baseJdbcUrl + updatedDbFile.getAbsolutePath());

    assertThat(updatedContents).containsAll(newContents);
    assertThat(updatedContents).isEqualTo(newContents);

}

From source file:com.awt.supark.EditCar.java

@Nullable
@Override/*from  www.j av  a 2s .  c o m*/
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.edit_car, container, false);

    addCarButton = (Button) view.findViewById(R.id.DoneButton);
    deleteButton = (Button) view.findViewById(R.id.DeleteButton);
    cancelButton = (Button) view.findViewById(R.id.cancelButton);
    carName = (EditText) view.findViewById(R.id.carName);
    carLicense = (EditText) view.findViewById(R.id.carLicense);
    txtCity = (TextView) view.findViewById(R.id.city);
    txtNum = (TextView) view.findViewById(R.id.number);
    radioNewSrb = (RadioButton) view.findViewById(R.id.radioNewSrb);
    radioGeneric = (RadioButton) view.findViewById(R.id.radioGeneric);
    radioLicenseGroup = (RadioGroup) view.findViewById(R.id.radioLicenseGroup);
    licensePlate = (LinearLayout) view.findViewById(R.id.licensePlate);
    licenseNum = "";
    context = getContext();

    // Setting the custom font
    Typeface licenseFont = Typeface.createFromAsset(getContext().getAssets(), "fonts/LicensePlate.ttf");
    txtCity.setTypeface(licenseFont);
    txtNum.setTypeface(licenseFont);

    db = SQLiteDatabase.openDatabase(getContext().getFilesDir().getPath() + "/carDB.db", null,
            SQLiteDatabase.CREATE_IF_NECESSARY);
    final Bundle b = getArguments();

    if (b.getInt("editid") != -1) {
        editid = b.getInt("editid");
        Cursor d = db.rawQuery("SELECT * FROM cars WHERE car_id = " + editid, null);
        d.moveToFirst();

        carName.setText(d.getString(d.getColumnIndex("car_name")));
        licenseNum = d.getString(d.getColumnIndex("car_license"));
        carLicense.setText(licenseNum);

        if (d.getInt(d.getColumnIndex("isgeneric")) == 0) {
            radioNewSrb.setChecked(true);
            radioGeneric.setChecked(false);
            radioListener();
        } else {
            radioGeneric.setChecked(true);
            radioNewSrb.setChecked(false);
            radioListener();
        }

        deleteButton.setVisibility(View.VISIBLE);
        if (isCarParked(editid)) {
            deleteButton.setEnabled(false);
        }
        TextView text = (TextView) view.findViewById(R.id.text1);
        text.setText(context.getResources().getString(R.string.edit_car));
        d.close();
    } else {
        radioNewSrb.setChecked(true);
        radioListener();
    }

    addCarButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editid == -1) {
                addCar(v);
            } else {
                editCar(v);
            }
        }
    });

    deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDeleteQuestionDialog("", getResources().getString(R.string.are_you_sure), v);
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity) context).openCarFragment(null, true);
        }
    });

    // Filters the emojis and other unwanted characters
    filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                int dend) {
            for (int i = start; i < end; i++) {
                if (!Character.isLetterOrDigit(source.charAt(i))) {
                    return "";
                }
            }
            return null;
        }
    };
    carName.setFilters(new InputFilter[] { filter });
    //carLicense.setFilters(new InputFilter[] { filter });

    // License number filler
    carLicense.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            licenseNum = charSequence.toString();
            updateLicensePlate(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    radioLicenseGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            radioListener();
        }
    });

    return view;
}