Qt
Aus Mein MediaWiki
MySQL
//--- use a QTextStream: it makes it easier to output ---
//--- QString to cout. ---
QTextStream cout(stdout, QIODevice::WriteOnly);
//--- define the database connection ---
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost"); // host
db.setDatabaseName("qttest"); // database
db.setUserName("qtuser"); // user
db.setPassword("qtuser"); // password
//--- attempt to open it ---
bool ok = db.open();
if ( ok ) {
//--- we're good! ---
cout << "Database open\n";
//--- run a query and print data returned ---
QSqlQuery query( "select * from address" );
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 address (fname, lname) VALUES ( :word, :word ) ");
query.bindValue( ":word", "Banana" );
query.exec();
//--- close connection to database
db.close();
}
else
//--- something went wrong ---
cout << "Error opening database\n";
Quelle: http://cs.smith.edu/dftwiki/index.php/Qt4/Qt-Creator_Read_MySql_Table_%28Console_Mode%29
QT MySQL driver
Unter Ubuntu muss das Paket libqt4-sql-mysql mit sudo apt-get install libqt4-sql-mysql installiert werden.
