Advertisement
Guest User

inbound.vala

a guest
Feb 15th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.86 KB | None | 0 0
  1. /*
  2.  * Required 'db' file next to 'inbound.vala' file
  3.  *
  4.  * Create 'db' file using sqlite3 command and run this commands:
  5.  *
  6.  * .open db
  7.  * CREATE TABLE STORAGE (
  8.  *  IMG_ID     INTEGER    NOT NULL    PRIMARY KEY    AUTOINCREMENT,
  9.  *  IMG_NAME   TEXT,
  10.  *  IMG_SOURCE TEXT
  11.  * );
  12.  * .exit
  13.  *
  14.  * Compile command: valac --pkg gtk+-3.0 --pkg glib-2.0 --pkg sqlite3 inbound.vala
  15.  *
  16.  * Good Luck!
  17.  *
  18.  */
  19. using Gtk;
  20. using GLib;
  21. using Sqlite;
  22.  
  23. public static int main (string[] args) {
  24.  
  25.     try {
  26.         // Read the PNG File and get the data using DataInputStream
  27.         var file = File.new_for_path ("/home/elementary/Proyectos/imageToDB/test.png");
  28.         var stream = new DataInputStream (file.read());
  29.         var info = file.query_info ("*", FileQueryInfoFlags.NONE);
  30.  
  31.         // Read the data into data var
  32.         int64 file_size = info.get_size ();
  33.         uchar[] data = new uint8[file_size];
  34.         stream.read (data);
  35.         stream.close ();
  36.         stream = null;
  37.  
  38.         // Encode data to Base64
  39.         string image = GLib.Base64.encode(data);
  40.  
  41.         //stdout.printf("Bytes: %s\n", image);<-- This is only for debug
  42.  
  43.         // Open database:
  44.         Sqlite.Database db;
  45.         int ec = Sqlite.Database.open ("db", out db);
  46.  
  47.         if (ec != Sqlite.OK) {
  48.            warning ("Can't open database: %d: %s\n", db.errcode (), db.errmsg ());
  49.            return 1;
  50.         }
  51.  
  52.         // Query for insert Base64 into DB
  53.         string query = "INSERT INTO STORAGE (IMG_NAME, IMG_SOURCE) VALUES ('" + file.get_basename () + "','" + image + "')";
  54.  
  55.         string errmsg;
  56.        
  57.         // Excecute Query
  58.         ec = db.exec (query, null, out errmsg);
  59.         if (ec != Sqlite.OK) {
  60.             warning ("Error: %s\n", errmsg);
  61.         }
  62.     } catch (GLib.Error e) {
  63.         warning ("Unable to encrypt file: " + e.message);
  64.         return 1;
  65.     }
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement