Skip to content

Commit

Permalink
chore: update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoop committed Jul 29, 2024
1 parent 76258c9 commit 483327c
Show file tree
Hide file tree
Showing 7 changed files with 1,490 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.zig-cache
zig-cache
.vscode
libsqlite*
29 changes: 13 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,38 @@ exe.root_module.addImport("zigqlite", zigqlite);

## Usage

### DB object:
See also, the [API documentation](https://joekoop.com/zigqlite).

To open a database in the given path, creating it if needed:
To open a database file at the given path, creating it if needed:

```zig
var db = try sqlite.DB.open(<path>);
defer db.close();
var db = try sqlite.DB.open(allocator, "database.db");
defer db.close() catch unreachable; // @todo figure out if I can handle this error or not
```

### Prepared Statement
Create a statment:

```zig
var stmt = try db.prep(<sql>);
var stmt = try db.prep("SELECT * FROM iguanas WHERE name = ?");
```

Where `<sql>` is the SQL command text. Can include positional arguments like `?` or named arguments like `:argname`.
The SQL can include positional arguments like `?` or named arguments like `:argname`.

Once prepared, the statement is executed with the `.exec()` function:

```zig
var cursor = try stmt.exec(<args>, <rowtype>);
// var cursor = try stmt.exec(args, rowtype);
var cursor = try stmt.exec(.{"Zero"}, struct { name: []u8, photo_url: []u8 });
```

Where `<args>` is a tuple or structure holding arguments to fill in the SQL command. If field names match named arguments, they are used regardless of order; otherwise the position in the tuple or struct determines the field it fills.
Where `args` is a tuple or structure holding arguments to fill in the SQL command. If field names match named arguments, they are used regardless of order; otherwise the position in the tuple or struct determines the field it fills.

The `<rowtype>` parameter is a struct type. Each returned row will be a value of this type. Each field will hold a column from the row, in order. If there are more fields than columns, the extra fields would be filled with their respective default value, if declared in the struct.

### Cursor object
The `rowtype` parameter is a struct type. Each returned row will be a value of this type. Each field will hold a column from the row, in order. If there are more fields than columns, the extra fields would be filled with their respective default value, if declared in the struct.

The value returned by the `stmt.exec()` function is used to retrieve results row by row with the `.fetch()` function:

```zig
var stmt = try db.prep("select name, age from children where height >= :minheight");
var cursor = try stmt.exec(.{1.30}, struct{name: []const u8, age: i16});
while (try cursor.fetch()) |kid| {
std.debug.print("{s} is {d} years old\n", .{kid.name, kid.age});
while (try cursor.fetch()) |iguana| {
std.debug.print("{s}'s picture is at {s}\n", .{ iguana.name, iguana.photo_url });
}
```
Loading

0 comments on commit 483327c

Please sign in to comment.