Example usage for android.database.sqlite SQLiteDatabase close

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

Introduction

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

Prototype

public void close() 

Source Link

Document

Releases a reference to the object, closing the object if the last reference was released.

Usage

From source file:org.akop.crosswords.Storage.java

public long findByHash(String hash) {
    StorageHelper helper = getHelper();/*from w  ww .j  av a 2  s .  c  o  m*/
    SQLiteDatabase db = helper.getReadableDatabase();
    long id = ID_NOT_FOUND;

    try {
        Cursor cursor = db.query(Puzzle.TABLE, new String[] { Puzzle._ID }, Puzzle.HASH + "=?",
                new String[] { hash }, null, null, null);

        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    id = cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
        }
    } finally {
        db.close();
    }

    return id;
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void insert(T item, int conflictAlgorithm) {
    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {/* w  ww  .j  a v a2  s .  c o  m*/
        db.beginTransaction();
        db.insertWithOnConflict(mTableName, null, itemToRow(item), conflictAlgorithm);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:org.akop.crosswords.Storage.java

public long findBySourceUrl(String sourceUrl) {
    StorageHelper helper = getHelper();// w  ww .  j  ava  2s  .c o m
    SQLiteDatabase db = helper.getReadableDatabase();
    long id = ID_NOT_FOUND;

    try {
        Cursor cursor = db.query(Puzzle.TABLE, new String[] { Puzzle._ID }, Puzzle.SOURCE_URL + " = ?",
                new String[] { sourceUrl }, null, null, null);

        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    id = cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
        }
    } finally {
        db.close();
    }

    return id;
}

From source file:com.clutch.ClutchStats.java

public ArrayList<StatRow> getLogs() {
    SQLiteDatabase db = getReadableDatabase();
    String[] args = {};//from   w  w  w.java 2  s . co m
    ArrayList<StatRow> res = new ArrayList<StatRow>();
    Cursor cur = db.rawQuery("SELECT uuid, ts, action, data FROM stats ORDER BY ts", args);
    cur.moveToFirst();
    while (!cur.isAfterLast()) {
        String uuid = cur.getString(0);
        double ts = cur.getDouble(1);
        String action = cur.getString(2);
        JSONObject data;
        try {
            data = new JSONObject(cur.getString(3));
        } catch (JSONException e) {
            Log.w(TAG, "Could not serialize to JSON: " + cur.getString(3));
            cur.moveToNext();
            continue;
        }
        res.add(new StatRow(uuid, ts, action, data));
        cur.moveToNext();
    }
    db.close();
    return res;
}

From source file:com.maxwen.wallpaper.board.databases.Database.java

public void addCategories(WallpaperJson categories) {
    SQLiteDatabase db = this.getWritableDatabase();
    for (int i = 0; i < categories.getCategories.size(); i++) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, categories.getCategories.get(i).name);
        values.put(KEY_THUMB_URL, categories.getCategories.get(i).thumbUrl);

        db.insert(TABLE_CATEGORIES, null, values);
    }/*from   w  ww  . ja v a2s . c  om*/
    db.close();
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void insert(List<T> items, int conflictAlgorithm) {
    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {//w w w.ja  v  a 2  s. c  o m
        db.beginTransaction();
        for (T item : items)
            db.insertWithOnConflict(mTableName, null, itemToRow(item), conflictAlgorithm);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public ArrayList<ImageMessage> getImageMessages(String chatId) {
    ArrayList<ImageMessage> messages = new ArrayList<>();
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor c = db.query(chatId, new String[] { MessageHistoryContract.MessageEntry._ID },
            MessageHistoryContract.MessageEntry.COLUMN_NAME_MESSAGE_TYPE + "=?",
            new String[] { MessageHistory.TYPE_IMAGE }, null, null, null);
    c.moveToFirst();/*from  w  w w .  ja  v  a  2  s . c  o m*/
    do {
        long messageId = c.getLong(0);
        MessageArrayContent mac = getMessage(chatId, messageId);
        if (mac instanceof ImageMessage)
            messages.add((ImageMessage) mac);
    } while (c.move(1));
    c.close();
    db.close();
    return messages;
}

From source file:com.raspi.chatapp.util.storage.MessageHistory.java

public String getOnline(String buddyId) {
    int index = buddyId.indexOf('@');
    if (index >= 0) {
        buddyId = buddyId.substring(0, index);
    }//from  www . j  av  a  2  s.com
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor c = db.query(MessageHistoryContract.ChatEntry.TABLE_NAME_ALL_CHATS,
            new String[] { MessageHistoryContract.ChatEntry.COLUMN_NAME_LAST_ONLINE },
            MessageHistoryContract.ChatEntry.COLUMN_NAME_BUDDY_ID + "=?", new String[] { buddyId }, null, null,
            null);
    String result = null;
    try {
        c.moveToFirst();
        if (c.getCount() > 0)
            result = c.getString(0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    c.close();
    db.close();
    return result;
}

From source file:com.barcamppenang2014.tabfragment.ProfileFragment.java

License:asdf

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.update_profile_page, container, false);
    profilefrg = this;
    bt_save = (Button) v.findViewById(R.id.btnSave);
    bt_connect = (ImageButton) v.findViewById(R.id.btnfbconnect);
    bt_namecard = (ImageButton) v.findViewById(R.id.bt_namecard);
    et_name = (EditText) v.findViewById(R.id.editTextName);
    et_email = (EditText) v.findViewById(R.id.editTextEmail);
    et_phone = (EditText) v.findViewById(R.id.editTextPhone);
    et_profession = (EditText) v.findViewById(R.id.editTextProfession);
    et_fbId = (EditText) v.findViewById(R.id.editTextFbId);
    tv_imgtext = (TextView) v.findViewById(R.id.tx_namecard);
    tv_remove = (TextView) v.findViewById(R.id.tx_remove);

    final Bitmap defaultNamecard = BitmapFactory.decodeResource(getActivity().getResources(),
            R.drawable.default_namecard);
    //set default image for namecard
    bt_namecard.setImageBitmap(defaultNamecard);
    // Retrive the data from sqlite if profile is created.
    isProfileCreated = check();//from  www.  j a  va 2s  .  c om

    if (isProfileCreated.equalsIgnoreCase("true")) {

        String[] myInfo = fillTextField();
        et_name.setText(myInfo[0]);
        et_email.setText(myInfo[1]);
        et_phone.setText(myInfo[2]);
        et_profession.setText(myInfo[3]);
        if (!myInfo[4].equals("none")) {
            et_fbId.setText(myInfo[4]);
        }
        String picUri = getSharePreferenceKey("picuri");
        // myInfo[5] retrieved from sharedpreference
        if (!picUri.equals("none")) {
            Uri imageUri = Uri.parse(picUri);
            try {
                URIToBitmap(imageUri);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

    bt_connect.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            fbLogin();
        }

    });

    tv_remove.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            SharedPreferences sharedpreferences = getActivity().getSharedPreferences("MyPREFERENCES",
                    Context.MODE_PRIVATE);
            Editor editor = sharedpreferences.edit();
            editor.putString("picuri", "none");
            editor.commit();

            //set default image for namecard
            bt_namecard.setImageBitmap(defaultNamecard);

        }

    });

    bt_namecard.setOnClickListener(galleryOnClickListener);
    tv_imgtext.setOnClickListener(galleryOnClickListener);

    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

    Session session = Session.getActiveSession();
    if (session == null) {

        if (savedInstanceState != null) {

            session = Session.restoreSession(getActivity(), null, statusCallback, savedInstanceState);
        }
        if (session == null) {
            Activity temp = getActivity();
            session = new Session(temp);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {

            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
        }
    }

    bt_save.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            String name = et_name.getText().toString();
            String email = et_email.getText().toString();
            String phone = et_phone.getText().toString();
            String profession = et_profession.getText().toString();
            // fbId and picUri can be null
            String fbId = et_fbId.getText().toString().equals("") ? "none" : et_fbId.getText().toString();
            // String picUri = getSharePreferenceKey("picuri");

            if (name.equals("") || email.equals("") || phone.equals("") || profession.equals("")) {
                Toast.makeText(getActivity(), "Please fill in all the details.", Toast.LENGTH_LONG).show();
            }

            else {
                // URLNamecard =
                // "http://chimou.com/barcamppg/namecard.php?get&email=yinsee@wsatp.com";

                // If bitmap is not null, POST to php               

                String picUri = getSharePreferenceKey("picuri");
                if (!picUri.equals("none")) {

                    // new POSTNamecard().execute(email);
                    //new POSTNamecard().execute("baa@gmail.com");
                }

                MyDatabase database = new MyDatabase(getActivity());
                SQLiteDatabase sqliteDatabase = database.getWritableDatabase();
                String sql = "INSERT OR REPLACE INTO USERPROFILE (ISPFOFILECREATED,MYKEYID, MYNAME, MYEMAIL, MYPHONE,MYPROFESSION, MYFBID )"
                        + " VALUES ('true','" + "1','" + name + "', '" + email + "', '" + phone + "', '"
                        + profession + "', '" + fbId + "'); ";

                sqliteDatabase.execSQL(sql);

                database.close();
                sqliteDatabase.close();

                MainActivity main = (MainActivity) getActivity();
                // main.switchContentWithinTab(new ProfileQrFragment(),
                // TITLE);
                main.switchContent(new ProfileQrFragment());

            }

        }

    });

    return v;

}

From source file:com.maxwen.wallpaper.board.databases.Database.java

@Nullable
public int getWallpapersCountOfCatgegory(String category) {
    List<String> selection = new ArrayList<>();

    StringBuilder CONDITION = new StringBuilder();
    CONDITION.append("LOWER(").append(KEY_CATEGORY).append(")").append(" LIKE ?");
    selection.add("%" + category.toLowerCase(Locale.getDefault()) + "%");

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_WALLPAPERS, null, CONDITION.toString(),
            selection.toArray(new String[selection.size()]), null, null, null, null);
    int numWallpapers = 0;
    if (cursor.moveToFirst()) {
        do {//from w  ww  .j av  a 2  s . co  m
            numWallpapers++;
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return numWallpapers;
}