QtStorageImageToDatabaseCase implementation is the content to be introduced in this article, mainly to learnQT DatabaseMediumImageThe following is the test code run on the Oracle 10g development platform.
- // Connect to the database
- QSqlDatabase db = QSqlDatabase: addDatabase ("QOCI ");
- // QSqlDatabase db = QSqlDatabase: addDatabase ("QODBC"); // cannot connect
- Db. setHostName ("192.168.0.123 ");
- Db. setDatabaseName ("ORCL ");
- Db. setUserName ("testuser ");
- Db. setPassword ("admin ");
- Db. setPort (1521 );
-
- If (! Db. open ())
- {
- Printf ("failed to open n ");
- }
- Else
- {
- Printf ("opened successfully n ");
-
- QSqlQuery query;
-
- // Do insert
-
- // Blob
- QByteArray data;
- QString path = "F:/FtpSet/Ftp02/1111111111.JPG ";
- QFile * file = new QFile (path); // fileName is the binary data file name.
- File-> open (QIODevice: ReadOnly );
- Data = file-> readAll ();
- File-> close ();
- QVariant var (data );
-
- QString SQL = "insert into qttest (id, field_blob) values (?,?) ";
- Query. prepare (SQL );
- Query. addBindValue (100 );
- Query. addBindValue (var );
- If (! Query.exe c ())
- {
- Printf ("[insert error]");
- Printf (query. lastError (). text (). toLocal8Bit (). data ());
- Printf ("n ");
- }
- Else
- {
- Printf ("[insert OK] n ");
- }
Another way to get BLOB data input parameters is to run normally in Mysql and in Oracle. The Code is as follows:
- QByteArray ba;
- QBuffer buffer(&ba);
- buffer.open(QIODevice::ReadWrite);
- QPixmap pixmap(path);
- pixmap.save(&buffer,"JPG");
- QVariant variant(ba);
Summary:QtStorageImageToDatabaseThe implementation of the case is complete. I hope this article will help you!