SQLite realizes user login function
void login::on_LoginToMain_clicked() { //The login button needs to determine whether the user name and password correspond correctly //If there is an error, a prompt dialog box will pop up //-------------------------------------- //Use SQlite to compare user name and password QSqlDatabase db;//Using QSlite driver if(QSqlDatabase::contains("qt_sql_default_connection")) { db = QSqlDatabase::database("qt_sql_default_connection"); } else { db = QSqlDatabase::addDatabase("QSQLITE"); } db.setDatabaseName("D:\\QtApplication\\library1-0\\db\\userdata.db");//Previously established database bool flag=db.open();//Judge whether to open if(flag) { QSqlQuery query;//New query instance //If the database is opened successfully, an associated QSqlQuery object will be created, and then QSqlQuery will perform the data insertion. //Special note: QSqlQuery object must be created after the db file is successfully open ed, otherwise an error will occur. if(query.exec("select * from member")) { //Here is to access all data qDebug()<<"Read successful"; //Define the input content of two text boxes in the login interface int uname=ui->userName->text().toInt(); QString password=ui->password->text(); qDebug()<<"create success"; if(ui->userName->text()==NULL&&ui->password->text()==NULL) { //If the input is empty QMessageBox::warning(this,QString(tr("Waring")), QString(tr("Input cannot be empty! Please re-enter!"))); } else { //When the input is not empty int nameno=0;//Used to locate the number of rows at seek, index while(query.next())//Loop to find { qDebug()<<"searchsuccess"; if(uname==query.value(1).toInt())//Here value (1) is the column with student number { //value(int n): gets the value of the attribute. Where n represents the nth attribute of your query, //For example, using "select * from student" is equivalent to //"select id, name from student", then value(0) returns the value of ID attribute, value(1) returns the value of name attribute nameno=query.value(0).toInt();//Here value (0) is the column with sequence number } else { qDebug()<<"none"; } } if(query.seek(nameno)) { //seek(int n): query points to the nth record in the result set. Locate the line corresponding to the user name (student number) just found and find the password for comparison qDebug()<<"select success"; QString spassword=query.value(2).toString();//Read the password stored in this line qDebug()<<uname<<spassword; if(spassword==password) { //If the passwords match, skip to the main interface QMessageBox::information(NULL,QString(tr(" ")), QString(tr("Login successful"))); qDebug()<<"login success"; accept(); } else {//User name and password do not correspond QMessageBox::warning(this,QString(tr("Waring")), QString(tr("Wrong user name or password! Please re-enter!"))); ui->userName->clear();//Automatically clear user name and password ui->password->clear(); ui->userName->setFocus();//Positioning cursor } } } } }}
Note here that do not use
query.prepare("select * from member");
The specific reasons are to be continued. The specific functions of prepare and exec are different! o_O
-
prepare()
This function converts the SQL text into a prepared statement object and returns a pointer to the object. This interface requires a database connection pointer and a text containing SQL statements to be prepared. It doesn't actually execute the SQL statement, it just prepares the SQL statement for execution
You need to execute step and finalize statements later. exec()
sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err)
db describes the database handle;
[sql UTF8String] the SQL statement to execute, such as "select * from member"
The third parameter is the callback function;
The fourth void * the first parameter of the callback function;
Err & error message, NULL if there is no SQL problem
Use exec() to query. Otherwise, it cannot be queried. member is the name of the table, which can be viewed in the visualizer navicat.
Beginners record, if there are errors, please correct.