// open MySQL QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL3","QPSQL7"); db.setHostName("localhost"); db.setDatabaseName("mydata"); db.setUserName("root"); db.setPassword("123456"); if (!db.open()){ qDebug() << "Failed to connect to root mysql admin"; qDebug() << db.lastError().text(); } else qDebug() << "open";
QSqlQuery query(db);
// attention that varchar should be define length, otherwise error. query.exec("create table student(id int primary key,name varchar(20))");
query.exec("insert into student values(1,'xiaogang')"); query.exec("insert into student values(2,'xiaoming')"); query.exec("insert into student values(3,'xiaohong')");
query.exec("select id,name from student where id >= 2");