[][src]Module rusqlite::blob

Incremental BLOB I/O.

Note that SQLite does not provide API-level access to change the size of a BLOB; that must be performed through SQL statements.

Blob conforms to std::io::Read, std::io::Write, and std::io::Seek, so it plays nicely with other types that build on these (such as std::io::BufReader and std::io::BufWriter). However, you must be careful with the size of the blob. For example, when using a BufWriter, the BufWriter will accept more data than the Blob will allow, so make sure to call flush and check for errors. (See the unit tests in this module for an example.)

Example

use rusqlite::blob::ZeroBlob;
use rusqlite::{Connection, DatabaseName, NO_PARAMS};
use std::error::Error;
use std::io::{Read, Seek, SeekFrom, Write};

fn main() -> Result<(), Box<Error>> {
    let db = Connection::open_in_memory()?;
    db.execute_batch("CREATE TABLE test (content BLOB);")?;
    db.execute(
        "INSERT INTO test (content) VALUES (ZEROBLOB(10))",
        NO_PARAMS,
    )?;

    let rowid = db.last_insert_rowid();
    let mut blob = db.blob_open(DatabaseName::Main, "test", "content", rowid, false)?;

    // Make sure to test that the number of bytes written matches what you expect;
    // if you try to write too much, the data will be truncated to the size of the
    // BLOB.
    let bytes_written = blob.write(b"01234567")?;
    assert_eq!(bytes_written, 8);

    // Same guidance - make sure you check the number of bytes read!
    blob.seek(SeekFrom::Start(0))?;
    let mut buf = [0u8; 20];
    let bytes_read = blob.read(&mut buf[..])?;
    assert_eq!(bytes_read, 10); // note we read 10 bytes because the blob has size 10

    db.execute("INSERT INTO test (content) VALUES (?)", &[ZeroBlob(64)])?;

    // given a new row ID, we can reopen the blob on that row
    let rowid = db.last_insert_rowid();
    blob.reopen(rowid)?;

    assert_eq!(blob.size(), 64);
    Ok(())
}

Structs

Blob

Handle to an open BLOB.

ZeroBlob

BLOB of length N that is filled with zeroes.