summaryrefslogtreecommitdiffstats
path: root/examples/sql/blob/main.cpp
blob: 0144ad7779038f319391afee4b481b9bbc069455 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <ntqapplication.h>
#include <tqsqldatabase.h>
#include <tqsqlquery.h>
#include <tqsqlcursor.h>
#include <ntqfile.h>

#define DRIVER       "TQPSQL7" /* see the TQt SQL documentation for a list of available drivers */
#define DATABASE     "" /* the name of your database */
#define USER         "" /* user name with appropriate rights */
#define PASSWORD     ""	/* password for USER */
#define HOST         "" /* host on which the database is running */

int main( int argc, char ** argv )
{

    TQApplication a( argc, argv, FALSE );
    TQSqlDatabase * db = TQSqlDatabase::addDatabase( DRIVER );
    db->setDatabaseName( DATABASE );
    db->setUserName( USER );
    db->setPassword( PASSWORD );
    db->setHostName( HOST );
    if ( !db->open() ) {
	tqWarning( db->lastError().databaseText() );
	return 1;
    }

    if ( argc < 2 ) {
	tqWarning( "Usage: %s <filename>", argv[0] );
	return 1;
    }
    
    // read a file which we want to insert into the database
    TQFile f( argv[1] );
    if ( !f.open( IO_ReadOnly ) ) {
	tqWarning( "Unable to open data file '%s' - exiting", argv[1] );
	return 1;
    }
    TQByteArray binaryData = f.readAll();
    tqWarning( "Data size: %d", binaryData.size() );
    
    // create a table with a binary field
    TQSqlQuery q;
    if ( !q.exec( "CREATE TABLE blobexample ( id INT PRIMARY KEY, binfield LONGBLOB )" ) ) {
	tqWarning( "Unable to create table - exiting" );
	return 1;
    }

    // insert a BLOB into the table
    if ( !q.prepare( "INSERT INTO blobexample ( id, binfield ) VALUES ( ?, ? )" ) ) {
	tqWarning( "Unable to prepare query - exiting" );
	return 1;
    }
    q.bindValue( 0, 1 );
    q.bindValue( 1, binaryData );
    if ( !q.exec() ) {
	tqWarning( "Unable to execute prepared query - exiting" );
	return 1;
    }

    // read the BLOB back from the database
    if ( !q.exec( "SELECT id, binfield FROM blobexample" ) ) {
	tqWarning( "Unable to execute query - exiting" );
	return 1;
    }
    tqWarning( "\nTQSqlQuery:" );
    while ( q.next() ) {
	tqWarning( "BLOB id: %d", q.value( 0 ).toInt() );
	tqWarning( "BLOB size: %d", q.value( 1 ).toByteArray().size() );
    }

    // write another BLOB using TQSqlCursor
    TQSqlCursor cur( "blobexample" );
    TQSqlRecord * r = cur.primeInsert();
    r->setValue( "id", 2 );
    r->setValue( "binfield", binaryData );
    if ( !cur.insert() ) {
	tqWarning( "Unable to insert BLOB using TQSqlCursor - exiting" );
	return 1;
    }

    // read the BLOBs back using TQSqlCursor
    if ( !cur.select() ) {
	tqWarning( "Unable retrieve blobexample table using TQSqlCursor - exiting" );
	return 1;
    }
    tqWarning( "\nTQSqlCursor:" );
    while ( cur.next() ) {
	tqWarning( "BLOB id: %d", cur.value( "id" ).toInt() );
	tqWarning( "BLOB size: %d", cur.value( "binfield" ).toByteArray().size() );
    }
        
    if ( !q.exec( "DROP TABLE blobexample" ) ) {
	tqWarning( "Unable to drop table - exiting" );
	return 1;
    }
    return 0;
}