如是,主要想像使用如下ADO一样获取表名,还想获取记录集,但在VC中使用SQLITE我还是头一次,向各位请教了。
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 打开本地Access库
strExec.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;Jet OLEDB:DataBase Password=%s"),m_mdbPath,m_mdbPass);
m_pConnection->Open(_bstr_t(strExec),"","",adModeUnknown);
// 获取表名并填入下拉框
m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);
int row=0;
while(!(m_pRecordset->adoEOF))
{
_bstr_t table_name=m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
_bstr_t table_type=m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
row=m_tableName.GetCount();
if(strcmp(((LPCSTR)table_type),"TABLE")==0)
{
strExec.Format("%s",(LPCSTR)table_name);
m_tableName.AddString(strExec);
m_tableName.SetCurSel(row);
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pConnection->Close();
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
m_pConnection.CreateInstance(__uuidof(Connection));
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 打开本地Access库
strExec.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Persist Security Info=False;Jet OLEDB:DataBase Password=%s"),m_mdbPath,m_mdbPass);
m_pConnection->Open(_bstr_t(strExec),"","",adModeUnknown);
// 获取表名并填入下拉框
m_pRecordset=m_pConnection->OpenSchema(adSchemaTables);
int row=0;
while(!(m_pRecordset->adoEOF))
{
_bstr_t table_name=m_pRecordset->Fields->GetItem("TABLE_NAME")->Value;
_bstr_t table_type=m_pRecordset->Fields->GetItem("TABLE_TYPE")->Value;
row=m_tableName.GetCount();
if(strcmp(((LPCSTR)table_type),"TABLE")==0)
{
strExec.Format("%s",(LPCSTR)table_name);
m_tableName.AddString(strExec);
m_tableName.SetCurSel(row);
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
m_pConnection->Close();
功能
执行一条查询语句,返回结果集
参数
sqlStr:需要执行的SQL语句
pDataTable:【out】返回的数据集
errMsg:【out】返回的错误信息
返回
*********************************/
int CMyDatabase::query(char *sqlStr, CDataTable *pDataTable, char **errMsg)
{
int result;
char **dbResult; //是 char ** 类型,两个*号
int nRow, nColumn;
int i , j;
int index;
result = sqlite3_get_table( m_pDb, sqlStr, &dbResult, &nRow, &nColumn, errMsg); if (result == SQLITE_OK)
{
index = nColumn;
//前面说过 dbResult 前面第一行数据是字段名称,从 nColumn 索引开始才是真正的数据
for( i = 0; i < nRow ; i++ )
{
CDataRow dr;
for( j = 0 ; j < nColumn; j ++)
{
//printf("字段名:%s 字段值:%s\n", dbResult[j], dbResult[index] ); if (dbResult[index])
{
dr.addColumn(dbResult[j], dbResult[index]);
index++;
}
}
if (pDataTable && dr.getColumns() > 0)
pDataTable->addRow(&dr);
}
} sqlite3_free_table( dbResult ); return result;
}
主要是使用sqlite3_get_table,进行查询,但别人建议最好别用,用sqlite3_exec代替,采用回调的方式查询。但我在x86架构下的linux,与mips架构下的linux使用sqlite3_get_table也没发现什么问题。