欢迎来到天天文库
浏览记录
ID:46392392
大小:34.01 KB
页数:3页
时间:2019-11-23
《qt连接数据库(MySQL)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、[代码][C/C++/Objective-C]代码QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL"/*"QODBC"*/);//becomesthenewdefaultconnectiondb.setUserName("root");//用户名db.setPassword("password");//密码db.setHostName("localhost");db.setDatabaseName("test");//数据库名db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_
2、SPACE=1");//使用SSLdb.setPort(3306);//端口if(db.open()){qDebug()<<"open"<#include//连接函数boolcreateConnection(){QSqlDatabasedb=QSqlDatabase::addDatabase("QMYSQL");db.setHostName("localhost");db.setData
3、baseName("test");db.setUserName("root");db.setPassword("123456");if(!db.open()){QMessageBox::critical(0,QObject::tr("DatabaseError"),db.lastError().text());returnfalse;}//如果连接成功,执行查询QSqlQueryquery;query.exec("select*fromstudent");while(query.next()){QStringname=query.value(3).toString();
4、QStringmajor=query.value(1).toString();QMessageBox::critical(0,name,major);}returntrue;}//主函数intmain(intargc,char**argv){QApplicationapp(argc,argv);if(!createConnection()){return1;}returnapp.exec();}实例-2求Qt的mysql驱动libqsqlmysql.a,qsqlmysql.dll#include#include#include
5、boolconnectDatabase(){QSqlDatabaseconn=QSqlDatabase::addDatabase("QMYSQL");conn.setHostName("localhost");conn.setUserName("root");conn.setPassword("123456");conn.setDatabaseName("test");if(!conn.open()){QMessageBox::critical(0,QLabel::trUtf8("数据库连接失败"),conn.lastError().text());returnfals
6、e;}returntrue;}intmain(intargc,char**argv){QApplicationapp(argc,argv);if(!connectDatabase())return1;returnapp.exec();}
此文档下载收益归作者所有