1

(Totally new to C++. Coming from python.)

Vector of names: namelist, vector of stats: statlist. Each name is a mysql table and each stat is a column in that table.

for (unsigned int i = 0; i < namelist.size(); i++)
{
    for (unsigned int j = 30; j < statlist.size(); j++)
    {
        string selectcolumn = "SELECT %s FROM %s.%s", statlist, statlist, namelist;
        const char* c = selectcolumn.c_str();
        qstate = mysql_query(conn, c);
    }
}

(I don't expect people to do this for me I just need some guidance/advice. ANY help is really appreciated!)

Each column row will be ranked by percentile then split into groups by 10 and put into another table.

  1. How do I correctly associate variables to %s string in the loop here?
  2. Do I need to dynamically generate a vector for each column as I select it?
  3. Once I select a table column do I need to use a while loop?
john mondego
  • 147
  • 1
  • 7
  • Can we see what a `name` and a `stat` look like? – NathanOliver Dec 17 '19 at 16:49
  • @AlgirdasPreidžius That string is a mysql query to select a column from a table. – john mondego Dec 17 '19 at 16:50
  • Even in python this logic wouldn't work. You have to use i and j to access the elements in your vector. Than formatting your string can't work this way, look at stringstream for that – RoQuOTriX Dec 17 '19 at 16:51
  • Except using `statList` twice makes the query into nonsense I think – RiggsFolly Dec 17 '19 at 16:51
  • Your current combination of C++/SQL isn't something am aware of but clearly you can't initialize a string like that in C++ with format specifiers (for string or whatsoever else too) attached to it. –  Dec 17 '19 at 16:51
  • @johnmondego That is not what I asked. Let me elaborate, what `"SELECT %s FROM %s.%s", statlist, namelist, statlist;` should do? It sounds, that you are looking for [`printf`](https://en.cppreference.com/w/cpp/io/c/fprintf), but, still, you should consider learning C++ from a good book, instead of coding randomly. – Algirdas Preidžius Dec 17 '19 at 16:51
  • @NathanOliver-ReinstateMonica names are a vector of stock symbols. Stats are prices, returns, volume, etc. Query should be "SELECT prices FROM prices.name – john mondego Dec 17 '19 at 16:52
  • 1
    Is a `stringstream` similar to what you're looking for? Allows you to create a string from variables such as: `std::stringstream example; example << "SELECT " << statlist[i] << " FROM " << namelist[j] << "." << statlist[i]; std::string selectcolumn = example.str();` (Note: Code not tested in any way, shape or form) – user11923373 Dec 17 '19 at 16:52
  • 2
    This is a recipe for a [sql injection](https://en.wikipedia.org/wiki/SQL_injection). Just saying. – WhozCraig Dec 17 '19 at 16:53
  • @AlgirdasPreidžius Its just a string that is passed to mysql as a query. I am trying to associate the current statlist and namelist variables in the loop to the %s but am I missing an index? – john mondego Dec 17 '19 at 16:56
  • @user11923373 Looks like it could be yes. I will try. Thanks for commenting! – john mondego Dec 17 '19 at 16:57
  • Unless I'm mistaken, `mysql_query` is not a standard C++ function. Are you usinga specific library? –  Dec 17 '19 at 17:06
  • @Chipster I'm using a mysql connector for C++ – john mondego Dec 17 '19 at 17:57

3 Answers3

2
  1. How do I correctly associate variables to %s string in the loop here?

If you're looking for a direct equivalent to what you are doing in Python, you might be looking for printf().

However, I would probably do what others have suggested, and simply concatenate the strings directly:

string selectcolumn = "SELECT " + statlist " FROM " + statlist "." + namelist;

That said, I would be doing you a disservice if I didn't warn you about SQL Injection, which you are open to by simply inserting this data into your query. There are lots of ways to prevent this, though I'm not sure how this applies to C++, as you seem to be using some 3rd party SQL library. However, I would imagine the principle is the same.

2.Do I need to dynamically generate a vector for each column as I select it? 3.Once I select a table column do I need to use a while loop?

This really depends on what you're using to interface with your SQL server, and how the data is given back to you. The details of this are unclear, to it's impossible to answer that part properly.

  • Great post! (for 2&3: using a pandas dataframe this would be simple to do). When I select a column from the SQL table I want to rank each row in that column by percentile. Should I store the column rows in a vector first and then calculate the values? These values are going to be stored in another table afterwards. – john mondego Dec 17 '19 at 23:25
  • Btw I'm using this if you're curious: https://dev.mysql.com/doc/refman/8.0/en/connector-cpp-info.html – john mondego Dec 17 '19 at 23:32
1

Given that statlist and namelist are strings too, you can use the concatenation operator ("+" in C++)

For example, in this case, you would do:

"SELECT " + statlist + " FROM " + namelist + "." + statlist
mathmaniac88
  • 562
  • 6
  • 20
1

If we can't see what types statlist and namelist are, we can only assume they are std::vector's. I don't really know if this is the "logic" you want, but you could concatenate the string like this:

for (unsigned int i = 0; i < namelist.size(); i++)
{
    for (unsigned int j = 30; j < statlist.size(); j++)
    {
        std::string selectcolumn = "SELECT " + statlist[j] +"FROM " + statlist[j] + "." + namelist[i];
        const char* c = selectcolumn.c_str();
        qstate = mysql_query(conn, c);
    }
}
RoQuOTriX
  • 2,623
  • 10
  • 24