mysql> SHOW VARIABLES LIKE 'character_set%';+--------------------------+----------------------------+| Variable_name | Value |+--------------------------+----------------------------+| character_set_client | latin1 || character_set_connection | latin1 || character_set_database | latin1 || character_set_results | latin1 || character_set_server | latin1 || character_set_system | utf8 || character_sets_dir | /usr/share/mysql/charsets/ |+--------------------------+----------------------------+
#include <stdlib.h>#include <QApplication>#include <QTextCodec>#include <QPlastiqueStyle>#include <QTableWidget>#include <QTableWidgetItem>#include <QSqlDatabase>#include <QSqlQuery>#include <QSqlError>int main(int argc, char *argv[]){ QTextCodec *codec=QTextCodec::codecForName("utf8");// QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QApplication app(argc, argv); app.setStyle( new QPlastiqueStyle ); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("test"); db.setUserName("test"); if ( !db.open() ) qDebug("DB NOT OPEN: %s", qPrintable(db.lastError().text() ) ); QTableWidget *t = new QTableWidget(0, 1); t->show(); int rows = 0; QTableWidgetItem *item = 0; QSqlQuery query(db); if ( !query.exec("SELECT data FROM test_table") ) qDebug("QUERY ERROR: %s", qPrintable( query.lastError().text() ) ); else { while( query.next() ) { t->insertRow(rows); item = new QTableWidgetItem( query.value(0).toString() ); t->setItem(rows, 0, item); rows++; } } return app.exec();}
CREATE TABLE test_table(data VARCHAR(80));
INSERT INTO test_table VALUES ('перевірка');
[client]default-character-set=koi8u[mysqld]character-set-server=koi8u
codec = QTextCodec::codecForName("Windows-1251");
codec->fromUnicode(data);
while (query.next()){ listWidget->addItem(codec->toUnicode(query.value(0).toByteArray()));}
/******************************************************************************** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include <qapplication.h>#include <qsqldatabase.h>#include <qdatatable.h>#include <qsqlcursor.h>#include <qmessagebox.h>#include <qtextcodec.h> /* Modify the following to match your environment */#define DRIVER "QMYSQL3" /* see the Qt SQL documentation for a list of available drivers */#define DATABASE "survey" /* the name of your database */#define USER "root" /* user name with appropriate rights */#define PASSWORD "l" /* password for USER */#define HOST "" /* host on which the database is running */int main( int argc, char** argv ){ QApplication app( argc, argv ); QTextCodec *codec=QTextCodec::codecForName("utf8");// QTextCodec::setCodecForLocale(codec); QTextCodec::setCodecForCStrings(codec); QSqlDatabase * db = QSqlDatabase::addDatabase( DRIVER ); db->setDatabaseName( DATABASE ); db->setUserName( USER ); db->setPassword( PASSWORD ); db->setHostName( HOST ); if( !db->open() ){ db->lastError().showMessage( "An error occured. Please read the README file in the sqltable" "dir for more information.\n\n" ); return 1; } QSqlCursor sqlCursor("sitepassport"); QDataTable table( &sqlCursor ); /* data table uses our cursor */ table.addColumn( "sitename", "Название сайта" ); table.addColumn( "siteowner", "Владелец сайта" ); table.setSorting( TRUE ); app.setMainWidget( &table ); table.refresh(); /* load data */ table.show(); /* show widget */ return app.exec();}
int main(int argc, char *argv[]){ QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-1251")); QTextCodec::setCodecForTr(QTextCodec::codecForName("Windows-1251")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("Windows-1251")); //QTextCodec *qt1lCodecOEM866 = QTextCodec::codecForName("IBM 866"); //QTextCodec::setCodecForCStrings(qt1lCodecutf8); QApplication app(argc, argv); Q_INIT_RESOURCE(realtorplus); MainWindow mainWin; mainWin.show(); return app.exec();}
#include <QSqlDatabase>#include <QtSql/QSqlQuery>#include <QApplication>#include <QSettings>class QtSql;bool createConnection(){ QSettings settings(QSettings::IniFormat, QSettings::UserScope, "RealtorPlus", "agency"); QString driver = settings.value("driver").toString(); QString database = settings.value("database").toString(); QString host = settings.value("host").toString(); QString user = settings.value("user").toString(); QString password = settings.value("password").toString(); QSqlDatabase db; db = QSqlDatabase::addDatabase(driver); db.setHostName(host); db.setDatabaseName(database); db.setUserName(user); db.setPassword(password); if (!db.open()) { /* QMessageBox::critical(0, qApp->tr("Cannot open database"), qApp->tr("Unable to establish a database connection.\n" "Perhaps Qt was built without database support.\n\n" "Click Cancel to exit."), QMessageBox::Cancel, QMessageBox::NoButton);*/ return false; } QSqlQuery query; query.exec("SET NAMES utf8_general_ci;"); return true;}#endif /*CONNECTION_H_*/
#include <qapplication.h>#include <qsqldatabase.h>#include <qdatatable.h>#include <qsqlcursor.h>#include <qmessagebox.h>#include <qtextcodec.h> /* Modify the following to match your environment */#define DRIVER "QMYSQL3" /* see the Qt SQL documentation for a list of available drivers */#define DATABASE "survey" /* the name of your database */#define USER "root" /* user name with appropriate rights */#define PASSWORD "" /* password for USER */#define HOST "" /* host on which the database is running */int main( int argc, char** argv ){ QApplication app( argc, argv ); QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8")); QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8")); // QTextCodec *codec=QTextCodec::codecForName("utf8");// QTextCodec::setCodecForLocale(codec); // QTextCodec::setCodecForCStrings(codec); QSqlDatabase * db = QSqlDatabase::addDatabase( DRIVER ); db->setDatabaseName( DATABASE ); db->setUserName( USER ); db->setPassword( PASSWORD ); db->setHostName( HOST ); if( !db->open() ){ db->lastError().showMessage( "An error occured. Please read the README file in the sqltable" "dir for more information.\n\n" ); return 1; } QSqlQuery query; query.exec("SET NAMES utf8_general_ci;"); QSqlCursor sqlCursor("sitepassport"); QDataTable table( &sqlCursor ); /* data table uses our cursor */ table.addColumn( "sitename", "Название сайта" ); table.addColumn( "siteowner", "Владелец сайта" ); table.setSorting( TRUE ); app.setMainWidget( &table ); table.refresh(); /* load data */ table.show(); /* show widget */ return app.exec();}
QTextCodec
table.addColumn( "sitename", "Название сайта" ); table.addColumn( "siteowner", "Владелец сайта" );