Home > Back-end >  QT connection new table SQL data and written to the database failed
QT connection new table SQL data and written to the database failed

Time:09-20

Just learning qt today connection new table SQL data and written to the database failed
Code the following yu is the name of the database user name and password are the test
 QSqlDatabase db=QSqlDatabase: : addDatabase (" QSQLITE "); 
The setHostName (" localhost ");
Db. SetDatabaseName (" yu ");
The setUserName (" test ");
The setPassword (" test ");
Bool ok=db. The open ();
If (ok) {
QMessageBox msgBox;
MsgBox. SetText (" ok ");
MsgBox. The exec ();
}
The else {
QMessageBox msgBox;
MsgBox. SetText (" Error connect database!" + db. LastError (). The text ());
MsgBox. The exec ();
}
QSqlQuery query;//the following relevant QSL statement
Query. The exec (" create table student (id int primary key, name varchar) ");
//the new student table, item id set the primary key, and a name
Query. The exec (" insert into student values (1, "xiaogang") ");
Query. The exec (" insert into student values (2, 'xiaoming') ");
Query. The exec (" insert into student values (3, "xiaohong") ");

CodePudding user response:

Friendship jacking, I this in learning QT
  • Related