有沒有辦法通過列名(如C++ Map)訪問SQLite結果而不是C/C++中的索引號?SQLite C++按名稱訪問列
例如,Python的SQLite的訪問允許字典訪問
Results = Query("SELECT * FROM table");
print Results['colname']
print Results['anothercol']
任何類似的方法用C++提供用於SQLite的界面?
有沒有辦法通過列名(如C++ Map)訪問SQLite結果而不是C/C++中的索引號?SQLite C++按名稱訪問列
例如,Python的SQLite的訪問允許字典訪問
Results = Query("SELECT * FROM table");
print Results['colname']
print Results['anothercol']
任何類似的方法用C++提供用於SQLite的界面?
我與丹尼爾走在www.sqlapi.com推薦SQLAPI ++ - - 在http://www.sqlapi.com/HowTo/fetch.html你可以找到一個簡單的按名字獲取字段的例子。這個例子是一個有點冗長,所以這裏是一個代碼只要點:
void showemps(SAConnection* pconn, int minage)
{
SACommand cmd(pconn, "select name, age from employees where age>:1");
cmd << minage;
cmd.execute();
while(cmd.FetchNext()) {
SAString sName = cmd.Field("name");
long nAge = cmd.Field("age");
printf("Name: %s, age: %d \n", sName, nAge);
}
}
較新的回答 http://www.sqlapi.com/這個庫可能會做你想做的。
老答案 基本上你將不得不與sqlite3_column_name16
或sqlite3_column_name
迭代列。你將不得不比較這些返回的字符串和你想查找的字符串。
我已經使用了這個,它的MFC,但是它給了你需要完成的基本概念。
int CSQLite3Query::FieldIndex(const CString &field)
{
CheckVM();
if (!field.IsEmpty())
{
for (int nField = 0; nField < m_nCols; nField++)
{
#ifdef UNICODE
CString sTemp = (LPCTSTR)sqlite3_column_name16(m_VM, nField);
#else
CString sTemp = (LPCTSTR)sqlite3_column_name(m_VM, nField);
#endif
if (sTemp == field)
return nField;
}
}
else
{
throw new CSQLite3Exception(MFCSQLITE3_ERROR,
MFCSQLITE3_INVALID_FIELD_NAME);
}
return -1;
}
如果您知道列名的索引只需要創建一個局部變量的列索引,如:
int colname = 0;
int anothercol = 2; //just guessing ;-)
Results = ...
std::cout << Results[colname];
std::cout << Results[anothercol];
不是很優雅... – 2009-05-03 22:31:15