如何将图片存入数据库中

来源:互联网 发布:北京软件中关村培训班 编辑:程序博客网 时间:2024/05/15 19:12

 

首先创建数据库

QSqlQuery query;

query->exec(QObject::tr("create table order_select (icon BLOB)"));

接下来,取图片并存入数据库中

QDir dir(":/images/");
        if ( !dir.exists() )
                qWarning( "Cannot find the example directory" );

        picFile = dir.entryList(QDir::Files/*,QDir::Name*/);
        QString temp;
        for(int i = 0;i <= picFile.count() - 1 ; ++i)
        {
            temp = picFile.at(i);
            temp = temp.remove(0,temp.lastIndexOf('.') + 1);
            if(temp == "png")
            {
                path << QObject::tr(":/images/%1").arg(picFile.at(i));
            }
        }
        for(int i=0;i<path.count();i++)
        {
            QByteArray ba;
            QBuffer buffer(&ba);
            buffer.open(QIODevice::ReadWrite);
            QPixmap(path[i]).save(&buffer,"PNG");
                        //这里默认传递的是NULL指针,导致save返回false。
            QVariant variant(ba);

           QSqlQuery  query1;
            query1.exec(QObject::tr("insert into order_select values (?)"));///存入数据库
            query1.bindValue(0, variant);
            query1.exec();
        }

原创粉丝点击