Skip to content

Instantly share code, notes, and snippets.

@scottnguyen
Created July 29, 2011 05:49
Show Gist options
  • Save scottnguyen/1113201 to your computer and use it in GitHub Desktop.
Save scottnguyen/1113201 to your computer and use it in GitHub Desktop.
DTL example for reading and writing to stdout from table
/* A DTL Example for reading and printing table data.
* Probably doesn't work... I can't test it though for some reason... MakeFile
* doesn't recognize Darwin/OSX. Probably gonna try to compile DTL on Linux
* tomorrow.
*
* Create your own ODBC connection string: http://www.connectionstrings.com
*
* Remember: DTL is a semi-container based on STL. Most STL functions can be
* used with DTL.
*
* @author Scott Nguyen <scottnguyen@acm.org>
*/
#include <iostream>
#include <vector>
#include "DTL.h"
using namespace dtl;
using namespace std;
struct tab_schema{
string name;
string address;
string id;
};
int main() {
vector<tab_schema> result_set;
string table_name = "tab";
try{
//replace the connection string with whatever database you're using
DBConnection::GetDefaultConnection().Connect("Data Source=test.db;Version=3;");
}
catch ( exception &e){ //connection failed
cerr << e.what() << endl;
}
//SELECT * FROM testtable
DBView<tab_schema> view( table_name );
//iterate over the entire result set from the previous SELECT query and
//pushes each struct into a vector
for (DBView::select_iterator it = view.begin() ; it != view.end() ; it++ ){
result_set.push_back(*it);
}
//Prints the row number and row data
unsigned int = 1;
for ( vector::iterator it = result_set.begin() ; it != result_set.end() ; it++ ){
cout << "ROW "<< n << ":";
for ( unsigned int i = 0 ; i < tab_schema.size() ; i++ ){
cout << it[i] << "\t\t";
}
cout << endl;
}
result_set.clear();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment