我做好了一个登陆框,可以正常登录,是在程序中判断是不是合法的用户名密码。现在想用数据库来替代,但是遇到了问题。
我想在第一次使用时程序应该能够初始化一个管理员的用户名和密码,等到第二次就不用再初始化了。因此构造函数为
loginDialog::loginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::loginDialog)
{
ui->setupUi(this);
ui->passwordText->setEchoMode(QLineEdit::Password);
ui->userNameText->setFocus();
QWidget::setWindowIcon(QIcon("../pictures/icon/images.png"));
QWidget::setWindowTitle(QString::fromLocal8Bit("请登录"));
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("../data/user.db");
QSqlQuery query;
query.exec(QObject::tr("create table users(name vchar,password vchar,passwdChanged int,loginTimes int)"));
query.exec("select * from users");
int passwdChanged = query.value(2).toInt();
int loginTimes = query.value(3).toInt();
if(passwdChanged != 1 && loginTimes != 1)
query.exec(QObject::tr("insert into users values ('管理员','iloveDB',1,1)"));//初始化管理员密码及登陆属性
}
---------------------------------------------
运行时qt提示
Starting G:\QTProject\DBProject\debug\DBProject.exe...
QSqlQuery::exec: database not open
QSqlQuery::exec: database not open
QSqlQuery::value: not positioned on a valid record
QSqlQuery::value: not positioned on a valid record
不知我哪里有问题?
[ 此帖被yzpdsg在2010-05-22 08:51重新编辑 ]