码迷,mamicode.com
首页 > 数据库 > 详细

sqlite3在D语言下之代码流畅体验

时间:2014-07-16 21:19:54      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   for   io   

 1 // Open a database in memory.
 2 Database db;
 3 try
 4 {
 5     db = Database(":memory:");
 6 }
 7 catch (SqliteException e)
 8 {
 9     // Error creating the database
10     assert(false, "Error: " ~ e.msg);
11 }
12 
13 // Create a table.
14 try
15 {
16     db.execute(
17         "CREATE TABLE person (
18             id INTEGER PRIMARY KEY,
19             last_name TEXT NOT NULL,
20             first_name TEXT,
21             score REAL,
22             photo BLOB
23          )"
24     );
25 }
26 catch (SqliteException e)
27 {
28     // Error creating the table.
29     assert(false, "Error: " ~ e.msg);
30 }
31 
32 // Populate the table.
33 try
34 {
35     auto query = db.query(
36         "INSERT INTO person (last_name, first_name, score, photo)
37          VALUES (:last_name, :first_name, :score, :photo)"
38     );
39     
40     // Bind everything with chained calls to params.bind().
41     query.params.bind(":last_name", "Smith")
42                 .bind(":first_name", "John")
43                 .bind(":score", 77.5);
44     ubyte[] photo = cast(ubyte[]) "..."; // Store the photo as raw array of data.
45     query.params.bind(":photo", photo);
46     query.execute();
47     
48     query.reset(); // Need to reset the query after execution.
49     query.params.bind(":last_name", "Doe")
50                 .bind(":first_name", "John")
51                 .bind(3, null) // Use of index instead of name.
52                 .bind(":photo", null);
53     query.execute();
54 }
55 catch (SqliteException e)
56 {
57     // Error executing the query.
58     assert(false, "Error: " ~ e.msg);
59 }
60 assert(db.totalChanges == 2); // Two ‘persons‘ were inserted.
61 
62 // Reading the table
63 try
64 {
65     // Count the Johns in the table.
66     auto query = db.query("SELECT count(*) FROM person WHERE first_name == ‘John‘");
67     assert(query.rows.front[0].get!int() == 2);
68     
69     // Fetch the data from the table.
70     query = db.query("SELECT * FROM person");
71     foreach (row; query.rows)
72     {
73         // "id" should be the column at index 0:
74         auto id = row[0].get!int();
75         // Some conversions are possible with the method as():
76         auto name = format("%s, %s", row["last_name"].get!string(), row["first_name"].get!(char[])());
77         // The score can be NULL, so provide 0 (instead of NAN) as a default value to replace NULLs:
78         auto score = row["score"].get!real(0.0);
79         // Use of opDispatch with column name:
80         auto photo = row.photo.get!(ubyte[])();
81         
82         // ... and use all these data!
83     }
84 }
85 catch (SqliteException e)
86 {
87     // Error reading the database.
88     assert(false, "Error: " ~ e.msg);
89 }

 

sqlite3在D语言下之代码流畅体验,布布扣,bubuko.com

sqlite3在D语言下之代码流畅体验

标签:style   blog   color   os   for   io   

原文地址:http://www.cnblogs.com/godspeedsam/p/3836203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!