Example usage for android.database.sqlite SQLiteDatabase CREATE_IF_NECESSARY

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

Introduction

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

Prototype

int CREATE_IF_NECESSARY

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

Click Source Link

Document

Open flag: Flag for #openDatabase to create the database file if it does not already exist.

Usage

From source file:Main.java

public static SQLiteDatabase openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) {
    return SQLiteDatabase.openDatabase(path, factory, SQLiteDatabase.CREATE_IF_NECESSARY, null);
}

From source file:curso.android.DAO.RespostaDAO.java

public static void initialize(Context context) {
    // Open a SQLite Database
    Const.db = context.openOrCreateDatabase("crm.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

    createTable(Const.db, "resposta");

}

From source file:curso.android.DAO.PerguntaDAO.java

public static void initialize(Context context) {
    // Open a SQLite Database
    Const.db = context.openOrCreateDatabase("crm.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
    createTable(Const.db, "pergunta");
}

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

@Nullable
@Override// www . j a v a 2  s . com
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.awt.supark.EditCar.java

@Nullable
@Override/*from www  .j a v a  2s. c  om*/
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;
}

From source file:org.liberty.android.fantastischmemopro.DatabaseHelper.java

public static void createEmptyDatabase(String path, String name) throws IOException, SQLException {
    File dbfile = new File(path + "/" + name);
    if (dbfile.exists()) {
        // throw new IOException("DB already exist");
        /* Create a backup and overwrite  it instead poping up an error */
        File backupFile = new File(dbfile.getAbsolutePath().replaceAll(".db$", ".old.db"));
        if (backupFile.exists()) {
            backupFile.delete();/* w  w w  .j  ava 2s  . c om*/
        }
        dbfile.renameTo(backupFile);
    }
    SQLiteDatabase database = SQLiteDatabase.openDatabase(path + "/" + name, null,
            SQLiteDatabase.OPEN_READWRITE | SQLiteDatabase.CREATE_IF_NECESSARY);

    database.execSQL(
            "CREATE TABLE dict_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, question TEXT, answer TEXT, note TEXT, category TEXT)");
    database.execSQL(
            "CREATE TABLE learn_tbl(_id INTEGER PRIMARY KEY ASC AUTOINCREMENT, date_learn, interval, grade INTEGER, easiness REAL, acq_reps INTEGER, ret_reps INTEGER, lapses INTEGER, acq_reps_since_lapse INTEGER, ret_reps_since_lapse INTEGER)");
    database.execSQL("CREATE TABLE control_tbl(ctrl_key TEXT, value TEXT)");

    database.beginTransaction();
    try {
        database.execSQL("DELETE FROM learn_tbl");
        database.execSQL("INSERT INTO learn_tbl(_id) SELECT _id FROM dict_tbl");
        database.execSQL(
                "UPDATE learn_tbl SET date_learn = '2010-01-01', interval = 0, grade = 0, easiness = 2.5, acq_reps = 0, ret_reps  = 0, lapses = 0, acq_reps_since_lapse = 0, ret_reps_since_lapse = 0");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_locale', 'US')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_locale', 'US')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_align', 'center')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_align', 'center')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('question_font_size', '24')");
        database.execSQL("INSERT INTO control_tbl(ctrl_key, value) VALUES('answer_font_size', '24')");
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
        database.close();
    }

}

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

private void loadDatabase() {
    try {//from   www .j  ava2s.c o m
        db = SQLiteDatabase.openDatabase(getApplicationContext().getFilesDir().getPath() + "/carDB.db", null,
                SQLiteDatabase.CREATE_IF_NECESSARY);
        Log.i("Service", "DB loaded successfully...");
    } catch (Exception e) {
        Log.i("Service", "DB read error");
        Log.i("Service", "Exception: " + e.toString());
    }
}

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.  j  a  va 2s  .  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();
    }
}