QT SQLite“无查询无法获取行”

时间:2013-07-01 15:31:13

标签: c++ qt sqlite

我有这个类DBManager在Qt中处理SQlite数据库 问题是有时候Query返回“No query Unable to fetch row”并且没有返回任何内容,即使数据库中有记录,有时候查询工作完全正常 任何人都可以帮助解决这个问题吗?

这是班级:

class DatabaseManager
{
private:
    QSqlDatabase PatternLibrary;
    QSqlQuery query;


public:
    Pattern pattern;
    std::vector<Pattern> allPatterns;
    std::vector<QString> patternNames;
    std::vector<RECTANGLE> rectangles_;
    DatabaseManager()
    {
        PatternLibrary = QSqlDatabase::addDatabase( "QSQLITE" );
        PatternLibrary.setDatabaseName("mydb");
        bool opened = PatternLibrary.open();
        query = QSqlQuery(PatternLibrary);
        if(opened)
        {
            qDebug( "Connected!");
            query.prepare( "CREATE TABLE IF NOT EXISTS Pattern (patternName         VARCHAR(30),RectangleID INTEGER ,X1 INTEGER, Y1 INTEGER, X2 INTEGER, Y2 INTEGER,X3     INTEGER,Y3 INTEGER, X4 INTEGER, Y4 INTEGER,PRIMARY KEY (patternName,RectangleID))" );
            bool create = query.exec();
            if(!create)
            {
                qDebug() <<PatternLibrary.lastError().text();
            }

        }
        else
        {
            qDebug() << PatternLibrary.lastError().text();
            qFatal( "Failed to connect." );
        }
        query.clear();
    }

//******************************************************************************//
 bool select_all_records()
    {
        query = QSqlQuery(PatternLibrary);
        query.clear();
        if(PatternLibrary.open())
        {
            query.prepare( "SELECT* FROM Pattern");
            if(!query.exec())
            {
                qDebug() << query.lastError().text();
                query.clear();
                return false;
            }
            else
            {
                int PatternCount=0;
                QString temp1="",temp2="";
                patternNames.clear();
                while( query.next() )
                {
                    temp1 = query.value(0).toString();
                    if(temp1 !=temp2)
                    {
                        PatternCount++;
                        temp2 = temp1;
                        patternNames.push_back(temp1);
                    }
                    RECTANGLE Rect;
                    Rect.patternName = query.value(0).toString();
                    Rect.rectangleId = query.value(1).toInt();
                    Rect.X1 = query.value(2).toInt();
                    Rect.Y1 = query.value(3).toInt();
                    Rect.X2 = query.value(4).toInt();
                    Rect.Y2 = query.value(5).toInt();
                    Rect.X3 = query.value(6).toInt();
                    Rect.Y3 = query.value(7).toInt();
                    Rect.X4 = query.value(8).toInt();
                    Rect.Y4 = query.value(9).toInt();
                    rectangles_.push_back(Rect);
                }
                for(unsigned int i =0;i<patternNames.size();i++)
                {
                    Pattern newPattern;
                    newPattern.PatternName = patternNames.at(i);
                    for(unsigned int j =0;j<rectangles_.size();j++)
                    {
                        if(rectangles_[j].patternName == newPattern.PatternName)
                        {
                            newPattern.pattern_.push_back(rectangles_[j]);
                            rectangles_.erase(rectangles_.begin()+j);
                            j--;
                        }

                    }
                    allPatterns.push_back(newPattern);
                }
                query.clear();
                return true;
            }
        }
        query.clear();
        qFatal( "Can't Open database" );
        return false;
    }
//************************************************************//

这是我的主要内容:

int main()
{
    DatabaseManager *mydbmanager = new DatabaseManager();
    bool b = mydbmanager-> select_all_records();  //returns false and "No query Unable to fetch row"
}

0 个答案:

没有答案