#include "qtsql.h"#include <QtSql>#include <QMessageBox>#include <QTextCodec>QtSQL::QtSQL(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags){ ui.setupUi(this); //QTextCodec *codec=QTextCodec::codecForName("utf8"); //QTextCodec::setCodecForCStrings(codec); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setUserName("root"); db.setPassword("root"); db.setHostName("localhost"); db.setDatabaseName("test"); bool opened = db.open(); if (!opened) QMessageBox::information(this, "title", "problem", QMessageBox::Ok); QSqlQuery q("SET NAMES utf8;");}void QtSQL::on_btn_clicked(){ QSqlQuery q3("DELETE FROM `encoding` WHERE ID = 10;"); QSqlQuery q("INSERT INTO `encoding` SET ID = 10, `Text` = '" + ui.txtRead->text() + "';"); //QSqlQuery q("INSERT INTO `encoding` SET ID = 10, `Text` = 'ывыв';");}void QtSQL::on_btn_2_clicked(){ QTextCodec *codec=QTextCodec::codecForName("utf8"); QTextCodec::setCodecForCStrings(codec); QSqlQuery q("SELECT * FROM `encoding` WHERE ID = 10;"); q.next(); QSqlRecord rec = q.record(); ui.txt->setText(rec.value("Text").toString());}
int main(int argc, char *argv[]){ QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-1251")); QTextCodec::setCodecForTr(QTextCodec::codecForName("Windows-1251")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("Windows-1251"));