diff options
| author | ruki <[email protected]> | 2021-08-14 00:53:49 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-08-14 00:53:49 +0800 |
| commit | ca8ccb9f1969491c5824adb640841d6ece29c3ed (patch) | |
| tree | 8542bad6730f335abbce905bd633a171f3209d03 | |
| parent | bc20947704e91af4598d140166dfe9693df5be50 (diff) | |
add sqlite3 vala example
| -rw-r--r-- | tests/projects/vala/sqlite3/src/main.vala | 63 | ||||
| -rw-r--r-- | tests/projects/vala/sqlite3/xmake.lua | 10 |
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/projects/vala/sqlite3/src/main.vala b/tests/projects/vala/sqlite3/src/main.vala new file mode 100644 index 000000000..536e0d5e0 --- /dev/null +++ b/tests/projects/vala/sqlite3/src/main.vala @@ -0,0 +1,63 @@ +/** + * Using SQLite in Vala Sample Code + * Port of an example found on the SQLite site. + * http://www.sqlite.org/quickstart.html + */ + +using GLib; +using Sqlite; + +public class SqliteSample : GLib.Object { + + public static int callback (int n_columns, string[] values, + string[] column_names) + { + for (int i = 0; i < n_columns; i++) { + stdout.printf ("%s = %s\n", column_names[i], values[i]); + } + stdout.printf ("\n"); + + return 0; + } + + public static int main (string[] args) { + Database db; + int rc; + + if (args.length != 3) { + stderr.printf ("Usage: %s DATABASE SQL-STATEMENT\n", args[0]); + return 1; + } + + if (!FileUtils.test (args[1], FileTest.IS_REGULAR)) { + stderr.printf ("Database %s does not exist or is directory\n", args[1]); + return 1; + } + + rc = Database.open (args[1], out db); + + if (rc != Sqlite.OK) { + stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ()); + return 1; + } + + rc = db.exec (args[2], callback, null); + /* maybe it is better to use closures, so you can access local variables, eg: */ + /*rc = db.exec(args[2], (n_columns, values, column_names) => { + for (int i = 0; i < n_columns; i++) { + stdout.printf ("%s = %s\n", column_names[i], values[i]); + } + stdout.printf ("\n"); + + return 0; + }, null); + */ + + if (rc != Sqlite.OK) { + stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ()); + return 1; + } + + return 0; + } +} diff --git a/tests/projects/vala/sqlite3/xmake.lua b/tests/projects/vala/sqlite3/xmake.lua new file mode 100644 index 000000000..f1e21e8ce --- /dev/null +++ b/tests/projects/vala/sqlite3/xmake.lua @@ -0,0 +1,10 @@ +add_rules("mode.release", "mode.debug") + +add_requires("sqlite3", "glib") + +target("test") + set_kind("binary") + add_rules("vala") + add_files("src/*.vala") + add_packages("sqlite3", "glib") + add_values("vala.packages", "sqlite3") |
