- QSqlDatabase db =QSqlDatabase::addDatabase("QMYSQL"/*"QODBC"*/); // becomes the new default connection
- db.setUserName("root");//用户名
- db.setPassword("password");//密码
- db.setHostName("localhost");
- db.setDatabaseName("test");//数据库名
- db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1");//使用SSL
- db.setPort(3306);//端口
- if(db.open()){
- qDebug()<<"open\\n"<<db.lastError().driverText()<<"\\n";
- }
- else{
- qDebug()<<"open faile\\n";
- }
- //该片段来自于http://www.codesnippet.cn/detail/040120131265.html
来源: http://www.codesnippet.cn/detail/040120131265.html