diff options
| author | 2020-10-29 23:55:18 +0100 | |
|---|---|---|
| committer | 2020-10-29 23:55:18 +0100 | |
| commit | 64fd636b7f1918a02f5af3f0627959f62e04d2c1 (patch) | |
| tree | 87b1136d3983f8174f7f1d689403b0543ac22693 /README.md | |
| parent | update documentation (diff) | |
| download | zig-sqlite-64fd636b7f1918a02f5af3f0627959f62e04d2c1.tar.gz zig-sqlite-64fd636b7f1918a02f5af3f0627959f62e04d2c1.tar.xz zig-sqlite-64fd636b7f1918a02f5af3f0627959f62e04d2c1.zip | |
update readme
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 28 |
1 files changed, 9 insertions, 19 deletions
| @@ -46,20 +46,10 @@ sqlite works exclusively by using prepared statements. The wrapper type is `sqli | |||
| 46 | \\SELECT id, name, age, salary FROM employees WHERE age > ? AND age < ? | 46 | \\SELECT id, name, age, salary FROM employees WHERE age > ? AND age < ? |
| 47 | ; | 47 | ; |
| 48 | 48 | ||
| 49 | var stmt = try db.prepare(query, .{ | 49 | var stmt = try db.prepare(query); |
| 50 | .age1 = 20, | ||
| 51 | .age2 = 40, | ||
| 52 | }); | ||
| 53 | defer stmt.deinit(); | 50 | defer stmt.deinit(); |
| 54 | 51 | ||
| 55 | The `Db.prepare` method takes a `comptime` query string and a tuple of bind parameters. | 52 | The `Db.prepare` method takes a `comptime` query string. |
| 56 | |||
| 57 | The number of bind parameters is comptime checked against the number of bind markers in the query string, so if you have 2 bind markers | ||
| 58 | you must provide 2 bind parameters. | ||
| 59 | |||
| 60 | Note that the fields name is irrelevant, only their order is relevant. | ||
| 61 | |||
| 62 | See the section "Bind parameters and resultset rows" for more information on the types mapping rules. | ||
| 63 | 53 | ||
| 64 | ### Executing a statement | 54 | ### Executing a statement |
| 65 | 55 | ||
| @@ -69,13 +59,15 @@ For queries which do not return data (`INSERT`, `UPDATE`) you can use the `exec` | |||
| 69 | \\UPDATE foo SET salary = ? WHERE id = ? | 59 | \\UPDATE foo SET salary = ? WHERE id = ? |
| 70 | ; | 60 | ; |
| 71 | 61 | ||
| 72 | var stmt = try db.prepare(query, .{ | 62 | var stmt = try db.prepare(query); |
| 63 | defer stmt.deinit(); | ||
| 64 | |||
| 65 | try stmt.exec({ | ||
| 73 | .salary = 20000, | 66 | .salary = 20000, |
| 74 | .id = 40, | 67 | .id = 40, |
| 75 | }); | 68 | }); |
| 76 | defer stmt.deinit(); | ||
| 77 | 69 | ||
| 78 | try stmt.exec(); | 70 | See the section "Bind parameters and resultset rows" for more information on the types mapping rules. |
| 79 | 71 | ||
| 80 | ### Reading data | 72 | ### Reading data |
| 81 | 73 | ||
| @@ -85,10 +77,7 @@ For queries which do return data you can use the `all` method: | |||
| 85 | \\SELECT id, name, age, salary FROM employees WHERE age > ? AND age < ? | 77 | \\SELECT id, name, age, salary FROM employees WHERE age > ? AND age < ? |
| 86 | ; | 78 | ; |
| 87 | 79 | ||
| 88 | var stmt = try db.prepare(query, .{ | 80 | var stmt = try db.prepare(query); |
| 89 | .age1 = 20, | ||
| 90 | .age2 = 40, | ||
| 91 | }); | ||
| 92 | defer stmt.deinit(); | 81 | defer stmt.deinit(); |
| 93 | 82 | ||
| 94 | const rows = try stmt.all( | 83 | const rows = try stmt.all( |
| @@ -99,6 +88,7 @@ For queries which do return data you can use the `all` method: | |||
| 99 | salary: u32, | 88 | salary: u32, |
| 100 | }, | 89 | }, |
| 101 | .{ .allocator = allocator }, | 90 | .{ .allocator = allocator }, |
| 91 | .{ .age1 = 20, .age2 = 40 }, | ||
| 102 | ); | 92 | ); |
| 103 | for (rows) |row| { | 93 | for (rows) |row| { |
| 104 | std.log.debug("id: {} ; name: {}; age: {}; salary: {}", .{ row.id, row.name, row.age, row.salary }); | 94 | std.log.debug("id: {} ; name: {}; age: {}; salary: {}", .{ row.id, row.name, row.age, row.salary }); |