Implementation of using Qt to store images to databases

Source: Internet
Author: User

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.

 
 
  1. // Connect to the database
  2. QSqlDatabase db = QSqlDatabase: addDatabase ("QOCI ");
  3. // QSqlDatabase db = QSqlDatabase: addDatabase ("QODBC"); // cannot connect
  4. Db. setHostName ("192.168.0.123 ");
  5. Db. setDatabaseName ("ORCL ");
  6. Db. setUserName ("testuser ");
  7. Db. setPassword ("admin ");
  8. Db. setPort (1521 );
  9.  
  10. If (! Db. open ())
  11. {
  12. Printf ("failed to open n ");
  13. }
  14. Else
  15. {
  16. Printf ("opened successfully n ");
  17.  
  18. QSqlQuery query;
  19.  
  20. // Do insert
  21.  
  22. // Blob
  23. QByteArray data;
  24. QString path = "F:/FtpSet/Ftp02/1111111111.JPG ";
  25. QFile * file = new QFile (path); // fileName is the binary data file name.
  26. File-> open (QIODevice: ReadOnly );
  27. Data = file-> readAll ();
  28. File-> close ();
  29. QVariant var (data );
  30.  
  31. QString SQL = "insert into qttest (id, field_blob) values (?,?) ";
  32. Query. prepare (SQL );
  33. Query. addBindValue (100 );
  34. Query. addBindValue (var );
  35. If (! Query.exe c ())
  36. {
  37. Printf ("[insert error]");
  38. Printf (query. lastError (). text (). toLocal8Bit (). data ());
  39. Printf ("n ");
  40. }
  41. Else
  42. {
  43. Printf ("[insert OK] n ");
  44. }

Another way to get BLOB data input parameters is to run normally in Mysql and in Oracle. The Code is as follows:

 
 
  1. QByteArray ba;  
  2. QBuffer buffer(&ba);  
  3. buffer.open(QIODevice::ReadWrite);  
  4. QPixmap pixmap(path);  
  5. pixmap.save(&buffer,"JPG");  
  6. QVariant variant(ba); 

Summary:QtStorageImageToDatabaseThe implementation of the case is complete. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.