diff options
author | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-11-08 12:31:36 -0600 |
---|---|---|
committer | Timothy Pearson <kb9vqf@pearsoncomputing.net> | 2011-11-08 12:31:36 -0600 |
commit | d796c9dd933ab96ec83b9a634feedd5d32e1ba3f (patch) | |
tree | 6e3dcca4f77e20ec8966c666aac7c35bd4704053 /examples/sql/blob | |
download | tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.tar.gz tqt3-d796c9dd933ab96ec83b9a634feedd5d32e1ba3f.zip |
Test conversion to TQt3 from Qt3 8c6fc1f8e35fd264dd01c582ca5e7549b32ab731
Diffstat (limited to 'examples/sql/blob')
-rw-r--r-- | examples/sql/blob/blob.pro | 11 | ||||
-rw-r--r-- | examples/sql/blob/main.cpp | 106 |
2 files changed, 117 insertions, 0 deletions
diff --git a/examples/sql/blob/blob.pro b/examples/sql/blob/blob.pro new file mode 100644 index 000000000..9240f2e42 --- /dev/null +++ b/examples/sql/blob/blob.pro @@ -0,0 +1,11 @@ +TEMPLATE = app +TARGET = blob + +CONFIG += qt warn_on release +win32:CONFIG += console + +REQUIRES = full-config + +HEADERS = +SOURCES = main.cpp +INTERFACES = diff --git a/examples/sql/blob/main.cpp b/examples/sql/blob/main.cpp new file mode 100644 index 000000000..e66e9aaf5 --- /dev/null +++ b/examples/sql/blob/main.cpp @@ -0,0 +1,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 <qapplication.h> +#include <qsqldatabase.h> +#include <qsqlquery.h> +#include <qsqlcursor.h> +#include <qfile.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() ) { + qWarning( db->lastError().databaseText() ); + return 1; + } + + if ( argc < 2 ) { + qWarning( "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 ) ) { + qWarning( "Unable to open data file '%s' - exiting", argv[1] ); + return 1; + } + TQByteArray binaryData = f.readAll(); + qWarning( "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 )" ) ) { + qWarning( "Unable to create table - exiting" ); + return 1; + } + + // insert a BLOB into the table + if ( !q.prepare( "INSERT INTO blobexample ( id, binfield ) VALUES ( ?, ? )" ) ) { + qWarning( "Unable to prepare query - exiting" ); + return 1; + } + q.bindValue( 0, 1 ); + q.bindValue( 1, binaryData ); + if ( !q.exec() ) { + qWarning( "Unable to execute prepared query - exiting" ); + return 1; + } + + // read the BLOB back from the database + if ( !q.exec( "SELECT id, binfield FROM blobexample" ) ) { + qWarning( "Unable to execute query - exiting" ); + return 1; + } + qWarning( "\nTQSqlQuery:" ); + while ( q.next() ) { + qWarning( "BLOB id: %d", q.value( 0 ).toInt() ); + qWarning( "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() ) { + qWarning( "Unable to insert BLOB using TQSqlCursor - exiting" ); + return 1; + } + + // read the BLOBs back using TQSqlCursor + if ( !cur.select() ) { + qWarning( "Unable retrieve blobexample table using TQSqlCursor - exiting" ); + return 1; + } + qWarning( "\nTQSqlCursor:" ); + while ( cur.next() ) { + qWarning( "BLOB id: %d", cur.value( "id" ).toInt() ); + qWarning( "BLOB size: %d", cur.value( "binfield" ).toByteArray().size() ); + } + + if ( !q.exec( "DROP TABLE blobexample" ) ) { + qWarning( "Unable to drop table - exiting" ); + return 1; + } + return 0; +} |