Feb 4, 2012 at 6:40pm Feb 4, 2012 at 6:40pm UTC
I have an array of unsigned chars
unsigned char* stuff;
I need to get this into a mysql database to store.
My question is how do i get this into some sort of string (my insert or update query).
I have tried using sprintf and strcat but to no avail.
Thanks in advance.
Feb 4, 2012 at 6:41pm Feb 4, 2012 at 6:41pm UTC
Huh? That is a string. If it's not 0 terminated you'll have to append a 0 at the end, but other than that...
Feb 4, 2012 at 7:04pm Feb 4, 2012 at 7:04pm UTC
1 2 3 4
unsigned char * stuff = "Hello World" ;
std::string MyString; //Requires #include <string>
MyString = stuff;
otherwise a
signed char *
or just a
char *
is a c-type string which would work too.
Last edited on Feb 4, 2012 at 7:05pm Feb 4, 2012 at 7:05pm UTC
Feb 4, 2012 at 7:07pm Feb 4, 2012 at 7:07pm UTC
I think my issue is that the unsigned char* was not terminated with 0. It is data extracted from image analysis software.
What is a good way to add a terminating character to a copy of this array?
Feb 4, 2012 at 7:08pm Feb 4, 2012 at 7:08pm UTC
@OsiumFramework :
While char* is a pointer to a char, the compilers treat this as a "special case". This facilitates the c-strings.
Feb 4, 2012 at 7:12pm Feb 4, 2012 at 7:12pm UTC
To put your data into a MySQL database, you'll need the MySQL C++ Connector API.
http://forge.mysql.com/wiki/Connector_C%2B%2B
I don't know if they've improved it any, but when I first configured it, the documentation was so poor that I had to resort to posting on their message-board for certain help involving how to build statically.
If you need any help setting it up, post back.
Feb 4, 2012 at 7:13pm Feb 4, 2012 at 7:13pm UTC
Mysql is not the issue. I have that setup. Thank you.
Feb 4, 2012 at 7:14pm Feb 4, 2012 at 7:14pm UTC
I speak not of installing MySQL, but installing the C++ headers required to interact with MySQL through your program.
* If you have the C++ headers and libraries in place, then you can simply refer to example code sources found on the forge.
Last edited on Feb 4, 2012 at 7:15pm Feb 4, 2012 at 7:15pm UTC
Feb 4, 2012 at 7:46pm Feb 4, 2012 at 7:46pm UTC
Mysql is not the issue.
Creating the query is.
I have an array of unsigned chars, data from an image analysis class. I need to insert this data into a string query.
When i use sprintf, the data gets inserted into the string as NLT??
i have tried string but when i go
stringVar = data
I get an ambiguous overloaded operator error for =
i have tried adding a 0 at the end of the array
templByte2 = new unsigned char[size+1];
templByte2 = templByte;
templByte2[size] = 0;
where size is the size of the original data
but i still get this NTL?? issue when i try to cout or printf
Any help would much appreciated. Thanks.
Feb 4, 2012 at 8:31pm Feb 4, 2012 at 8:31pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <sstream>
int main()
{
unsigned int length; // = ? 5
unsigned char data[]; // = ? {0, 1, 2, 3, 4}
std::stringstream ss;
for (int i = 0; i < length; ++i)
{
ss >> data[i];
}
std::string stringRepresentation = ss.str(); // "01234"
connector.executeQuery(useStringRepresentationHere);
}
Might be too slow for your use. Benchmark it.
Last edited on Feb 4, 2012 at 8:33pm Feb 4, 2012 at 8:33pm UTC
Feb 4, 2012 at 8:51pm Feb 4, 2012 at 8:51pm UTC
This is great. Thanks.
Now my issue is when i want to put this into the database
string q;
q = "INSERT INTO table (data) VALUES ('";
q += temp;
q += "'";
when i call q.c_str()
i get
INSERT INTO table (data) VALUES ('NLT??
instead of the real data.
Feb 4, 2012 at 9:13pm Feb 4, 2012 at 9:13pm UTC
Why are you calling q.c_str()? The connector API uses the SQLString class for queries.
1 2 3 4 5
std::string stringRepresentation = seeCodeAbove;
SQLString query = "INSERT INTO `table` (`data`) VALUES ('" ;
query.append(stringRepresentation);
query.append("')" );
connector.execute(query);
Last edited on Feb 4, 2012 at 9:15pm Feb 4, 2012 at 9:15pm UTC
Feb 6, 2012 at 7:00pm Feb 6, 2012 at 7:00pm UTC
in mysql connector what is the function to escape a string?
Feb 7, 2012 at 12:56am Feb 7, 2012 at 12:56am UTC
I'm by no means an expert with that API, so I'd advise you to check with the library maintainers (if you can get in contact with them, they never bothered to answer most of my questions when I posted on their board). Sorry, but I think I've outlived my usefulness on this topic.
Feb 7, 2012 at 2:14am Feb 7, 2012 at 2:14am UTC
I gave up on mysql connector. I am going with mysql++, appears to be much more powerful.