1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
Libgda Blobs example
====================
Description:
------------
The example in this directory illustrate how to manipulate BLOBS (binary large objects).
This example uses an SQLite database, but can be modified to use any type of database,
in this case you have to create the appropriate test table in your database, and modify the
code of the open_connection() function.
The SQLite SQL to create the test table is:
CREATE TABLE blobstable (id INTEGER PRIMARY KEY AUTOINCREMENT, data blob);
The PostgreSQL SQL to create the test table is (the "WITH OIDS" clause is optionnal
but it's required to get the inserted row):
CREATE TABLE blobstable (id serial PRIMARY KEY, data oid) WITH OIDS;
This test program offers 3 operations which are:
* to store the contents of a file to the database (which returns the ID of the stored data)
* to list stored blobs
* to fetch a stored data from the database from its ID (which creates a fetched_<ID> file).
This program also shows that transactions are created when reading BLOBs, and that these
transactions need to be rolled back after the BLOB "handle" (data model and/or GValue) has been released.
Compiling and running:
----------------------
To compile (make sure Libgda is installed prior to this):
> make
or, if cross compiling for Windows from a Linux box (the Makefile.cross.win32 needs to be adapted to specify the path where the Windows binaries are):
> make -f Makefile.cross.win32
and to run (stores the blobtest executable file):
> ./blobtest store blobtest
Before writing BLOB: No transaction started
STORING file 'blobtest' to database BLOB
After writing BLOB: Transaction started
Inserted row is (for each numbered column in the table):
[+0] = [1]
[+1] = [\177ELF\001\001\001\000\000\000\000\000\000\000\000\000\002\000\003\000\001\000\000\000\020\216\004\0104\000\000\000\2542\000\000\000\000\000\000]
After rolling back blob READ transaction: No transaction started
Ok.
> ./blobtest list
id | data
---+-----------------------------------------------------------------------------------------------------------------------------------------------
1 | \177ELF\001\001\001\000\000\000\000\000\000\000\000\000\002\000\003\000\001\000\000\000\320\216\004\0104\000\000\000<9\000\000\000\000\000\000
(1 row)
Ok.
Still in a transaction, all modifications will be lost when connection is closed
> ./blobtest fetch 1
Before reading BLOB: No transaction started
FETCHING BLOB with ID 1 to file 'fetched_1'
After reading BLOB: Transaction started
After rolling back blob READ transaction: No transaction started
Ok.
> cmp blobtest fetched_1
Should not return any difference
|