mirror of
https://github.com/isar/rusqlite.git
synced 2024-11-26 19:41:37 +08:00
Merge pull request #481 from gwenn/rm-unwrap
Remove unwrap from examples
This commit is contained in:
commit
333cc49523
16
README.md
16
README.md
@ -11,7 +11,7 @@ an interface similar to [rust-postgres](https://github.com/sfackler/rust-postgre
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
use rusqlite::types::ToSql;
|
use rusqlite::types::ToSql;
|
||||||
use rusqlite::{Connection, NO_PARAMS};
|
use rusqlite::{Connection, Result, NO_PARAMS};
|
||||||
use time::Timespec;
|
use time::Timespec;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -22,8 +22,8 @@ struct Person {
|
|||||||
data: Option<Vec<u8>>,
|
data: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<()> {
|
||||||
let conn = Connection::open_in_memory().unwrap();
|
let conn = Connection::open_in_memory()?;
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"CREATE TABLE person (
|
"CREATE TABLE person (
|
||||||
@ -33,7 +33,7 @@ fn main() {
|
|||||||
data BLOB
|
data BLOB
|
||||||
)",
|
)",
|
||||||
NO_PARAMS,
|
NO_PARAMS,
|
||||||
).unwrap();
|
)?;
|
||||||
let me = Person {
|
let me = Person {
|
||||||
id: 0,
|
id: 0,
|
||||||
name: "Steven".to_string(),
|
name: "Steven".to_string(),
|
||||||
@ -44,22 +44,22 @@ fn main() {
|
|||||||
"INSERT INTO person (name, time_created, data)
|
"INSERT INTO person (name, time_created, data)
|
||||||
VALUES (?1, ?2, ?3)",
|
VALUES (?1, ?2, ?3)",
|
||||||
&[&me.name as &ToSql, &me.time_created, &me.data],
|
&[&me.name as &ToSql, &me.time_created, &me.data],
|
||||||
).unwrap();
|
)?;
|
||||||
|
|
||||||
let mut stmt = conn
|
let mut stmt = conn
|
||||||
.prepare("SELECT id, name, time_created, data FROM person")
|
.prepare("SELECT id, name, time_created, data FROM person")?;
|
||||||
.unwrap();
|
|
||||||
let person_iter = stmt
|
let person_iter = stmt
|
||||||
.query_map(NO_PARAMS, |row| Person {
|
.query_map(NO_PARAMS, |row| Person {
|
||||||
id: row.get(0),
|
id: row.get(0),
|
||||||
name: row.get(1),
|
name: row.get(1),
|
||||||
time_created: row.get(2),
|
time_created: row.get(2),
|
||||||
data: row.get(3),
|
data: row.get(3),
|
||||||
}).unwrap();
|
})?;
|
||||||
|
|
||||||
for person in person_iter {
|
for person in person_iter {
|
||||||
println!("Found person {:?}", person.unwrap());
|
println!("Found person {:?}", person.unwrap());
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
26
src/blob.rs
26
src/blob.rs
@ -16,43 +16,41 @@
|
|||||||
//! ```rust
|
//! ```rust
|
||||||
//! use rusqlite::blob::ZeroBlob;
|
//! use rusqlite::blob::ZeroBlob;
|
||||||
//! use rusqlite::{Connection, DatabaseName, NO_PARAMS};
|
//! use rusqlite::{Connection, DatabaseName, NO_PARAMS};
|
||||||
|
//! use std::error::Error;
|
||||||
//! use std::io::{Read, Seek, SeekFrom, Write};
|
//! use std::io::{Read, Seek, SeekFrom, Write};
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() -> Result<(), Box<Error>> {
|
||||||
//! let db = Connection::open_in_memory().unwrap();
|
//! let db = Connection::open_in_memory()?;
|
||||||
//! db.execute_batch("CREATE TABLE test (content BLOB);")
|
//! db.execute_batch("CREATE TABLE test (content BLOB);")?;
|
||||||
//! .unwrap();
|
|
||||||
//! db.execute(
|
//! db.execute(
|
||||||
//! "INSERT INTO test (content) VALUES (ZEROBLOB(10))",
|
//! "INSERT INTO test (content) VALUES (ZEROBLOB(10))",
|
||||||
//! NO_PARAMS,
|
//! NO_PARAMS,
|
||||||
//! )
|
//! )?;
|
||||||
//! .unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! let rowid = db.last_insert_rowid();
|
//! let rowid = db.last_insert_rowid();
|
||||||
//! let mut blob = db
|
//! let mut blob = db
|
||||||
//! .blob_open(DatabaseName::Main, "test", "content", rowid, false)
|
//! .blob_open(DatabaseName::Main, "test", "content", rowid, false)?;
|
||||||
//! .unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! // Make sure to test that the number of bytes written matches what you expect;
|
//! // 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
|
//! // if you try to write too much, the data will be truncated to the size of the
|
||||||
//! // BLOB.
|
//! // BLOB.
|
||||||
//! let bytes_written = blob.write(b"01234567").unwrap();
|
//! let bytes_written = blob.write(b"01234567")?;
|
||||||
//! assert_eq!(bytes_written, 8);
|
//! assert_eq!(bytes_written, 8);
|
||||||
//!
|
//!
|
||||||
//! // Same guidance - make sure you check the number of bytes read!
|
//! // 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 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
|
//! 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)])
|
//! db.execute("INSERT INTO test (content) VALUES (?)", &[ZeroBlob(64)])?;
|
||||||
//! .unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! // given a new row ID, we can reopen the blob on that row
|
//! // given a new row ID, we can reopen the blob on that row
|
||||||
//! let rowid = db.last_insert_rowid();
|
//! let rowid = db.last_insert_rowid();
|
||||||
//! blob.reopen(rowid).unwrap();
|
//! blob.reopen(rowid)?;
|
||||||
//!
|
//!
|
||||||
//! assert_eq!(blob.size(), 64);
|
//! assert_eq!(blob.size(), 64);
|
||||||
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
|
@ -34,19 +34,19 @@
|
|||||||
//! })
|
//! })
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() -> Result<()> {
|
||||||
//! let db = Connection::open_in_memory().unwrap();
|
//! let db = Connection::open_in_memory()?;
|
||||||
//! add_regexp_function(&db).unwrap();
|
//! add_regexp_function(&db)?;
|
||||||
//!
|
//!
|
||||||
//! let is_match: bool = db
|
//! let is_match: bool = db
|
||||||
//! .query_row(
|
//! .query_row(
|
||||||
//! "SELECT regexp('[aeiou]*', 'aaaaeeeiii')",
|
//! "SELECT regexp('[aeiou]*', 'aaaaeeeiii')",
|
||||||
//! NO_PARAMS,
|
//! NO_PARAMS,
|
||||||
//! |row| row.get(0),
|
//! |row| row.get(0),
|
||||||
//! )
|
//! )?;
|
||||||
//! .unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! assert!(is_match);
|
//! assert!(is_match);
|
||||||
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -3,7 +3,7 @@
|
|||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use rusqlite::types::ToSql;
|
//! use rusqlite::types::ToSql;
|
||||||
//! use rusqlite::{params, Connection};
|
//! use rusqlite::{params, Connection, Result};
|
||||||
//! use time::Timespec;
|
//! use time::Timespec;
|
||||||
//!
|
//!
|
||||||
//! #[derive(Debug)]
|
//! #[derive(Debug)]
|
||||||
@ -14,8 +14,8 @@
|
|||||||
//! data: Option<Vec<u8>>,
|
//! data: Option<Vec<u8>>,
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() -> Result<()> {
|
||||||
//! let conn = Connection::open_in_memory().unwrap();
|
//! let conn = Connection::open_in_memory()?;
|
||||||
//!
|
//!
|
||||||
//! conn.execute(
|
//! conn.execute(
|
||||||
//! "CREATE TABLE person (
|
//! "CREATE TABLE person (
|
||||||
@ -25,8 +25,7 @@
|
|||||||
//! data BLOB
|
//! data BLOB
|
||||||
//! )",
|
//! )",
|
||||||
//! params![],
|
//! params![],
|
||||||
//! )
|
//! )?;
|
||||||
//! .unwrap();
|
|
||||||
//! let me = Person {
|
//! let me = Person {
|
||||||
//! id: 0,
|
//! id: 0,
|
||||||
//! name: "Steven".to_string(),
|
//! name: "Steven".to_string(),
|
||||||
@ -37,24 +36,22 @@
|
|||||||
//! "INSERT INTO person (name, time_created, data)
|
//! "INSERT INTO person (name, time_created, data)
|
||||||
//! VALUES (?1, ?2, ?3)",
|
//! VALUES (?1, ?2, ?3)",
|
||||||
//! params![me.name, me.time_created, me.data],
|
//! params![me.name, me.time_created, me.data],
|
||||||
//! )
|
//! )?;
|
||||||
//! .unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! let mut stmt = conn
|
//! let mut stmt = conn
|
||||||
//! .prepare("SELECT id, name, time_created, data FROM person")
|
//! .prepare("SELECT id, name, time_created, data FROM person")?;
|
||||||
//! .unwrap();
|
|
||||||
//! let person_iter = stmt
|
//! let person_iter = stmt
|
||||||
//! .query_map(params![], |row| Person {
|
//! .query_map(params![], |row| Person {
|
||||||
//! id: row.get(0),
|
//! id: row.get(0),
|
||||||
//! name: row.get(1),
|
//! name: row.get(1),
|
||||||
//! time_created: row.get(2),
|
//! time_created: row.get(2),
|
||||||
//! data: row.get(3),
|
//! data: row.get(3),
|
||||||
//! })
|
//! })?;
|
||||||
//! .unwrap();
|
|
||||||
//!
|
//!
|
||||||
//! for person in person_iter {
|
//! for person in person_iter {
|
||||||
//! println!("Found person {:?}", person.unwrap());
|
//! println!("Found person {:?}", person.unwrap());
|
||||||
//! }
|
//! }
|
||||||
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
#![allow(unknown_lints)]
|
#![allow(unknown_lints)]
|
||||||
|
Loading…
Reference in New Issue
Block a user