% ./testquery.pl -db testquery -query "select * from station_info"
Host: localhost
Database: testquery
Query: select * from station_info
Answer: 80821 2833 35.43 -5.54 -9999 10 60101 MO طنجة
Answer: 80822 2833 35.88 -5.31 -9999 10 60320 MO سبتة
Answer: 80823 2379 17.09 -20.83 -9999 10 99999 MO الكويرة
Answer: 1608 3108 49.0333 13.2333 612 1 10796 Germany Plzeň
Answer: 1808 3198 50.6833 14.0333 377 1 11502 Czech Republic ÚstÃ* n. L.
Answer: 1814 3108 48.9333 14.45 432 1 11541 Czech Republic Č. Budějovice
% ./testquery.pl -db testquery -query "select * from station_info"
Host: localhost
Database: testquery
Query: select * from station_info
Answer: 80821 2833 35.43 -5.54 -9999 10 60101 MO طنجة
Answer: 80822 2833 35.88 -5.31 -9999 10 60320 MO سبتة
Answer: 80823 2379 17.09 -20.83 -9999 10 99999 MO الكويرة
Answer: 1608 3108 49.0333 13.2333 612 1 10796 Germany Plzeň
Answer: 1808 3198 50.6833 14.0333 377 1 11502 Czech Republic ÚstÃ* n. L.
Answer: 1814 3108 48.9333 14.45 432 1 11541 Czech Republic Č. Budějovice
To copy to clipboard, switch view to plain text mode
Hence, I don't think there is anything wrong with the database.
#include <QtGui>
#include <QtSql>
bool createConnection()
{
db.setHostName("localhost");
db.setDatabaseName("testquery");
db.setUserName("guest");
db.setPassword("guest");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs MySQL support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
return false;
}
return true;
}
{
QString cmd
("SELECT id, country, placename FROM station_info");
int i = 0;
if( query.isActive() )
{
table->setRowCount(query.size());
table->setColumnCount(4);
labels << "ID" << "Country" << "From Latin1" << "From UTF-8";
table->setHorizontalHeaderLabels(labels);
while ( query.next() )
{
double id = query.value(0).toInt();
QString country
= query.
value(1).
toString();
QString placename1
= query.
value(2).
toString();
QString placename2
= codec
->toUnicode
(query.
value(2).
toByteArray());
i++;
}
}
}
int main(int argc, char *argv[])
{
if (!createConnection())
return 1;
populateTableFromDatabase(table);
hbox->addWidget(table);
widget.setLayout(hbox);
widget.setWindowTitle("testquery: MySQL 5");
widget.resize(450,240);
widget.show();
return app.exec();
}
#include <QtGui>
#include <QtSql>
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("testquery");
db.setUserName("guest");
db.setPassword("guest");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs MySQL support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
return true;
}
void populateTableFromDatabase(QTableWidget* table)
{
QTextCodec* codec = QTextCodec::codecForName("utf-8");
QString cmd("SELECT id, country, placename FROM station_info");
int i = 0;
QSqlQuery query(cmd);
if( query.isActive() )
{
table->setRowCount(query.size());
table->setColumnCount(4);
QStringList labels;
labels << "ID" << "Country" << "From Latin1" << "From UTF-8";
table->setHorizontalHeaderLabels(labels);
while ( query.next() )
{
double id = query.value(0).toInt();
QString country = query.value(1).toString();
QString placename1 = query.value(2).toString();
QString placename2 = codec->toUnicode(query.value(2).toByteArray());
table->setItem(i,0,new QTableWidgetItem(QString::number(id)));
table->setItem(i,1,new QTableWidgetItem(country));
table->setItem(i,2,new QTableWidgetItem(placename1));
table->setItem(i,3,new QTableWidgetItem(placename2));
i++;
}
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
if (!createConnection())
return 1;
QHBoxLayout *hbox = new QHBoxLayout();
QTableWidget *table = new QTableWidget();
populateTableFromDatabase(table);
hbox->addWidget(table);
QWidget widget;
widget.setLayout(hbox);
widget.setWindowTitle("testquery: MySQL 5");
widget.resize(450,240);
widget.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks