标签:读写 tsql 数据 locale 打开 bug qfile each ddb
#include "mainwindow.h"
#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QtCore/QCoreApplication>
#include <QCoreApplication>
#include <QSpinBox>
#include <QSlider>
#include <QHBoxLayout>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include "newspaper.h"
#include "reader.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>
#include <QTextCodec>
bool connect(const QString &dbName)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName(dbName);
db.setPort(3306);
db.setUserName("root");
db.setPassword("root");
if (!db.open())
{
QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
return false;
}
else
{
QMessageBox::information(0,QObject::tr("Tips"),QObject::tr("连接数据库成功。。!"));
return true;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/*测试链接数据库
//测试支持的数据库
// QCoreApplication a(argc, argv);
// qDebug() << "Available drivers:";
// QStringList drivers = QSqlDatabase::drivers();
// foreach(QString driver, drivers)
// qDebug() << "\t "<< driver;
// qDebug() << "End";
// return a.exec();
//测试打开数据库
// QCoreApplication a(argc, argv);
// QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
// db.setHostName("localhost");
// db.setDatabaseName("qttest");//已建立的数据库名称
// db.setUserName("root");//用户名称
// db.setPassword("root");//密码
// bool ok = db.open();//如果成功ok位true,否则为false
// qDebug() << "\t"<<ok;
qDebug() << "Available drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers) qDebug() << "\t "<< driver;
qDebug() << "End";
*/
//测试连接并打开操作数据库
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
if (connect("eng"))
{
QSqlQuery query;
/*
query.prepare("INSERT INTO student VALUES (?, ?, ?, ?)");
QVariantList IDs;
IDs << 1 << 2 << 3 << 4;
query.addBindValue(IDs);
QVariantList names;
names << "Tom" << "Jack" << "Jane" << "Jerry";
query.addBindValue(names);
QVariantList dept_names;
dept_names << "计算机" << "艺术" << "数学" << "外语";
query.addBindValue(dept_names);
QVariantList tot_creds;
tot_creds << "10" << "11" << "12" << "13";
query.addBindValue(tot_creds);
if (!query.execBatch())
{
QMessageBox::critical(0, QObject::tr("Database Error"),
query.lastError().text());
}
query.finish();
*/
query.prepare("INSERT INTO tempTest (name, description) "
"VALUES (:name, :description)");
query.bindValue(":name", "测试");
query.bindValue(":description", "测试插入数据");
query.exec();
QString sql="SELECT name, description FROM tempTest";
query.exec(sql);
while (query.next())
{
QString name = query.value(0).toString();
QString description = query.value(1).toString();
qDebug() << name << ": " << description;
}
}
else
{
return 1;
}
return a.exec();
}
标签:读写 tsql 数据 locale 打开 bug qfile each ddb
原文地址:http://www.cnblogs.com/yingchuanxiaoge/p/7573646.html