SQL query C++ code (My first C++ application) I would really welcome some advice, and notes about my code. I am a long time Python user, and some days ago i decided i should try C++. So a couple of days passed, and i wrote my first application which has a practical use. I still find C++ very complicated (my current perspective is over complicated) but i can clearly say it is faster then my same python implementation.

The code:

/* 
 * File:   main.cpp
 * Author: gerdos
 *
 * Created on October 10, 2016, 4:23 PM
 */

#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;unistd.h&gt;
#include &lt;list&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

#include &lt;mysql_connection.h&gt;
#include &lt;cppconn/driver.h&gt;
#include &lt;cppconn/exception.h&gt;
#include &lt;cppconn/resultset.h&gt;
#include &lt;cppconn/statement.h&gt;
#include &lt;cppconn/resultset_metadata.h&gt;

using namespace sql;
using namespace std;

int main(int argc, char* argv[]){
    int opt;
    vector&lt;string&gt; column_name_list;

    // Shut GetOpt error messages down (return '?'): 
    opterr = 0;

    // Retrieve the options:
    while ( (opt = getopt(argc, argv, "F:")) != -1 ) {  // for each option...
        if (opt == 'F'){
            column_name_list.insert(column_name_list.end(), string(optarg));       
        }
    }
    string queries;
    for (unsigned i = 1; i &lt; argc; i++){
        if (string(argv[i]) != "-F" &amp;&amp; find(column_name_list.begin(), column_name_list.end(), argv[i]) == column_name_list.end()){
            queries += "'" + string(argv[i]) + "',";
        }
    }

    string columns;
    for (unsigned i = 0; i &lt; column_name_list.size(); i++){
        columns += column_name_list[i] + ",";
    }
    columns.erase(columns.size()-1);
    queries.erase(queries.size()-1);

    try{
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;

        driver = get_driver_instance();

        con = driver-&gt;connect("sql_adress", "user", "nope"); // Join a server
        con-&gt;setSchema("database_name"); // Use a database

        stmt = con-&gt;createStatement();
        res = stmt-&gt;executeQuery(string("select " + columns + " from table_name where accession in (" + queries + ")"));

        sql::ResultSetMetaData *res_meta = res -&gt; getMetaData();
        int cols = res_meta -&gt; getColumnCount();
        if (res-&gt;rowsCount() == 0){
            cout &lt;&lt; "Query not found!" &lt;&lt; endl;
            return 0;
        }
        while (res-&gt;next()) {
            for (unsigned i = 1; i &lt;= cols; i++){
                cout &lt;&lt; res-&gt;getString(i) &lt;&lt; endl;
            }  
        }
        delete res;
        delete stmt;
        delete con;
    }  
    // No idea about this part yet, copied it from a tutorial
    catch (sql::SQLException &amp;e){
        cout &lt;&lt; "# ERR: SQLException in " &lt;&lt; __FILE__;
        cout &lt;&lt; "(" &lt;&lt; __FUNCTION__ &lt;&lt; ") on line " &lt;&lt; __LINE__ &lt;&lt; endl;
        cout &lt;&lt; "# ERR: " &lt;&lt; e.what();
        cout &lt;&lt; " (MySQL error code: " &lt;&lt; e.getErrorCode();
        cout &lt;&lt; ", SQLState: " &lt;&lt; e.getSQLState() &lt;&lt; " )" &lt;&lt; endl;
    }
    return 0;
}
I found this code very difficult to understand, and I feel that I won't be alone. I can't refactor it (for the aforementioned reason) nevertheless some advice which you might find instructive:


Please use names which people will make sense of: column_name_list, opt, "F", opterr etc etc - this makes reading code very difficult.
formatting - please use good formatting it all makes a difference.
break things up into methods so that each method is doing only one thing.
Add better comments if things are not self-explanatory.


My two cents.