最近在做QT和Win32SDK的项目,其中涉及到利用QT的方式访问ACCESS数据库.
QT利用一种类细于JAVA JDBC的方式呈现给用户一种极简单易用的访问数据库的方式。
好,下面我直接用代码说话。以下是3个星期前我写的一个类模板,专门用于访问ACCESS。在我的设计中这个类模板的每一个实例化对象专门用于访问一张表。
以下是头文件component.h
#ifndef COMPONENT_H
#define COMPONENT_H
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif
#include <QApplication>
#include <QString>
#include <QVector>
#include <QSqlTableModel>
#include <QSqlRecord>
int const PRIMARYKEY_INVALID_VALUE = -1;
template <typename T>
class SqlTableObj : public T
{
public:
SqlTableObj() {}
explicit SqlTableObj(typename T::PrimaryKeyType const &primaryKeyValue);
typedef SqlTableObj<T> this_type;
typedef QVector<this_type> CollectionType;
static CollectionType const collection(QString const &filter = QString(""));
static void updateState(QString const &filter = QString(""),int *a = NULL );
static void updateStateEx( QString const &filter = QString(""),int row = 0,int index = 0,int value =0 );
int getSignals();
private:
static void createTableModel(QString const &filter, QSqlTableModel &model);
};
template <typename T>
SqlTableObj<T>::SqlTableObj(typename T::PrimaryKeyType const &primaryKeyValue)
{
if (T::primaryKeyInvalidValue() != primaryKeyValue)
{
QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );
QSqlTableModel model( 0, qdb );
QString filter = T::primaryKeyName() + QString("=%1");
createTableModel(filter.arg(primaryKeyValue), model);
/*
if (1 == model.rowCount())
{
T::assign(model.record(0));
}
*/
T::assign(model.record(0));
}
}
template <typename T> typename SqlTableObj<T>::CollectionType const
SqlTableObj<T>::collection(QString const &filter/* = QString("")*/)
{
QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );
QSqlTableModel model( 0, qdb );
createTableModel(filter, model);
this_type tableObj;
CollectionType result;
for (int i = 0; i < model.rowCount(); ++i)
{
tableObj.assign(model.record(i));
if (tableObj.valid())
{
result.push_back(tableObj);
}
}
return result;
}
template <typename T>
void SqlTableObj<T>::updateState( QString const &filter,int *a = NULL )
{
QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );
QSqlTableModel model( 0, qdb );
createTableModel(filter, model);
/**/
for (int i = 0; i < model.rowCount(); i++)
{
model.setData( model.index( i, 2 ), a[i] );//a[i]
}
model.submitAll();
//
}
template <typename T>
void SqlTableObj<T>::updateStateEx( QString const &filter = QString(""),
int index = 0,int value =0 )
{
QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );
QSqlTableModel model( 0, qdb );
createTableModel(filter, model);
model.setData( model.index( index, 2 ), value );
model.submitAll();
//// model.record(index).value("state")
}
template <typename T>
void SqlTableObj<T>::updateStateEEx( QString const &filter = QString(""),
int row = 0,int index = 0,int value =0 )
{
QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );
QSqlTableModel model( 0, qdb );
createTableModel(filter, model);
model.setData( model.index( row, index ), value );
model.submitAll();
//// model.record(index).value("state")
}
template <typename T>
int SqlTableObj<T>::getSignals()
{
QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );
QSqlTableModel model( 0, qdb );
createTableModel("", model);
model.setEditStrategy(QSqlTableModel::OnFieldChange);
T::assign(model.record(0));
return 0;
}
template <typename T>
void SqlTableObj<T>::createTableModel(QString const &filter, QSqlTableModel &model)
{
model.setTable(T::internalTableName());
model.setFilter(filter);
model.select();
}
class ComponentTable
{
public:
ComponentTable();
bool valid() const { return m_id != PRIMARYKEY_INVALID_VALUE; }
int id() const { return m_id; }
QString const name() const { return m_name; }
void setName(QString const &newName);
QString const filePathName() const { return m_filePathName; }
int minDistance() const { return m_minDistance; }
int maxDistance() const { return m_maxDistance; }
float direction_X() const { return m_direction_X; }
float direction_Y() const { return m_direction_Y; }
float direction_Z() const { return m_direction_Z; }
int fId_1() const { return m_fId_1; }
int fId_2() const { return m_fId_2; }
int fId_3() const { return m_fId_3; }
int categoryId() const { return m_categoryId; }
private:
friend class SqlTableObj<ComponentTable>;
typedef int PrimaryKeyType;
static PrimaryKeyType const primaryKeyInvalidValue() { return PRIMARYKEY_INVALID_VALUE; }
////////////////////////////////////////////////////////////////////////////////
static QString const connectionName()
{
return qApp->applicationDirPath() + "/Database/myhpss.mdb";
}
static QString const internalTableName()
{
return "component";
}
static QString const primaryKeyName() { return "ID"; }
void assign(QSqlRecord const &record);
int m_id;
QString m_name;
QString m_filePathName;
int m_minDistance;
int m_maxDistance;
float m_direction_X;
float m_direction_Y;
float m_direction_Z;
int m_fId_1;
int m_fId_2;
int m_fId_3;
int m_categoryId;
};
typedef SqlTableObj<ComponentTable> Component;
typedef QVector<Component> ComponentArray;
#endif //COMPONENT_H
以下是component.cpp文件
#include "component.h"
ComponentTable::ComponentTable()
{
m_id = PRIMARYKEY_INVALID_VALUE;
m_minDistance = -1;
m_maxDistance = -1;
m_direction_X = 0.0f;
m_direction_Y = 0.0f;
m_direction_Z = 0.0f;
m_fId_1 = -1;
m_fId_2 = -1;
m_fId_3 = -1;
m_categoryId = -1;
}
void ComponentTable::setName(QString const &newName)
{
m_name = newName;
//待续:将新值写入数据库
}
void ComponentTable::assign(QSqlRecord const &record)
{
m_id = record.value("ID").toInt();
m_name = record.value(QObject::tr("名称")).toString();
m_filePathName = record.value("filePathName").toString();
m_minDistance = record.value("minDistance").toInt();
m_maxDistance = record.value("maxDistance").toInt();
m_direction_X = static_cast<float>(record.value("direction_X").toDouble());
m_direction_Y = static_cast<float>(record.value("direction_Y").toDouble());
m_direction_Z = static_cast<float>(record.value("direction_Z").toDouble());
m_fId_1 = record.value("fId_1").toInt();
m_fId_2 = record.value("fId_2").toInt();
m_fId_3 = record.value("fId_3").toInt();
m_categoryId = record.value("categoryID").toInt();
}
那个ComponentTable类就是一个能部分说明问题的例子。
关于完整例程我将随后补上。