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:com.rener.sea.DBHelper.java

public boolean isEmpty() {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_ID }, null, null, null, null,
            null, null);//w  ww. java 2  s  .c  om
    boolean flag = cursor != null ? (cursor.getCount() > 0) : false;
    db.close();
    cursor.close();
    // TODO: tes it
    // return flag;
    return flag || !dummyDB;

}

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

public ChatEntry[] getChats() {
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    Cursor chats = db.query(MessageHistoryContract.ChatEntry.TABLE_NAME_ALL_CHATS,
            new String[] { MessageHistoryContract.ChatEntry.COLUMN_NAME_BUDDY_ID,
                    MessageHistoryContract.ChatEntry.COLUMN_NAME_NAME },
            null, null, null, null, MessageHistoryContract.ChatEntry.COLUMN_NAME_NAME);
    int chatCount = chats.getCount();
    ChatEntry[] resultChats = new ChatEntry[chatCount];
    int i = 0;/*from w w w  .  j  a va 2  s . c o m*/
    chats.moveToFirst();
    if (chats.getCount() > 0)
        do {
            String buddyId = chats.getString(0);
            String name = chats.getString(1);
            MessageArrayContent mac = getLastMessage(buddyId);

            if (mac instanceof TextMessage) {
                TextMessage msg = (TextMessage) mac;

                String lastMessageDate;
                Date msgTime = new Date(msg.time);
                Calendar startOfDay = Calendar.getInstance();
                startOfDay.set(Calendar.HOUR_OF_DAY, 0);
                startOfDay.set(Calendar.MINUTE, 0);
                startOfDay.set(Calendar.SECOND, 0);
                startOfDay.set(Calendar.MILLISECOND, 0);
                long diff = startOfDay.getTimeInMillis() - msgTime.getTime();
                if (diff <= 0)
                    lastMessageDate = String.format(context.getResources().getString(R.string.time), msgTime);
                else if (diff > 1000 * 60 * 60 * 24)
                    lastMessageDate = String.format(context.getResources().getString(R.string.date), msgTime);
                else
                    lastMessageDate = context.getResources().getString(R.string.last_message_yesterday);

                resultChats[i] = new ChatEntry(buddyId, name, MessageHistory.TYPE_TEXT, msg.status,
                        lastMessageDate, ((msg.left) ? name + ": " : "") + msg.message, !msg.left);
            } else if (mac instanceof ImageMessage) {
                ImageMessage msg = (ImageMessage) mac;

                String lastMessageDate;
                Date msgTime = new Date(msg.time);
                Calendar startOfDay = Calendar.getInstance();
                startOfDay.set(Calendar.HOUR_OF_DAY, 0);
                startOfDay.set(Calendar.MINUTE, 0);
                startOfDay.set(Calendar.SECOND, 0);
                startOfDay.set(Calendar.MILLISECOND, 0);
                long diff = startOfDay.getTimeInMillis() - msgTime.getTime();
                if (diff <= 0)
                    lastMessageDate = String.format(context.getResources().getString(R.string.time), msgTime);
                else if (diff > 1000 * 60 * 60 * 24)
                    lastMessageDate = String.format(context.getResources().getString(R.string.date), msgTime);
                else
                    lastMessageDate = context.getResources().getString(R.string.last_message_yesterday);
                msg.description += "".equals(msg.description) ? context.getResources().getString(R.string.image)
                        : "";

                resultChats[i] = new ChatEntry(buddyId, name, MessageHistory.TYPE_IMAGE, msg.status,
                        lastMessageDate, msg.description, !msg.left);
            }
            i++;
        } while (chats.move(1));
    chats.close();
    db.close();
    return resultChats;
}

From source file:com.odoo.orm.OModel.java

/**
 * Select./*from   w  w w. ja v a2 s . c om*/
 * 
 * @param where
 *            the where
 * @param whereArgs
 *            the where args
 * @param groupBy
 *            the group by
 * @param having
 *            the having
 * @param orderBy
 *            the order by
 * @return the list
 */
public List<ODataRow> select(String where, Object[] whereArgs, String groupBy, String having, String orderBy) {
    List<ODataRow> records = new ArrayList<ODataRow>();
    SQLiteDatabase db = getReadableDatabase();
    String limit = null;
    if (mLimit > 0) {
        limit = mOffset + ", " + mLimit;
    }
    Cursor cr = db.query(getTableName(), new String[] { "*" }, getWhereClause(where),
            getWhereArgs(where, whereArgs), groupBy, having, orderBy, limit);
    if (cr.moveToFirst()) {
        do {
            ODataRow row = new ODataRow();
            for (OColumn col : getColumns()) {
                if (col.getRelationType() == null) {
                    row.put(col.getName(), createRecordRow(col, cr));
                } else {
                    switch (col.getRelationType()) {
                    case ManyToMany:
                        row.put(col.getName(),
                                new OM2MRecord(this, col, cr.getInt(cr.getColumnIndex(OColumn.ROW_ID))));
                        break;
                    case OneToMany:
                        row.put(col.getName(),
                                new OO2MRecord(this, col, cr.getInt(cr.getColumnIndex(OColumn.ROW_ID))));
                        break;
                    case ManyToOne:
                        row.put(col.getName(),
                                new OM2ORecord(this, col, cr.getInt(cr.getColumnIndex(col.getName()))));
                        break;
                    }
                }
            }
            /*
             * Adding functional column values to record values if not
             * syncing
             */
            if (!mSyncingData) {
                for (OColumn col : mFunctionalColumns) {
                    if (!col.canFunctionalStore()) {
                        row.put(col.getName(), getFunctionalMethodValue(col, row));
                    }
                }
            }
            if (row.getInt("id") == 0 || row.getString("id").equals("false"))
                row.put("id", 0);
            records.add(row);
        } while (cr.moveToNext());
    }
    cr.close();
    db.close();
    return records;
}

From source file:com.rener.sea.DBHelper.java

public long createUser(String userName, String passwd, int pID, String salt) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(DBSchema.USER_USERNAME, userName);
    values.put(DBSchema.USER_PASSHASH, passwd);
    values.put(DBSchema.USER_PERSON_ID, pID);
    values.put(DBSchema.USER_SALT, salt);

    long id = db.insert(DBSchema.TABLE_USERS, null, values);
    db.close();
    return id;// if -1 error during insertion
}

From source file:com.rener.sea.DBHelper.java

public long getUserId(String username) {
    long agentId = -1;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_ID },
            DBSchema.USER_USERNAME + "=?", new String[] { String.valueOf(username) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();//  www  .  j  av a  2s .  c  o m
        agentId = cursor.getLong(0);
        db.close();
        cursor.close();
    }
    return agentId;
}

From source file:com.rener.sea.DBHelper.java

public boolean isAgent(String username) {
    boolean agent = false;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_USERS, new String[] { DBSchema.USER_TYPE },
            DBSchema.USER_USERNAME + "=?", new String[] { String.valueOf(username) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();/* w ww  .j  av  a 2s  .c o  m*/
        agent = cursor.getString(0).equals("agent");
        db.close();
        cursor.close();
    }
    return agent;
}

From source file:com.rener.sea.DBHelper.java

public Person findPersonById(long id) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_PERSON, new String[] { DBSchema.PERSON_ID, },
            DBSchema.PERSON_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();//from  w w  w .j  a v a2  s .  com
        Person person = new Person(cursor.getLong(0), this);
        db.close();
        cursor.close();

        return person;
    }
    return new Person(-1, this);
}

From source file:com.rener.sea.DBHelper.java

public Location findLocationById(long id) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(DBSchema.TABLE_LOCATION, new String[] { DBSchema.LOCATION_ID },
            DBSchema.LOCATION_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
    if ((cursor != null) && (cursor.getCount() > 0)) {
        cursor.moveToFirst();/*from  w  w w.  j  a v a  2  s. co  m*/
        Location location = new Location(cursor.getLong(0), this);
        db.close();
        cursor.close();
        return location;
    }
    return new Location(-1, this);
}

From source file:com.rener.sea.DBHelper.java

private long deleteOption(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//from   ww w.jav a  2s.c  o  m
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            db.delete(DBSchema.TABLE_OPTION, DBSchema.OPTION_ID + "=?",
                    new String[] { String.valueOf(item.getLong(DBSchema.OPTION_ID)) });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}

From source file:com.rener.sea.DBHelper.java

private long deleteItem(JSONArray data) {
    SQLiteDatabase db = this.getWritableDatabase();
    int i = -1;//  ww w .  j ava2s  .  c o m
    try {
        for (i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            long id = db.delete(DBSchema.TABLE_ITEM, DBSchema.ITEM_ID + "=?",
                    new String[] { String.valueOf(item.getLong(DBSchema.ITEM_ID)) });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    db.close();
    return i;
}