Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 : Object {
- public static int callback (int n_columns, string[] values,
- string[] column_names)
- {
- for (int i = 0; i < n_columns; i++) {
- debug ("%s = %s\n", column_names[i], values[i]);
- }
- debug ("\n");
- return 0;
- }
- public static int main (string[] args) {
- Database db;
- int rc;
- if (args.length != 4) {
- error ("Usage: %s DATABASE SQL-STATEMENT STMT2\n", args[0]);
- return 1;
- }
- if (!FileUtils.test (args[1], FileTest.IS_REGULAR)) {
- error ("Database %s does not exist or is directory\n", args[1]);
- return 1;
- }
- rc = Database.open (args[1], out db);
- if (rc != Sqlite.OK) {
- error ("Can't open database: %d, %s\n", rc, db.errmsg ());
- return 1;
- }
- try {
- Cancellable cancellable = new Cancellable ();
- var t = new Thread<int> (null, () => {
- try {
- cancellable.set_error_if_cancelled();
- // this line is blocking
- if (db.exec (args[2], callback, null) != Sqlite.OK) {
- warning ("SQL error: %d, %s\n", rc, db.errmsg ());
- }
- return 0;
- } catch ( Error e ) {
- warning (e.message);
- return -1;
- }
- });
- debug("runnig");
- Thread.usleep ((ulong) TimeSpan.SECOND);
- debug("stop");
- cancellable.cancel();
- // nothing happens actually here, because thread is still within db.exec
- Thread.usleep ((ulong)1e7);
- debug("continuing...");
- if (db.exec (args[3], callback, null) != Sqlite.OK) {
- warning ("SQL error: %d, %s\n", rc, db.errmsg ());
- }
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement