converting a mysql_row to an integer - c++

is there an easy way to convert a row obtained from a result of a query into an integer in C/C++...
eg
//iterating over each row of the result
while((row = mysql_fetch_row(result)) != NULL)
{
printf("%s\n",row[0]);
}
output
100
101
102
mysql_row is of the type string so i can easyily display..but my problem is i want to convert that to an intger so that i can add all the column values of the result
i.e 100+101+102
i guess using sscanf helps but dont knw how to use it in this situation

use atoi(row[index])
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

"mysql_row is of the type string so i can easyily display"
Erm, don't do that.
MYSQL_TYPE_LONG is what you want, which makes the result an int.
EDIT: Based on the comment below. I hadn't used the C API in years so I just looked it up. It's ... rather limited as compared to say, Oracle's C API. Everything is returned as a string in your case, so you have to convert it yourself.
As mentioned in the other answers, if you're using C++ you can use atoi. If that's not working ... you've got some other issue (wrong column, perhaps).

Related

How to check if my string input followed the correct syntax in C++?

I'm new to this site and this is my first time to ask here.
My problem is I want to check if my string follows a correct pattern or syntax. I'm doing it with C++ String (std::string). I have already done this using C-Style string, however, I want to do it this time in C++ String. Sample problem below:
Input: 2y'' + 3y' - 2y = 0
or y'' = 4y
I want to check if the derivative input is in correct syntax like (a)y'' + (b)y' + (c)y = 0, a second order homogeneous equation. However, I still want to input a non-standard form equation like the second sample input that can be transposed and make it to standard form.
What I did before with it is remove all the white spaces, loop the entire string and check every index. Eg. if 'y' is found the next char should be '\'' or an arithmetic symbol like '-' or '+' or '=' then if it does not match, then, it must return false.
Or maybe I am just implementing this wrong. I'm new to programming and just taking a computer science course. Note: Sorry for my bad English and sorry if I did not written my code here. Its just way too long.
Regular expressions might be the answer. They're commonly used for checking whether a string first a format, or finding parts of a string that do.
RegExr is a great tool to both learn and test your regular expressions.

Airtable If-statement outputting NaN

I'm using an If-statement to assign integers to strings from another cell. This seems to be working, but if I reference these columns, I'm getting a NaN value. This is my formula below. I tried adding INT() around the output values, but that seemed to break everything. Am I missing something?
IF(FIND('1',{Functional response}),-4,
IF(FIND('2',{Functional response}),-2,
IF(FIND('3',{Functional response}),0,
IF(FIND('4',{Functional response}),2,
IF(FIND('5',{Functional response}),4,"")))))
Assuming Functional response can only store a number 1 to 5 as a string a simple option in excel would be to first convert the string to a number and then use the choose function to assign a value. this works as the numbers are are sequential integers. Assuming Cell K2 has the value of Functional response, your formula could be:
=CHOOSE(--K2,-4,-2,0,2,4)
=CHOOSE(K2+0,-4,-2,0,2,4)
=CHOOSE(K2-0,-4,-2,0,2,4)
=CHOOSE(K2*1,-4,-2,0,2,4)
=CHOOSE(K2/1,-4,-2,0,2,4)
Basically sending the string of a pure number through a math operation has excel convert it to a number. By sending it through a math operation that does not change its value, you get the string as a number.
CHOOSE is like a sequential IF function Supply it with an integer as the first argument and then it will return the value from the subsequent list that matches the number. if the number you supply is greater than the number of options you will get an error.
Alternatively you could just do a straight math convertion on the number stored as a string in K2 using the following formula:
=(K2-3)*2
And as my final option, you could build a table and use VLOOKUP or INDEX/MATCH.
NOTE: If B2:B6 was stored as strings instead of numbers, K2 instead of --K2 would need to be used.

Query middle of string in MongoDB?

I need to run a query on a collection that contains a timestamp string ("YYYYMMDD:HH:MM:SS.SSSS"). I want to find all documents that have an hour less than 9 and hour greater than 14. SQL offers the MID() function but I couldn't find an equivalent function. How can I run this query in C++? If you don't know how to in C++ but in mongo shell let me know, I may be able to convert it to C++.
UPDATE
Using JohnnyHK's suggestion below, I tried:
BSONObj queryafter = BSONObjBuilder().appendRegex("date", "........:0[0-8]").obj();
BSONObj queryafter = BSONObjBuilder().appendRegex("date", "........:[17-23]").obj();
c.update(dbcol, Query(querybefore), BSON("$set"<<BSON("noise"<<"true")), false, true);
It compiled but it didn't filter correctly.
It's not terribly elegant, but you could do this in the shell using a regular expression:
db.test.find({ts: /....:..:..:0[5-8]/})
In the C++ driver you would build your query object with something like:
BSONObj query = BSONObjBuilder().appendRegex("ts", "....:..:..:0[5-8]").obj();
UPDATE
For your new requirements, it gets a little more involved, but still doable:
BSONObjBuilder().appendRegex("ts", "^........:(0[0-8]|1[5-9]|2[0-3]):..:").obj();
You could write your own little way to do it in c++. If you have a string "YYYY:MM:DD:HH:MM:SS.SSSS" then you can tokenise it around the : using strtok or stringstream. Then use atoi to convert the hours into an int and finally compare with <9 & >4.
Here is a strtok example and here is a stringstream example.
EDIT: If you are confident of the position of the hours in the string (Which you must be right) Then you can also do it this way without a for loop:
string timestamp = yourTimestamp;
int hours = atoi(timestamp.substr(12, 2).c_str())
if (hours < 9 && hours > 4)
your stuff
The explanation is, substr the section that contains the hours, turn it into a c string and convert to an int. Use the hours for compare.

How to validate input string in short time format

I've got an input string which is to represent a 24h time. Valid inputs are strings like:
7:04
07:4
7:4
07:04
And an invalid input string would be obviously something like 25:68. Is there any easy way to validate the time format, and then parse the time in epoch format given that the time is of today? Is there possibly any boost library that comes handy here?
You can do it using strptime, which is standard on Unix-like systems:
http://www.kernel.org/doc/man-pages/online/pages/man3/strptime.3.html
I see two options:
Simple regex or boost::xpressive (I like it more).
Get the numbers between semicolon and convert them to numbers, then check their values. Something like this:
string sTime("53:30");
int iSemiColon = sTime.find(':');
if (atoi(sTime.substr(0, iSemiColon).c_str()) >= 24) return false;
if (atoi(sTime.substr(iSemiColon + 1).c_str()) >= 60) return false;
See this answer. If you're not limited to STL, you could use boost.Regex for the validation part, though it's such a simple problem that you could easily roll your own solution.

How to INSERT binary std::string into BLOB

I have binary std::string and I need insert it into the BLOB (MySQL) using simple data layer I have. So, I need to execute query: ExecuteSQL((LPTSTR)strQ).
When I am creating this query string (strQ) I cannot add anything to the string after I add this binary string - it just kind if terminated and nothing can be added. I do not want to use mysql_real_escape_string because i want to keep it not only for MySQL.
Anybody to HELP PLEASE!!!
Assuming you have code that looks something like this:
std::string s = ... // populate string somehow
ExecuteSQL( (LPCSTR) s );
Then you have several problems. Forstly, the cast won't work. In C++, whenever you use a cast you are almost certainly doing something incorrect which will break your code. You need to create a null-terminated string using the std::string member function c_str():
ExecuteSQL( s.c_str() );
However, this may not fix all your your problems because you say you hava a binary string. If that string contains the zero byte, then your SQL will terminate at that character rather than the end of string. In that case you probably need to investigate binding your values explicitly.
Edit: For details of how to bind a parameter to a MySQL statement, see http://dev.mysql.com/doc/refman/5.1/en/mysql-stmt-bind-param.html