Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Required 'db' file next to 'inbound.vala' file
- *
- * Create 'db' file using sqlite3 command and run this commands:
- *
- * .open db
- * CREATE TABLE STORAGE (
- * IMG_ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
- * IMG_NAME TEXT,
- * IMG_SOURCE TEXT
- * );
- * .exit
- *
- * Compile command: valac --pkg gtk+-3.0 --pkg glib-2.0 --pkg sqlite3 inbound.vala
- *
- * Good Luck!
- *
- */
- using Gtk;
- using GLib;
- using Sqlite;
- public static int main (string[] args) {
- try {
- // Read the PNG File and get the data using DataInputStream
- var file = File.new_for_path ("/home/elementary/Proyectos/imageToDB/test.png");
- var stream = new DataInputStream (file.read());
- var info = file.query_info ("*", FileQueryInfoFlags.NONE);
- // Read the data into data var
- int64 file_size = info.get_size ();
- uchar[] data = new uint8[file_size];
- stream.read (data);
- stream.close ();
- stream = null;
- // Encode data to Base64
- string image = GLib.Base64.encode(data);
- //stdout.printf("Bytes: %s\n", image);<-- This is only for debug
- // Open database:
- Sqlite.Database db;
- int ec = Sqlite.Database.open ("db", out db);
- if (ec != Sqlite.OK) {
- warning ("Can't open database: %d: %s\n", db.errcode (), db.errmsg ());
- return 1;
- }
- // Query for insert Base64 into DB
- string query = "INSERT INTO STORAGE (IMG_NAME, IMG_SOURCE) VALUES ('" + file.get_basename () + "','" + image + "')";
- string errmsg;
- // Excecute Query
- ec = db.exec (query, null, out errmsg);
- if (ec != Sqlite.OK) {
- warning ("Error: %s\n", errmsg);
- }
- } catch (GLib.Error e) {
- warning ("Unable to encrypt file: " + e.message);
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement