#include <QCoreApplication>
#include <QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QDebug>
#include <QVariant>
#include <Qtsql/QtSql>
#include <qtextstream.h>
//as usual ..
#include <iostream>
int main(int argc, char *argv[])
{ QTextStream cout ( stdout , QIODevice :: WriteOnly );
//creating a database connection
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("해당 주소");
db.setUserName("loy124");
db.setPassword("");
db.setDatabaseName("loy124");
// lets tset the connection
bool ok = db.open();
if ( ok ) {
//--- we're good! ---
cout << "Database open\n";
//--- run a query and print data returned ---
QSqlQuery query( "select * from GPS" );//테이블이름을 지정
if ( !query.isActive() )
cout << "Query Error" + query.lastError().text()
<< endl;
else while (query.next()) {
int Id = query.value(0).toInt();
QString word = query.value(1).toString();
cout << QString( "%1\t%2\n").arg( Id).arg( word );
}
//--- add a new entry to the table ---
query.prepare( "INSERT INTO table1 (Word) VALUES ( :latitude ) ");//테이블내 필드 검색
query.bindValue( ":latitude", "" );
query.exec();
//--- close connection to database
db.close();
}
else
//--- something went wrong ---
cout << "Error opening database\n";
return 0;
}
mySQL에 접근해서 테이블값을 불러오는 코드를 생성해보았다. 역시 구글링하다보니 안나오는건 없는듯..
참조: http://www.science.smith.edu/dftwiki/index.php/Qt4/Qt-Creator_Read_MySql_Table_(Console_Mode)