//connection.h
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
** of its contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QtGlobal>
/*
This file defines a helper functiontoopen a connection to an
in-memory SQLITE database andto create a test table.
If you want touse another database, simply modify the code
below. All the examples in this directory use this functionto
connect to a database.
*/
//! [0]static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n""This example needs SQLite support. Please read ""the Qt SQL driver documentation for information how ""to build it.\n\n""Click Cancel to exit."), QMessageBox::Cancel);
returnfalse;
}
QTextCodec *gbk = QTextCodec::codecForName("GB18030");
if (gbk)
{
QSqlQuery query;
query.exec("create table goods (id varchar(33) primary key, ""name varchar(200), color varchar(20), ""size varchar(20), retail varchar(20), ""storage int, productid varchar(33), ""unitname varchar(20), no varchar(200))");
std::string strgb18030("insert into goods values('7A7BA4B902C147E6A37BAF325DB14D50', '七匹狼', '红色', 'L', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151020')");
QString strUnicode = gbk->toUnicode(strgb18030.c_str());
Q_ASSERT(query.exec(strUnicode));
strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D51', '七匹狼', '红色', 'S', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151021')";
strUnicode = gbk->toUnicode(strgb18030.c_str());
query.exec(strUnicode);
strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D52', '八匹马', '绿色', 'L', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151022')";
strUnicode = gbk->toUnicode(strgb18030.c_str());
query.exec(strUnicode);
strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D53', '八匹马', '绿色', , 'S', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151023')";
strUnicode = gbk->toUnicode(strgb18030.c_str());
query.exec(strUnicode);
strgb18030 = "insert into goods values('7A7BA4B902C147E6A37BAF325DB14D54', '七匹狼', '蓝色', 'XL', '100.50', 100, '7A7BA4B902C147E6A37BAF325DB14D50', '件', '20151024')";
strUnicode = gbk->toUnicode(strgb18030.c_str());
query.exec(strUnicode);
}
returntrue;
}
//! [0]
#endif