Search code examples
c++sqlmysql-connector-c

C++ with sql connector getting metadata without asking for to much data


I just started working with the sql conenctor in C++ (and sql). I need to write a bunch of header files from objects out of a database. (MariaDB but would be great if it works on all sql dbs) My Solution so far is getting the tablenames with

res = stmt->executeQuery("SHOW TABLES from " + dbname);

where dbname is a string entered by the user. I am storing the data for later use in a vector called tablenames and use it as follows:

   for(std::string table :tablenames){
    delete stmt; //freeing memory and attach new information to it
    delete res;
    std::string query;
    query = "SELECT * from " + table + ";";
    stmt = con->createStatement();
    res = stmt ->executeQuery(query);
    std::cout << query << "\n";
    //using metadata for getting information or passing it to another method
}

It is not ready but will work, but I take much more information out of the database, than I need. I would like to take just 1 row out of the database (or maybe just the tableinformation) and access the metadata to retrieve the information (like columnlabel, columntypename and maybe the displaysize) i need for the headerfile. So my problem is, that i generate to much traffic with my solution, especially if i run this against big databases. I found some solutions that used something like WHERE id = 1 but i can't guarantee that there is an id (or something else) in the table. I hope you can help me to find a better solution.


Solution

  • Use the limit keyword. "select * from table LIMIT 1" This will do as it says and limit the result to just one.