//输出可用数据库
qDebug()<<"available drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers)
qDebug()<<driver;
//打开MySQL
QSqlDatabase data_base = QSqlDatabase::addDatabase("QMYSQL");
data_base.setHostName("10.0.31.41"); //设置主机地址
data_base.setPort(3309); //设置端口
data_base.setDatabaseName("gv_local"); //设置数据库名称
data_base.setUserName("root"); //设置用户名
data_base.setPassword("root123"); //设置密码
if(!data_base.open())
qDebug()<<"failed to connect to mysql";
else
qDebug()<<"success";
QString select_all_sql = "select * from gv_alarm_info";
//查询所有数据
QSqlQuery sql_query;
sql_query.prepare(select_all_sql);
if(!sql_query.exec())
{
qDebug()<<sql_query.lastError();
}
else
{
while(sql_query.next())
{
int id = sql_query.value(0).toInt();
QString templ_name = sql_query.value(2).toString();
qDebug()<<QString("Id:%1 Templ Name:%2").arg(id).arg(templ_name);
}
}