SQLiteDatabase rawQuery - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for SQLiteDatabase rawQuery method.

This code example shows how to use the following methods:delete, execSQL, rawQuery
 
    @Test(expected = Exception.class) 
    public void testExecSQLInsertNullShouldBeException() throws Exception { 
        //this inserts null in android, but it when it happens it is likely an error.  H2 throws an exception.  So we'll make Robolectric expect an Exception so that the error can be found. 
 
        database.delete("exectable", null, null);
 
        Cursor cursor = database.rawQuery("select * from exectable", null);
        cursor.moveToFirst();
        assertThat(cursor.getCount()).isEqualTo(0);
 
        database.execSQL("insert into exectable (first_column) values (?);", new String[]{});
        Cursor cursor2 = database.rawQuery("select * from exectable", new String[]{null});
        cursor.moveToFirst();
        assertThat(cursor2.getCount()).isEqualTo(1);
 
    } 
 
    @Test 
    public void testExecSQLAutoIncrementSQLite() throws Exception { 
9
CodeRank
This code example shows how to use the following methods:execSQL, rawQuery
        } 
    } 
 
    /* Convert database from AnyMemo < 9.0 */ 
    private void convertOldDatabase(SQLiteDatabase database) {
        Cursor res = database.rawQuery("select name from sqlite_master where type = 'table' and name = 'dict_tbl'", null);
        boolean isOldDatabase = res.getCount() > 0;
        res.close();
        // This is old database 
        if (isOldDatabase) {
            // copy all cards 
            database.execSQL("insert into cards (ordinal, question, answer, note)" +
                    " select _id as ordinal, question, answer, note from dict_tbl"); 
 
 
            // Make sure the count matches in old database; 
            int count_dict = 0, count_learn = 0;
            Cursor result = database.rawQuery("SELECT _id FROM dict_tbl", null);
            count_dict = result.getCount();
            result.close();

5
CodeRank
This code example shows how to use the following methods:getPath, rawQuery
             } 
        } 
 
        @Override 
        public void onOpen(SQLiteDatabase db) {
            debug("Opening tabs.db: " + db.getPath());
 
            Cursor cursor = null;
            try { 
                cursor = db.rawQuery("PRAGMA synchronous=OFF", null);
            } finally { 
                if (cursor != null)
                    cursor.close();
            } 
 
            // From Honeycomb on, it's possible to run several db 
            // commands in parallel using multiple connections. 
            if (Build.VERSION.SDK_INT >= 11) {
                db.enableWriteAheadLogging();
                db.setLockingEnabled(false);

7
CodeRank
This code example shows how to use the following methods:close, execSQL, rawQuery
	public static final String CREATE_BLACKLIST = "CREATE TABLE IF NOT EXISTS DROIDSNIFF_BLACKLIST "
			+ "(id integer primary key autoincrement, " + "domain varchar(100));"; 
 
	public static void initDB(Context c) {
		DBHelper.droidsniffDB = c.openOrCreateDatabase(DROIDSNIFF_DBNAME, Context.MODE_PRIVATE, null);
		droidsniffDB.execSQL(CREATE_PREFERENCES);
		droidsniffDB.execSQL(CREATE_BLACKLIST);
	} 
 
	public static boolean getGeneric(Context c) {
		initDB(c);
		Cursor cur = droidsniffDB.rawQuery("SELECT * FROM DROIDSNIFF_PREFERENCES WHERE name = 'generic';", new String[] {});
		if (cur.moveToNext()) {
			String s = cur.getString(cur.getColumnIndex("value"));
			cur.close();
			droidsniffDB.close();
			return Boolean.parseBoolean(s);
		} else { 
			cur.close();
			droidsniffDB.close();

6
CodeRank
This code example shows how to use the following methods:execSQL, rawQuery
        assertEquals(mGoodDb.rawQuery("SELECT _id FROM Events;", null).getCount(), 2);
    } 
 
    private void addVersion67Events() { 
        // April 5th 1:01:01 AM to April 6th 1:01:01 
        mBadDb.execSQL("INSERT INTO Events (_id,dtstart,dtend,duration,dtstart2,dtend2," +
                "eventTimezone,eventTimezone2,allDay,calendar_id) " + 
                "VALUES (1,1270454471000,1270540872000,'P10S'," + 
                "1270454460000,1270540861000,'America/Los_Angeles','America/Los_Angeles',1,1);"); 
 
        // April 5th midnight to April 6th midnight, duration cleared 
        mGoodDb.execSQL("INSERT INTO Events (_id,dtstart,dtend,duration,dtstart2,dtend2," +
                "eventTimezone,eventTimezone2,allDay,calendar_id) " + 
                "VALUES (1,1270425600000,1270512000000,null," + 
                "1270450800000,1270537200000,'UTC','America/Los_Angeles',1,1);"); 
 
        // April 5th 1:01:01 AM to April 6th 1:01:01, recurring weekly (We only check for the 
        // existence of an rrule so it doesn't matter if the day is correct) 
        mBadDb.execSQL("INSERT INTO Events (_id,dtstart,dtend,duration,dtstart2,dtend2," +
                "eventTimezone,eventTimezone2,allDay,rrule,calendar_id) " + 

7
CodeRank
This code example shows how to use the following methods:rawQuery
	} 
 
	private static String getTypeString(final SQLiteDatabase db, final String table, final String column) {
 
		final String sql = "SELECT typeof(" + column + ") FROM " + table;
		final Cursor cur = db.rawQuery(sql, null);
		if (cur == null) return null;
		cur.moveToFirst();
		try { 
			return cur.getString(0);
		} finally { 
			cur.close();
		} 
	} 
 
	private static boolean isTypeCompatible(final String old_type, final String new_type,
			final boolean treat_null_as_compatible) {
		if (old_type != null && new_type != null) {
			final int old_idx = old_type.contains("(") ? old_type.indexOf("(") : old_type.indexOf(" ");
			final int new_idx = new_type.contains("(") ? new_type.indexOf("(") : new_type.indexOf(" ");
6
CodeRank
This code example shows how to use the following methods:close, isOpen, rawQuery, setLocale, setVersion
    	lastCursor=myDataBase.rawQuery(sql, selectionArgs);
    	return lastCursor;
    } 
     
    private void checkDBOpen() { 
    	if(myDataBase!=null && myDataBase.isOpen()) myDataBase.close();
    	if(lastCursor!=null && !lastCursor.isClosed()) lastCursor.close();
	} 
 
	public void execSQL(String sql){
		checkDBOpen(); 
    	openDataBase(); 
    	myDataBase.execSQL(sql);
    	close(); 
    } 
    /* 
    public SQLiteDatabase Instance(){ 
    	return myDataBase; 
    } 
    */ 
6
CodeRank
This code example shows how to use the following methods:close, delete, insert, rawQuery
	public void loginstat() 
	{	  Calendar currentDate = Calendar.getInstance();
	SimpleDateFormat formatter= 
			new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
	String dateNow = formatter.format(currentDate.getTime());
	c=sqdb.rawQuery("select count(*) from loginstat",null);
	c.moveToFirst();
	int maxid=c.getInt(0);
	c.close();
	if(maxid==5)
	{ 
		c=sqdb.rawQuery("Select time from loginstat order by time",null);	
		c.moveToFirst();
		String temp=c.getString(0);
		c.close();
		sqdb.delete("loginstat","time" + "=?",new String[] {temp} );
		workhorse.put("time",dateNow);
		sqdb.insert("loginstat",null,workhorse);
		workhorse.clear();
		c.close();

6
CodeRank
This code example shows how to use the following methods:insert, rawQuery, update
            cvs.put("bg_color", background);
            cvs.put("marked_text_color", markedText);
            cvs.put("marked_bg_color", markedBackground);
            Long id = this.id;
            if (id != null) {
                db.update("styles", cvs, "_id = ?", new String[] { id.toString() });
            } else { 
                id = db.insert("styles", null, cvs);
            } 
            Cursor cursor = db.rawQuery("SELECT * FROM styles WHERE _id = ?",
                    new String[] { id.toString() });
            try { 
                if (cursor.moveToFirst()) {
                    return new Style(cursor);
                } else { 
                    return null; 
                } 
            } finally { 
                cursor.close();
            } 
6
CodeRank
This code example shows how to use the following methods:isOpen, rawQuery
    public LoggerDbAdapter(Context ctx) {
        this.mCtx = ctx;
    } 
 
    public LoggerDbAdapter open() throws SQLException { 
        if (mDb != null && mDb.isOpen())
            return this;
        if (mDbHelper == null)
            mDbHelper = new DatabaseHelper(mCtx);
        try { 
            if (mDb == null)
                mDb = mDbHelper.getWritableDatabase();
        } catch (IllegalStateException ise) {
            Logger.LogError("Couldn't open logger", ise);
        } 
        return this;
    } 
 
    public void close() { 
        if (mDb != null && mDb.isOpen())

5
CodeRank

Related SQLiteDatabase rawQuery Questions & Answers:

Other SQLiteDatabase rawQuery Examples

87:	
88:			SQLiteDatabase db = this.getReadableDatabase();
89:			Cursor cursor = db.rawQuery(selectQuery, null);
90:	
91:			// looping through all rows and adding to list
-
170:		public boolean isSiteExists(SQLiteDatabase db, String rss_link) {
171:	
172:			Cursor cursor = db.rawQuery("SELECT 1 FROM " + TABLE_RSS
173:					+ " WHERE rss_link = '" + rss_link + "'", new String[] {});
174:			boolean exists = (cursor.getCount() > 0);

Read More

89:	
90:			SQLiteDatabase db = this.getWritableDatabase();
91:			Cursor cursor = db.rawQuery(selectQuery, null);
92:	
93:			// looping through all rows and adding to list
-
133:			String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
134:			SQLiteDatabase db = this.getReadableDatabase();
135:			Cursor cursor = db.rawQuery(countQuery, null);
136:			cursor.close();
137:

Read More

2:			String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
3:			SQLiteDatabase db = this.getReadableDatabase();
4:			Cursor cursor = db.rawQuery(countQuery, null);
5:			cursor.close();
6:

Read More

rawQuery Method Overview
Runs the provided SQL and returns a Cursor over the result set.
See Code Examples for other Android SQLiteDatabase Methods: