Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 2.18 KB | None | 0 0
  1. /**
  2.  * Using SQLite in Vala Sample Code
  3.  * Port of an example found on the SQLite site.
  4.  * http://www.sqlite.org/quickstart.html
  5.  */
  6.  
  7.  using GLib;
  8.  using Sqlite;
  9.  
  10.  public class SqliteSample : Object {
  11.  
  12.     public static int callback (int n_columns, string[] values,
  13.         string[] column_names)
  14.     {
  15.         for (int i = 0; i < n_columns; i++) {
  16.             debug ("%s = %s\n", column_names[i], values[i]);
  17.         }
  18.         debug ("\n");
  19.  
  20.         return 0;
  21.     }
  22.  
  23.     public static int main (string[] args) {
  24.         Database db;
  25.         int rc;
  26.  
  27.         if (args.length != 4) {
  28.             error ("Usage: %s DATABASE SQL-STATEMENT STMT2\n", args[0]);
  29.             return 1;
  30.         }
  31.  
  32.         if (!FileUtils.test (args[1], FileTest.IS_REGULAR)) {
  33.             error ("Database %s does not exist or is directory\n", args[1]);
  34.             return 1;
  35.         }
  36.  
  37.         rc = Database.open (args[1], out db);
  38.  
  39.         if (rc != Sqlite.OK) {
  40.             error ("Can't open database: %d, %s\n", rc, db.errmsg ());
  41.             return 1;
  42.         }
  43.  
  44.         try {
  45.  
  46.             Cancellable cancellable = new Cancellable ();
  47.             var t = new Thread<int> (null, () => {
  48.                 try {
  49.                     cancellable.set_error_if_cancelled();
  50.                     // this line is blocking
  51.                     if (db.exec (args[2], callback, null) != Sqlite.OK) {
  52.                         warning ("SQL error: %d, %s\n", rc, db.errmsg ());
  53.                     }
  54.                     return 0;
  55.                     } catch ( Error e ) {
  56.                         warning (e.message);
  57.                         return -1;
  58.                     }
  59.                     });
  60.             debug("runnig");
  61.             Thread.usleep ((ulong) TimeSpan.SECOND);
  62.             debug("stop");
  63.             cancellable.cancel();
  64.             // nothing happens actually here, because thread is still within db.exec
  65.             Thread.usleep ((ulong)1e7);
  66.  
  67.             debug("continuing...");
  68.  
  69.             if (db.exec (args[3], callback, null) != Sqlite.OK) {
  70.                 warning ("SQL error: %d, %s\n", rc, db.errmsg ());
  71.             }
  72.             return 0;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement