Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Jordi Puigdellívol
- Marlin Tagging system
- libsqlite3-dev
- create table tags (uri TEXT, color INT, tags TEXT);
- valac --pkg sqlite3 -o sqlitesample SqliteSample.vala
- */
- using GLib;
- using Sqlite;
- public class MarlinTags : GLib.Object {
- private Database db;
- 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 bool openDB(string dbName){
- if (!FileUtils.test (dbName, FileTest.IS_REGULAR)) {
- stderr.printf ("Database %s does not exist or is directory\n", dbName);
- return false;
- }
- int rc = Database.open (dbName, out db);
- if (rc != Sqlite.OK) {
- stderr.printf ("Can't open database: %d, %s\n", rc, db.errmsg ());
- return false;
- }
- return true;
- }
- public bool addColor(string uri, int color){
- //TODO: Find if the uri already exists in database, if so, then update
- //TODO: Convert int to string
- string c = "insert into tags(uri,color) values ("+uri+",0)";
- int rc = db.exec (c, null, null);
- if (rc != Sqlite.OK) {
- stderr.printf ("[addColor: SQL error] %d, %s\n", rc, db.errmsg ());
- return false;
- }
- return true;
- }
- public int getColor(string uri)
- {
- string c = "select color from tags where uri='" + uri + "'";
- Statement stmt;
- int rc = 0;
- int col, cols;
- string txt = "-1";
- if ((rc = db.prepare_v2 (c, -1, out stmt, null)) == 1) {
- printerr ("SQL error: %d, %s\n", rc, db.errmsg ());
- return -1;
- }
- cols = stmt.column_count();
- do {
- rc = stmt.step();
- switch (rc) {
- case Sqlite.DONE:
- break;
- case Sqlite.ROW:
- for (col = 0; col < cols; col++) {
- txt = stmt.column_text(col);
- //print ("%s = %s\n", stmt.column_name (col), txt);
- }
- break;
- default:
- printerr ("Error: %d, %s\n", rc, db.errmsg ());
- break;
- }
- } while (rc == Sqlite.ROW);
- return (int)txt.to_long();
- }
- public bool showTable(string table){
- string consult = "select * from " + table;
- int rc = db.exec (consult, callback, null);
- if (rc != Sqlite.OK) {
- stderr.printf ("[showTable: SQL error]: %d, %s\n", rc, db.errmsg ());
- return false;
- }
- return true;
- }
- /*Main*/
- public static int main (string[] args) {
- MarlinTags t = new MarlinTags();
- //The database must exists, add here the full path
- string marlinDB = "testdb";
- //string consult = "select * from tags";
- t.openDB(marlinDB);
- //t.addColor("/home/diana",0);
- //t.showTable("tags");
- print("Color for file is %i\n", t.getColor("/home/jordi"));
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement