query.exec报QSqlQuery::exec: database not open

DataBase::DataBase()
{
    int ret = 0;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("scooters.dat");
    if (!db.open())
    {
        qDebug() << "db.open error" << endl;
        return;
    QSqlQuery query(db);  //开始不加这句运行时一直报错
    //QSqlQuery: 帮助使用数据库数据库db构造qsqlquery对象。如果db无效,将使用应用程序的默认数据库。
    ret = query.exec("CREATE TABLE students (name VARCHAR(10) PRIMARY KEY NOT NULL, vaule VARCHAR(60) NOT NULL)");
    ret = query.exec("INSERT INTO students (name, vaule) VALUES ('clientid', 'efff@aaaaadsad|qdafa/sdfa')");
    query.exec("SELECT * FROM students WHERE name = 'clientid';");
    while(query.next())
    {
        QString name = query.value(0).toString();
        QString vaule = query.value(1).toString();

        qDebug()<< name << " : " << vaule << endl;;
    }
}

你可能感兴趣的:(Qt)