SQLiteDatabase insertOrThrow - 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 insertOrThrow method.

This code example shows how to use the following methods:beginTransaction, endTransaction, insertOrThrow
            throw new IllegalArgumentException("unable to insert with this uri: " + uri);
        } 
        SQLiteDatabase db = null;
        try { 
            db = mDatabaseHelper.getWritableDatabase();
            db.beginTransaction();
            final long id = db.insertOrThrow("tests", null, values);
            Uri newUri = ContentUris.withAppendedId(uri, id);
            db.setTransactionSuccessful();
            return newUri;
        } finally { 
            if (db != null) {
                db.endTransaction();
            } 
        } 
    } 
 
    @Override 
    public boolean onCreate() { 
        mDatabaseHelper = new MyDatabaseHelper(getContext());
8
CodeRank
This code example shows how to use the following methods:insertOrThrow, rawQuery
        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};
        ContentValues values = new ContentValues();
        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);
        database.insertOrThrow("table_name", null, values);
         
        Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null);
        assertThat(cursor.moveToFirst()).isTrue();
        byte[] byteValueFromDatabase = cursor.getBlob(0);
        String stringValueFromDatabase = cursor.getString(1);
        assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue);
        assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue);
    } 
 
    @Test(expected = IllegalArgumentException.class) 
    public void testRawQueryThrowsIndex0NullException() throws Exception { 
        database.rawQuery("select second_column, first_column from rawtable WHERE `id` = ?", new String[]{null});
    } 
 
7
CodeRank
This code example shows how to use the following methods:close, beginTransaction, endTransaction, insertOrThrow, setTransactionSuccessful
 
	public long insertUser( ContentValues userValues) throws SQLException {
		long result = -1;
		SQLiteDatabase db = mDatabaseOpenHelper.getWritableDatabase();
		try { 
			db.beginTransaction();
			try { 
		 result = db.insertOrThrow(UserDatabase.TABLE_USER_FTS, null, userValues);
				// commit 
				db.setTransactionSuccessful();
			} finally { 
				db.endTransaction();
			} 
		} finally { 
			db.close();
		} 
		return result;
	} 
 
	public int delete(String selection, String[] selectionArgs) {
6
CodeRank
This code example shows how to use the following methods:insert, insertOrThrow, rawQuery, update
		ftsVals.put(C_DESC, values.getAsString(C_DESC));
		ftsVals.put(C_NAME, values.getAsString(C_NAME));
		ftsVals.put(C_LAST_MODIFIED, values.getAsLong(C_LAST_MODIFIED));
		ftsVals.put(C_SUPERVISOR, values.getAsString(C_SUPERVISOR));
		try { 
			db.insertOrThrow(TASK_TABLE, null, values);
			db.insert(FTS_TABLE, null, ftsVals);
		} catch (SQLException e) {
			Log.d(TAG, "exception: " + e.getLocalizedMessage());
			Long id = values.getAsLong(C_ID);
			Cursor cursor = db.rawQuery("SELECT version FROM " + TASK_TABLE +
					" WHERE " + C_ID + " = " + Long.toString(id), null);
			if( cursor != null ) {
				Long remote_ver = values.getAsLong(C_VERSION);
				cursor.moveToFirst(); 
				if(cursor.getLong(0) != remote_ver) {
					db.update(TASK_TABLE, values, C_ID + " = " + id, null);
					db.update(FTS_TABLE, ftsVals, C_ID + " = " + id, null);
					Log.d(TAG, "duplicate id, task was updated");
				} 

6
CodeRank
This code example shows how to use the following methods:put, insertOrThrow
        onCreate(sqLiteDatabase);
    } 
 
    public void addChatPost(String nickname, String post)
    { 
        ContentValues contentValues = new ContentValues();
        contentValues.put("nickname", nickname);
        contentValues.put("post", post);
        getWritableDatabase().insertOrThrow(CHATLOG, null, contentValues);
    } 
 
    public List<ChatMessage> getAllMessages()
    { 
        Cursor cursor = getReadableDatabase().query(CHATLOG, new String[]{"nickname", "post"}, null, null, null, null, "id");
        List<ChatMessage> messages = new ArrayList<ChatMessage>();
 
        while (cursor.moveToNext())
        { 
            messages.add(new ChatMessage(ChatMessage.Type.Message, cursor.getString(0), cursor.getString(1)));
        } 
6
CodeRank
This code example shows how to use the following methods:close, insertOrThrow, update
This code example shows how to use the following methods:insertOrThrow, isOpen
	 */ 
	public Boolean testSQLiteDatabase()
	{ 
		if (isConnect)
		{ 
			if (mSQLiteDatabase.isOpen())
			{ 
				return true; 
			} else 
			{ 
				return false; 
			} 
		} else 
		{ 
			return false; 
		} 
	} 
 
	/** 
	 * 以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决, 

5
CodeRank
This code example shows how to use the following methods:put, insertOrThrow
This code example shows how to use the following methods:execSQL, insertOrThrow
			row.put(COLUMN_EDITION, edition);
			row.put(COLUMN_PRICE, price);
			row.put(COLUMN_QUANTITY, quantity);
			 
			try { 
	    		mDatabase.insertOrThrow(tableName, null, row);
	    	} 
	    	catch (SQLException e) {
	    		mDatabase.execSQL(String.format("UPDATE %s SET %s=%d WHERE %s=%d", tableName, COLUMN_QUANTITY, quantity, COLUMN_PRODUCT_HASH, productHash));
	    		//mDatabase.update(tableName, row, String.format("%s=%s", COLUMN_PRODUCT_HASH, productHash), null); 
	    	} 
		} 
 
		else { 
			mDatabase.delete(tableName, String.format("%s=%s", COLUMN_PRODUCT_HASH, productHash), null);
		} 
    } 
     
    @Override 
	public synchronized void close() { 
5
CodeRank
This code example shows how to use the following methods:close, insertOrThrow
		Log.d(TAG, "insertOrIgnore on " + values);
		// open database 
		db = dbHelper.getWritableDatabase();
		long l;
		try { 
			l = db.insertOrThrow(table, null, values);
		} catch (SQLException e) {
			l = -1;
		} finally { 
			// close database 
			db.close();
		} 
		return l;
 
	} 
 
	/** 
	 * query db for reteriving messages 
	 *  
	 * @param type 
4
CodeRank

Related SQLiteDatabase insertOrThrow Questions & Answers:

insertOrThrow Method Overview
Convenience method for inserting a row into the database.
See Code Examples for other Android SQLiteDatabase Methods: