Difference between revisions of "Qt4/Qt-Creator Read MySql Table (Console Mode)"
(Created page with "--~~~~ ---- {| | width="40%" | __TOC__ | <bluebox> right | 80px <br /> <br /> This tutorial illustrates how to use Qt Creator to write a simply program...") |
(→Steps) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 25: | Line 25: | ||
* Enter the code below in '''main.cpp''': | * Enter the code below in '''main.cpp''': | ||
− | + | <br /> | |
+ | <br /> | ||
<source lang="cpp"> | <source lang="cpp"> | ||
// mySqlSimpleTable project | // mySqlSimpleTable project | ||
Line 35: | Line 36: | ||
// of a table called "table1". | // of a table called "table1". | ||
// The contents of the table is dumped on the console. | // The contents of the table is dumped on the console. | ||
+ | // Good information on Qt and QSqlQueries can be found here: | ||
+ | // http://doc.qt.nokia.com/latest/qsqlquery.html | ||
+ | // Table format: | ||
+ | //CREATE TABLE IF NOT EXISTS `table1` ( | ||
+ | // `Id` int(11) NOT NULL auto_increment, | ||
+ | // `word` varchar(45) default NULL, | ||
+ | // PRIMARY KEY (`Id`) | ||
+ | //) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ; | ||
// | // | ||
Line 48: | Line 57: | ||
//--- QString to cout. --- | //--- QString to cout. --- | ||
QTextStream cout(stdout, QIODevice::WriteOnly); | QTextStream cout(stdout, QIODevice::WriteOnly); | ||
− | + | ||
//--- define the database connection --- | //--- define the database connection --- | ||
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); | QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); | ||
Line 54: | Line 63: | ||
db.setDatabaseName("dummy"); // database | db.setDatabaseName("dummy"); // database | ||
db.setUserName("dummy"); // user | db.setUserName("dummy"); // user | ||
− | db.setPassword(" | + | db.setPassword("dummy"); // password |
− | + | ||
//--- attempt to open it --- | //--- attempt to open it --- | ||
bool ok = db.open(); | bool ok = db.open(); | ||
Line 62: | Line 71: | ||
//--- we're good! --- | //--- we're good! --- | ||
cout << "Database open\n"; | cout << "Database open\n"; | ||
− | + | ||
//--- run a query and print data returned --- | //--- run a query and print data returned --- | ||
QSqlQuery query( "select * from table1" ); | QSqlQuery query( "select * from table1" ); | ||
− | while (query.next()) { | + | 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 ( :word ) "); | ||
+ | query.bindValue( ":word", "Banana" ); | ||
+ | query.exec(); | ||
+ | |||
//--- close connection to database | //--- close connection to database | ||
db.close(); | db.close(); | ||
Line 78: | Line 95: | ||
cout << "Error opening database\n"; | cout << "Error opening database\n"; | ||
− | return 0; | + | return 0; |
} | } | ||
− | |||
</source> | </source> | ||
− | =QtCreator= | + | =QtCreator Screen-Shot= |
− | <center>[[Image:QtCreatorMySqlProgram.png]]</center> | + | <center>[[Image:QtCreatorMySqlProgram.png|800px]]</center> |
Latest revision as of 08:45, 25 February 2011
--D. Thiebaut 15:11, 24 February 2011 (EST)
Contents |
Setup
We assume that you have installed Qt Creator and Qt 4 on your Mac (or Windows/Linux platform).
Steps
- Follow the steps of the Hello World example, and create a project called mySqlSimpleTable.
- Enter the code below in main.cpp:
// mySqlSimpleTable project
// main.cpp
// D. Thiebaut
// This program accesses a MySql database server, opens up a
// connection to a database (called dummy), with user name
// "dummy" and password "xxxxxx", and reads the full contents
// of a table called "table1".
// The contents of the table is dumped on the console.
// Good information on Qt and QSqlQueries can be found here:
// http://doc.qt.nokia.com/latest/qsqlquery.html
// Table format:
//CREATE TABLE IF NOT EXISTS `table1` (
// `Id` int(11) NOT NULL auto_increment,
// `word` varchar(45) default NULL,
// PRIMARY KEY (`Id`)
//) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
//
#include <QtCore/QCoreApplication>
#include <QtSql>
#include <qtextstream.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
//--- 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("xgridmac.dyndns.org"); // host
db.setDatabaseName("dummy"); // database
db.setUserName("dummy"); // user
db.setPassword("dummy"); // 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 table1" );
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 ( :word ) ");
query.bindValue( ":word", "Banana" );
query.exec();
//--- close connection to database
db.close();
}
else
//--- something went wrong ---
cout << "Error opening database\n";
return 0;
}
QtCreator Screen-Shot