1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
4 | **
|
---|
5 | ** This file is part of an example program for Qt. This example
|
---|
6 | ** program may be used, distributed and modified without limitation.
|
---|
7 | **
|
---|
8 | *****************************************************************************/
|
---|
9 |
|
---|
10 | #include <qapplication.h>
|
---|
11 | #include <qsqldatabase.h>
|
---|
12 | #include <qsqlquery.h>
|
---|
13 | #include <qsqlcursor.h>
|
---|
14 | #include <qfile.h>
|
---|
15 |
|
---|
16 | #define DRIVER "QPSQL7" /* see the Qt SQL documentation for a list of available drivers */
|
---|
17 | #define DATABASE "" /* the name of your database */
|
---|
18 | #define USER "" /* user name with appropriate rights */
|
---|
19 | #define PASSWORD "" /* password for USER */
|
---|
20 | #define HOST "" /* host on which the database is running */
|
---|
21 |
|
---|
22 | int main( int argc, char ** argv )
|
---|
23 | {
|
---|
24 |
|
---|
25 | QApplication a( argc, argv, FALSE );
|
---|
26 | QSqlDatabase * db = QSqlDatabase::addDatabase( DRIVER );
|
---|
27 | db->setDatabaseName( DATABASE );
|
---|
28 | db->setUserName( USER );
|
---|
29 | db->setPassword( PASSWORD );
|
---|
30 | db->setHostName( HOST );
|
---|
31 | if ( !db->open() ) {
|
---|
32 | qWarning( db->lastError().databaseText() );
|
---|
33 | return 1;
|
---|
34 | }
|
---|
35 |
|
---|
36 | if ( argc < 2 ) {
|
---|
37 | qWarning( "Usage: %s <filename>", argv[0] );
|
---|
38 | return 1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | // read a file which we want to insert into the database
|
---|
42 | QFile f( argv[1] );
|
---|
43 | if ( !f.open( IO_ReadOnly ) ) {
|
---|
44 | qWarning( "Unable to open data file '%s' - exiting", argv[1] );
|
---|
45 | return 1;
|
---|
46 | }
|
---|
47 | QByteArray binaryData = f.readAll();
|
---|
48 | qWarning( "Data size: %d", binaryData.size() );
|
---|
49 |
|
---|
50 | // create a table with a binary field
|
---|
51 | QSqlQuery q;
|
---|
52 | if ( !q.exec( "CREATE TABLE blobexample ( id INT PRIMARY KEY, binfield LONGBLOB )" ) ) {
|
---|
53 | qWarning( "Unable to create table - exiting" );
|
---|
54 | return 1;
|
---|
55 | }
|
---|
56 |
|
---|
57 | // insert a BLOB into the table
|
---|
58 | if ( !q.prepare( "INSERT INTO blobexample ( id, binfield ) VALUES ( ?, ? )" ) ) {
|
---|
59 | qWarning( "Unable to prepare query - exiting" );
|
---|
60 | return 1;
|
---|
61 | }
|
---|
62 | q.bindValue( 0, 1 );
|
---|
63 | q.bindValue( 1, binaryData );
|
---|
64 | if ( !q.exec() ) {
|
---|
65 | qWarning( "Unable to execute prepared query - exiting" );
|
---|
66 | return 1;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // read the BLOB back from the database
|
---|
70 | if ( !q.exec( "SELECT id, binfield FROM blobexample" ) ) {
|
---|
71 | qWarning( "Unable to execute query - exiting" );
|
---|
72 | return 1;
|
---|
73 | }
|
---|
74 | qWarning( "\nQSqlQuery:" );
|
---|
75 | while ( q.next() ) {
|
---|
76 | qWarning( "BLOB id: %d", q.value( 0 ).toInt() );
|
---|
77 | qWarning( "BLOB size: %d", q.value( 1 ).toByteArray().size() );
|
---|
78 | }
|
---|
79 |
|
---|
80 | // write another BLOB using QSqlCursor
|
---|
81 | QSqlCursor cur( "blobexample" );
|
---|
82 | QSqlRecord * r = cur.primeInsert();
|
---|
83 | r->setValue( "id", 2 );
|
---|
84 | r->setValue( "binfield", binaryData );
|
---|
85 | if ( !cur.insert() ) {
|
---|
86 | qWarning( "Unable to insert BLOB using QSqlCursor - exiting" );
|
---|
87 | return 1;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // read the BLOBs back using QSqlCursor
|
---|
91 | if ( !cur.select() ) {
|
---|
92 | qWarning( "Unable retrieve blobexample table using QSqlCursor - exiting" );
|
---|
93 | return 1;
|
---|
94 | }
|
---|
95 | qWarning( "\nQSqlCursor:" );
|
---|
96 | while ( cur.next() ) {
|
---|
97 | qWarning( "BLOB id: %d", cur.value( "id" ).toInt() );
|
---|
98 | qWarning( "BLOB size: %d", cur.value( "binfield" ).toByteArray().size() );
|
---|
99 | }
|
---|
100 |
|
---|
101 | if ( !q.exec( "DROP TABLE blobexample" ) ) {
|
---|
102 | qWarning( "Unable to drop table - exiting" );
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 | return 0;
|
---|
106 | }
|
---|