@@ -25,7 +25,7 @@ postgres="*"
2525
2626当然我们还是进行最简单的操作,直接粘贴复制,[ 代码来源] ( https://github.com/sfackler/rust-postgres#overview )
2727
28- ``` rust
28+ ``` rust
2929
3030extern crate postgres;
3131
@@ -108,10 +108,10 @@ pub fn insert_info(conn : &Connection,title : &str, body: &str){
108108 let stmt = match conn . prepare (" insert into blog (title, body) values ($1, $2)" ) {
109109 Ok (stmt ) => stmt ,
110110 Err (e ) => {
111- println! (" Preparing query failed: {:?}" , e );
111+ println! (" Preparing query failed: {:?}" , e );
112112 return ;
113- }
114- };
113+ }
114+ };
115115 stmt . execute (& [& title , & body ]). expect (" Inserting blogposts failed" );
116116}
117117
@@ -126,9 +126,9 @@ pub fn query<T>(conn: &Connection,query: &str) ->PgResult<T>
126126 // rows.iter().next().unwrap()
127127 row . get_opt (2 ). unwrap ()
128128
129- }
129+ }
130130
131- pub fn query_all (conn : & Connection ,query : & str ){
131+ pub fn query_all (conn : & Connection ,query : & str ){
132132 println! (" Executing query: {}" , query );
133133 for row in & conn . query (query ,& []). unwrap (){
134134 let person = Person {
@@ -139,7 +139,7 @@ pub fn query_all(conn: &Connection,query: &str){
139139 println! (" Found person {}" , person . name);
140140 }
141141
142- }
142+ }
143143
144144```
145145然后在main.rs 中调用相应的函数代码如下
@@ -150,7 +150,7 @@ pub fn query_all(conn: &Connection,query: &str){
150150
151151``` rust
152152extern crate postgres;
153- extern crate db;
153+ extern crate db;
154154
155155use postgres :: {Connection , SslMode };
156156
@@ -162,19 +162,19 @@ struct Blog {
162162}
163163
164164fn main () {
165- let conn : Connection = connect ();
166-
165+ let conn : Connection = connect ();
166+
167167 let blog = Blog {
168168 title : String :: from (" title" ),
169169 body : String :: from (" body" ),
170- };
170+ };
171171 let title = blog . title. to_string ();
172172 let body = blog . body. to_string ();
173- insert_info (& conn ,& title ,& body );
173+ insert_info (& conn ,& title ,& body );
174174
175175 for row in query :: <String >(& conn ," select * from blog" ){
176176 println! (" {:?}" ,row );
177- }
177+ }
178178 let sql = " select * from person" ;
179179 query_all (& conn ,& sql );
180180}
@@ -192,14 +192,14 @@ fn main() {
192192说返回的是一个可迭代的数据,那也就是说,我可以使用for循环,将数据打印,
193193但是发现怎么也不能实现:
194194
195- ``` rust
195+ ``` rust
196196
197- pub fn query_all (conn : & Connection ,query : & str ){
197+ pub fn query_all (conn : & Connection ,query : & str ){
198198 println! (" Executing query: {}" , query );
199- for row in & conn . query (query ,& []). unwrap (){
199+ for row in & conn . query (query ,& []). unwrap (){
200200 println! (" Found person {:?}" , row . get_opt (1 ));
201201 }
202- }
202+ }
203203
204204```
205205报错如下:
0 commit comments