﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-坚钢不可夺其志 天地莫敢争其锋-文章分类-跨平台技术相关</title><link>http://www.cppblog.com/cppblog/category/5169.html</link><description>无 限 风 光 尽 在 我 心 弥 坚</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 02:10:49 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 02:10:49 GMT</pubDate><ttl>60</ttl><item><title>QT中利用ODBC方式访问ACCESS数据库</title><link>http://www.cppblog.com/cppblog/articles/29095.html</link><dc:creator>潜龙居</dc:creator><author>潜龙居</author><pubDate>Tue, 31 Jul 2007 12:51:00 GMT</pubDate><guid>http://www.cppblog.com/cppblog/articles/29095.html</guid><wfw:comment>http://www.cppblog.com/cppblog/comments/29095.html</wfw:comment><comments>http://www.cppblog.com/cppblog/articles/29095.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/cppblog/comments/commentRss/29095.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cppblog/services/trackbacks/29095.html</trackback:ping><description><![CDATA[&nbsp;
<p>最近在做QT和Win32SDK的项目，其中涉及到利用QT的方式访问ACCESS数据库.</p>
<p>QT利用一种类细于JAVA JDBC的方式呈现给用户一种极简单易用的访问数据库的方式。</p>
<p>好，下面我直接用代码说话。以下是3个星期前我写的一个类模板，专门用于访问ACCESS。在我的设计中这个类模板的每一个实例化对象专门用于访问一张表。<br>以下是头文件component.h<br><br>#ifndef COMPONENT_H<br>#define COMPONENT_H</p>
<p style="FONT-FAMILY: Times New Roman">#if defined(_MSC_VER) &amp;&amp; (_MSC_VER &gt;= 1020)<br>#pragma once<br>#endif</p>
<p style="FONT-FAMILY: Times New Roman">#include &lt;QApplication&gt;<br>#include &lt;QString&gt;<br>#include &lt;QVector&gt;<br>#include &lt;QSqlTableModel&gt;<br>#include &lt;QSqlRecord&gt;</p>
<p style="FONT-FAMILY: Times New Roman">int const PRIMARYKEY_INVALID_VALUE = -1;</p>
<p style="FONT-FAMILY: Times New Roman"><br>template &lt;typename T&gt;<br>class SqlTableObj : public T<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; SqlTableObj() {}<br>&nbsp;&nbsp;&nbsp; explicit SqlTableObj(typename T::PrimaryKeyType const &amp;primaryKeyValue);</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; typedef SqlTableObj&lt;T&gt; this_type;<br>&nbsp;&nbsp;&nbsp; typedef QVector&lt;this_type&gt; CollectionType;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; static CollectionType const collection(QString const &amp;filter = QString(""));<br>&nbsp;&nbsp;&nbsp; static void updateState(QString const &amp;filter = QString(""),int *a = NULL );<br>&nbsp;&nbsp;&nbsp;&nbsp;static void updateStateEx( QString const &amp;filter = QString(""),int row = 0,int index = 0,int value =0 );<br>&nbsp;&nbsp;&nbsp; int getSignals();</p>
<p style="FONT-FAMILY: Times New Roman">private:<br>&nbsp;&nbsp;&nbsp; static void createTableModel(QString const &amp;filter, QSqlTableModel &amp;model);<br>};</p>
<p style="FONT-FAMILY: Times New Roman">template &lt;typename T&gt;<br>SqlTableObj&lt;T&gt;::SqlTableObj(typename T::PrimaryKeyType const &amp;primaryKeyValue)<br>{<br>&nbsp;&nbsp;&nbsp; if (T::primaryKeyInvalidValue() != primaryKeyValue) <br>&nbsp;{<br>&nbsp;&nbsp; QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );<br>&nbsp;&nbsp; QSqlTableModel model( 0, qdb );</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; QString filter = T::primaryKeyName() + QString("=%1");<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; createTableModel(filter.arg(primaryKeyValue), model);<br>/*<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (1 == model.rowCount()) <br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T::assign(model.record(0));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>*/<br>&nbsp;&nbsp; T::assign(model.record(0));<br>&nbsp;&nbsp;&nbsp; }<br>}</p>
<p style="FONT-FAMILY: Times New Roman">template &lt;typename T&gt; typename SqlTableObj&lt;T&gt;::CollectionType const <br>SqlTableObj&lt;T&gt;::collection(QString const &amp;filter/* = QString("")*/)<br>{<br>&nbsp;QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );<br>&nbsp;&nbsp;&nbsp; QSqlTableModel model( 0, qdb );<br>&nbsp;&nbsp;&nbsp; createTableModel(filter, model);</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; this_type tableObj;<br>&nbsp;&nbsp;&nbsp; CollectionType result;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; model.rowCount(); ++i) <br>&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tableObj.assign(model.record(i));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (tableObj.valid()) <br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result.push_back(tableObj);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; return result;<br>}</p>
<p style="FONT-FAMILY: Times New Roman">template &lt;typename T&gt;<br>void SqlTableObj&lt;T&gt;::updateState( QString const &amp;filter,int *a = NULL )<br>{<br>&nbsp;QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );<br>&nbsp;QSqlTableModel model( 0, qdb );<br>&nbsp;createTableModel(filter, model);<br>/**/<br>&nbsp;for (int i = 0; i &lt; model.rowCount(); i++) <br>&nbsp;{<br>&nbsp;&nbsp;model.setData( model.index( i, 2 ), a[i] );//a[i]<br>&nbsp;}</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;model.submitAll();<br>&nbsp;//<br>}<br>template &lt;typename T&gt;<br>void SqlTableObj&lt;T&gt;::updateStateEx( QString const &amp;filter = QString(""),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int index = 0,int value =0 )<br>{<br>&nbsp;QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );<br>&nbsp;QSqlTableModel model( 0, qdb );<br>&nbsp;createTableModel(filter, model);</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;model.setData( model.index( index, 2 ), value );</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;model.submitAll();<br>////&nbsp;model.record(index).value("state")<br>}<br>template &lt;typename T&gt;<br>void SqlTableObj&lt;T&gt;::updateStateEEx( QString const &amp;filter = QString(""),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int row = 0,int index = 0,int value =0 )<br>{<br>&nbsp;QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );<br>&nbsp;QSqlTableModel model( 0, qdb );<br>&nbsp;createTableModel(filter, model);</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;model.setData( model.index( row, index ), value );</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;model.submitAll();<br>&nbsp;////&nbsp;model.record(index).value("state")<br>}<br>template &lt;typename T&gt;<br>int SqlTableObj&lt;T&gt;::getSignals()<br>{<br>&nbsp;QSqlDatabase qdb = QSqlDatabase::database( T::connectionName(),true );<br>&nbsp;QSqlTableModel model( 0, qdb );<br>&nbsp;createTableModel("", model);<br>&nbsp;&nbsp; <br>&nbsp;model.setEditStrategy(QSqlTableModel::OnFieldChange);<br>&nbsp;T::assign(model.record(0));</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;return 0;<br>}<br>template &lt;typename T&gt;<br>void SqlTableObj&lt;T&gt;::createTableModel(QString const &amp;filter, QSqlTableModel &amp;model)<br>{<br>&nbsp;&nbsp;&nbsp; model.setTable(T::internalTableName());<br>&nbsp;&nbsp;&nbsp; model.setFilter(filter);<br>&nbsp;&nbsp;&nbsp; model.select();<br>}</p>
<p style="FONT-FAMILY: Times New Roman">class ComponentTable<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; ComponentTable();</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; bool valid() const { return m_id != PRIMARYKEY_INVALID_VALUE; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int id() const { return m_id; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; QString const name() const { return m_name; }<br>&nbsp;&nbsp;&nbsp; void setName(QString const &amp;newName);</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; QString const filePathName() const { return m_filePathName; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int minDistance() const { return m_minDistance; }<br>&nbsp;&nbsp;&nbsp; &nbsp;int maxDistance() const { return m_maxDistance; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; float direction_X() const { return m_direction_X; }<br>&nbsp;&nbsp;&nbsp;&nbsp; float direction_Y() const { return m_direction_Y; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float direction_Z() const { return m_direction_Z; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int fId_1() const { return m_fId_1; }<br>&nbsp;&nbsp;&nbsp; &nbsp;int fId_2() const { return m_fId_2; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int fId_3() const { return m_fId_3; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int categoryId() const { return m_categoryId; }</p>
<p style="FONT-FAMILY: Times New Roman">private:<br>&nbsp;&nbsp;&nbsp; friend class SqlTableObj&lt;ComponentTable&gt;;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; typedef int PrimaryKeyType;<br>&nbsp;&nbsp;&nbsp; static PrimaryKeyType const primaryKeyInvalidValue() { return PRIMARYKEY_INVALID_VALUE; }<br>////////////////////////////////////////////////////////////////////////////////<br>&nbsp;&nbsp;&nbsp; static QString const connectionName() <br>&nbsp;{ <br>&nbsp;&nbsp;return qApp-&gt;applicationDirPath() + "/Database/myhpss.mdb"; <br>&nbsp;}<br>&nbsp;&nbsp;&nbsp; static QString const internalTableName() <br>&nbsp;{ <br>&nbsp;&nbsp;return "component"; <br>&nbsp;}<br>&nbsp;&nbsp;&nbsp; static QString const primaryKeyName() { return "ID"; }</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; void assign(QSqlRecord const &amp;record);</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int m_id;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; QString m_name;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; QString m_filePathName;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int m_minDistance;<br>&nbsp;&nbsp;&nbsp; int m_maxDistance;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; float m_direction_X;<br>&nbsp;&nbsp;&nbsp; float m_direction_Y;<br>&nbsp;&nbsp;&nbsp; float m_direction_Z;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int m_fId_1;<br>&nbsp;&nbsp;&nbsp; int m_fId_2;<br>&nbsp;&nbsp;&nbsp; int m_fId_3;</p>
<p style="FONT-FAMILY: Times New Roman">&nbsp;&nbsp;&nbsp; int m_categoryId;<br>};</p>
<p style="FONT-FAMILY: Times New Roman">typedef SqlTableObj&lt;ComponentTable&gt; Component;<br>typedef QVector&lt;Component&gt; ComponentArray;</p>
<p style="FONT-FAMILY: Times New Roman">#endif&nbsp; //COMPONENT_H<br></p>
<p>以下是component.cpp文件<br>#include "component.h"</p>
<p>ComponentTable::ComponentTable()<br>{<br>&nbsp;&nbsp;&nbsp; m_id = PRIMARYKEY_INVALID_VALUE;</p>
<p>&nbsp;&nbsp;&nbsp; m_minDistance = -1;<br>&nbsp;&nbsp;&nbsp; m_maxDistance = -1;</p>
<p>&nbsp;&nbsp;&nbsp; m_direction_X = 0.0f;<br>&nbsp;&nbsp;&nbsp; m_direction_Y = 0.0f;<br>&nbsp;&nbsp;&nbsp; m_direction_Z = 0.0f;</p>
<p>&nbsp;&nbsp;&nbsp; m_fId_1 = -1;<br>&nbsp;&nbsp;&nbsp; m_fId_2 = -1;<br>&nbsp;&nbsp;&nbsp; m_fId_3 = -1;</p>
<p>&nbsp;&nbsp;&nbsp; m_categoryId = -1;<br>}</p>
<p>void ComponentTable::setName(QString const &amp;newName)<br>{<br>&nbsp;&nbsp;&nbsp; m_name = newName;<br>&nbsp;&nbsp;&nbsp; //待续：将新值写入数据库<br>}</p>
<p>void ComponentTable::assign(QSqlRecord const &amp;record)<br>{<br>&nbsp;&nbsp;&nbsp; m_id = record.value("ID").toInt();</p>
<p>&nbsp;&nbsp;&nbsp; m_name = record.value(QObject::tr("名称")).toString();</p>
<p>&nbsp;&nbsp;&nbsp; m_filePathName = record.value("filePathName").toString();</p>
<p>&nbsp;&nbsp;&nbsp; m_minDistance = record.value("minDistance").toInt();<br>&nbsp;&nbsp;&nbsp; m_maxDistance = record.value("maxDistance").toInt();</p>
<p>&nbsp;&nbsp;&nbsp; m_direction_X = static_cast&lt;float&gt;(record.value("direction_X").toDouble());<br>&nbsp;&nbsp;&nbsp; m_direction_Y = static_cast&lt;float&gt;(record.value("direction_Y").toDouble());<br>&nbsp;&nbsp;&nbsp; m_direction_Z = static_cast&lt;float&gt;(record.value("direction_Z").toDouble());</p>
<p>&nbsp;&nbsp;&nbsp; m_fId_1 = record.value("fId_1").toInt();<br>&nbsp;&nbsp;&nbsp; m_fId_2 = record.value("fId_2").toInt();<br>&nbsp;&nbsp;&nbsp; m_fId_3 = record.value("fId_3").toInt();</p>
<p>&nbsp;&nbsp;&nbsp; m_categoryId = record.value("categoryID").toInt();<br>}<br>那个ComponentTable类就是一个能部分说明问题的例子。<br>关于完整例程我将随后补上。</p>
<img src ="http://www.cppblog.com/cppblog/aggbug/29095.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cppblog/" target="_blank">潜龙居</a> 2007-07-31 20:51 <a href="http://www.cppblog.com/cppblog/articles/29095.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>