1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <QtGui/QApplication> #include <QtSql> #include <QMessageBox> #include "studentmanage.h"
bool createConnection() { QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("DB_Stu"); db.setUserName("root"); db.setPassword("123456"); if (!db.open()) { QMessageBox::warning(0, QObject::tr("Database Error"), db.lastError().text()); return false; } return true; }
void createData() { QSqlQuery query; query.exec("Drop table student"); query.exec("Create table student (" "Sno int not null," "Name varchar(40) not null," "Age int not null," "Math int not null," "Chinese int not null," "English int not null)"); query.exec("Insert into student (Sno,Name,Age,Math,Chinese,English) values(1,'Dear_fox',1,100,100,100)"); query.exec("Insert into student (Sno,Name,Age,Math,Chinese,English) values(2,'Killua',2,99,98,98)"); }
int main(int argc, char *argv[]) { QApplication a(argc, argv); bool isCreate = !QFile::exists("DB_Stu"); if (!createConnection()) return 1; if(isCreate) createData(); StudentManage studentManage; studentManage.resize(400,300); studentManage.show(); return a.exec(); }
|