mirror of
https://github.com/isar/rusqlite.git
synced 2025-08-20 21:09:31 +08:00
Merge pull request #481 from gwenn/rm-unwrap
Remove unwrap from examples
This commit is contained in:
26
src/blob.rs
26
src/blob.rs
@@ -16,43 +16,41 @@
|
||||
//! ```rust
|
||||
//! use rusqlite::blob::ZeroBlob;
|
||||
//! use rusqlite::{Connection, DatabaseName, NO_PARAMS};
|
||||
//! use std::error::Error;
|
||||
//! use std::io::{Read, Seek, SeekFrom, Write};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let db = Connection::open_in_memory().unwrap();
|
||||
//! db.execute_batch("CREATE TABLE test (content BLOB);")
|
||||
//! .unwrap();
|
||||
//! 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,
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! )?;
|
||||
//!
|
||||
//! let rowid = db.last_insert_rowid();
|
||||
//! let mut blob = db
|
||||
//! .blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
||||
//! .unwrap();
|
||||
//! .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").unwrap();
|
||||
//! 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)).unwrap();
|
||||
//! blob.seek(SeekFrom::Start(0))?;
|
||||
//! let mut buf = [0u8; 20];
|
||||
//! let bytes_read = blob.read(&mut buf[..]).unwrap();
|
||||
//! 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)])
|
||||
//! .unwrap();
|
||||
//! 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).unwrap();
|
||||
//! blob.reopen(rowid)?;
|
||||
//!
|
||||
//! assert_eq!(blob.size(), 64);
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
use std::cmp::min;
|
||||
|
@@ -34,19 +34,19 @@
|
||||
//! })
|
||||
//! }
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let db = Connection::open_in_memory().unwrap();
|
||||
//! add_regexp_function(&db).unwrap();
|
||||
//! fn main() -> Result<()> {
|
||||
//! let db = Connection::open_in_memory()?;
|
||||
//! add_regexp_function(&db)?;
|
||||
//!
|
||||
//! let is_match: bool = db
|
||||
//! .query_row(
|
||||
//! "SELECT regexp('[aeiou]*', 'aaaaeeeiii')",
|
||||
//! NO_PARAMS,
|
||||
//! |row| row.get(0),
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! )?;
|
||||
//!
|
||||
//! assert!(is_match);
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
use std::error::Error as StdError;
|
||||
|
19
src/lib.rs
19
src/lib.rs
@@ -3,7 +3,7 @@
|
||||
//!
|
||||
//! ```rust
|
||||
//! use rusqlite::types::ToSql;
|
||||
//! use rusqlite::{params, Connection};
|
||||
//! use rusqlite::{params, Connection, Result};
|
||||
//! use time::Timespec;
|
||||
//!
|
||||
//! #[derive(Debug)]
|
||||
@@ -14,8 +14,8 @@
|
||||
//! data: Option<Vec<u8>>,
|
||||
//! }
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let conn = Connection::open_in_memory().unwrap();
|
||||
//! fn main() -> Result<()> {
|
||||
//! let conn = Connection::open_in_memory()?;
|
||||
//!
|
||||
//! conn.execute(
|
||||
//! "CREATE TABLE person (
|
||||
@@ -25,8 +25,7 @@
|
||||
//! data BLOB
|
||||
//! )",
|
||||
//! params![],
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! )?;
|
||||
//! let me = Person {
|
||||
//! id: 0,
|
||||
//! name: "Steven".to_string(),
|
||||
@@ -37,24 +36,22 @@
|
||||
//! "INSERT INTO person (name, time_created, data)
|
||||
//! VALUES (?1, ?2, ?3)",
|
||||
//! params![me.name, me.time_created, me.data],
|
||||
//! )
|
||||
//! .unwrap();
|
||||
//! )?;
|
||||
//!
|
||||
//! let mut stmt = conn
|
||||
//! .prepare("SELECT id, name, time_created, data FROM person")
|
||||
//! .unwrap();
|
||||
//! .prepare("SELECT id, name, time_created, data FROM person")?;
|
||||
//! let person_iter = stmt
|
||||
//! .query_map(params![], |row| Person {
|
||||
//! id: row.get(0),
|
||||
//! name: row.get(1),
|
||||
//! time_created: row.get(2),
|
||||
//! data: row.get(3),
|
||||
//! })
|
||||
//! .unwrap();
|
||||
//! })?;
|
||||
//!
|
||||
//! for person in person_iter {
|
||||
//! println!("Found person {:?}", person.unwrap());
|
||||
//! }
|
||||
//! Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
#![allow(unknown_lints)]
|
||||
|
Reference in New Issue
Block a user