cplusplus connet mysql show data question - c++

**``` c++ connect mysql
1.
Heading
#include <mysql/mysql.h>
#include <iostream>
#include<stdio.h>
#include <unistd.h>
using namespace std;
MYSQL *conn;MYSQL_RES *res;MYSQL_ROW row;
int main(int argc,char **argv)
{
const char *sever = "127.0.0.1"; //主机
const char *user = "root"; //用户名
const char *password = "root"; //用户密码
const char *database = "demo"; //数据库名称
conn = mysql_init(NULL);
if(!mysql_real_connect(conn,sever,user,password,database,3306,NULL,0))
{
cout << "connect error" << endl;
return 0;
}
mysql_set_character_set(conn,"utf8");
while(1==1){//where there not work
cout<<"enter while"<<endl;
sleep(2);
cout<<"link ok"<<endl;
if(mysql_query(conn,"select * from person"))
{
cout << "query error" << endl;
return 0;
}
//res = mysql_store_result(conn);
res = mysql_use_result(conn);
while((row = mysql_fetch_row(res)) != NULL)
{
cout << row[0] << '\t' << row[1] << '\t' << row[2] << '\t'
<< row[3] << '\t' << row[4] << endl;
}
cout<<"query end"<<endl;
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}
```
it can show data from mysql.but why it not cycle.the while doesn't work?
i have tried many conform to explain this .but no any one can help my ,some one who can tell my why .

Related

BIO_write() is not writing to the file when using BIO_f_base64()

I am trying to learn OPENSSL and following this tutorial. I have written following code to read from a file and write the encoded form in another file.
#include <iostream>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#define MAX_BUFFER_SIZE 512
int main (int argc, char* argv[])
{
BIO* bio_out = nullptr;
BIO* bio_in = nullptr;
BIO* bio_b64 = nullptr;
int in_byte, out_byte;
char buffer[MAX_BUFFER_SIZE];
std::memset(buffer, '\0', MAX_BUFFER_SIZE);
if (argc != 3)
{
std::cout << "Usage: bio_b64_encode <source-read> <encoded-write>\n";
return 1;
}
bio_in = BIO_new_file(argv[1], "r");
bio_out = BIO_new_file(argv[2], "wb");
bio_b64 = BIO_new(BIO_f_base64());
BIO_push(bio_b64, bio_out);
in_byte = BIO_read(bio_in, buffer, MAX_BUFFER_SIZE);
while (in_byte > 0)
{
std::cout << "Read " << in_byte << " bytes.\n";
out_byte = BIO_write(bio_b64, buffer, in_byte);
std::cout << "Wrote " << out_byte << " bytes.\n";
if (in_byte != out_byte)
{
std::cout << "In bytes: " << in_byte << "and Out bytes: " << out_byte << " are not equal.\n";
BIO_free(bio_in);
BIO_free(bio_out);
return 1;
}
in_byte = BIO_read(bio_in, buffer, MAX_BUFFER_SIZE);
}
BIO_free(bio_in);
BIO_free_all(bio_b64);
return 0;
}
The code reads properly from the input file given in argv[1] and tries to write to argv[2] but when I open the output file, it is empty but it should have the encoded text from the input file.
the output of the above program when I run ./bio_b64_encode in.txt out.txt is
Read 22 bytes.
Wrote 22 bytes.
but the output file is empty. If I remove the encoding constraints and try to simply write whatever I read from input file (the code is below), it works properly.
#include <iostream>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#define MAX_BUFFER_SIZE 512
int main (int argc, char* argv[])
{
BIO* bio_out = nullptr;
BIO* bio_in = nullptr;
BIO* bio_b64 = nullptr;
int in_byte, out_byte;
char buffer[MAX_BUFFER_SIZE];
std::memset(buffer, '\0', MAX_BUFFER_SIZE);
if (argc != 3)
{
std::cout << "Usage: bio_write <file-read> <file-write>\n";
return 1;
}
bio_in = BIO_new_file(argv[1], "r");
bio_out = BIO_new_file(argv[2], "w");
in_byte = BIO_read(bio_in, buffer, MAX_BUFFER_SIZE);
while (in_byte > 0)
{
std::cout << "Read " << in_byte << " bytes.\n";
out_byte = BIO_write(bio_out, buffer, in_byte);
std::cout << "Wrote " << out_byte << " bytes.\n";
if (in_byte != out_byte)
{
std::cout << "In bytes: " << in_byte << "and Out bytes: " << out_byte << " are not equal.\n";
BIO_free(bio_in);
BIO_free(bio_out);
return 1;
}
in_byte = BIO_read(bio_in, buffer, MAX_BUFFER_SIZE);
}
BIO_free(bio_in);
BIO_free_all(bio_out);
return 0;
}
I am not able to figure out what am I missing here.

Try to connect Mysql but FAILED

I tried to connect MySQL.
But it seems like something goes wrong and I cannot get it.
I'm basically sending a simple SELECT statement to the database and checking if the operation was successful or not.
Unfortunately, this always outputs "Failed!"
#include <iostream>
#include <string>
#include <mysql/mysql.h>
using namespace std;
int main() {
const char *db = "test";
const char *server = "localhost";
const char *user = "root";
const char *password = "111222";
int port = 3306;
const char *sql = "select 1";
MYSQL *con = mysql_init(NULL);
// con = mysql_init(con);
if (con == NULL) {
cout << "Error:" << mysql_error(con) << endl;
exit(1);
}
MYSQL *new_con = mysql_real_connect(con, server, user, password, db, port, NULL, 0);
if (new_con != NULL) {
cout << "Successful!" << endl;
} else {
cout << "Failed!" << mysql_error(new_con) << endl;
}
int ret = mysql_query(new_con, sql);
if (ret != 0) {
cout << "error: " << mysql_error(new_con) << endl;
}
return 0;
}
Adapted from https://dev.mysql.com/doc/c-api/8.0/en/mysql-real-connect.html
MYSQL mysql;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost","root","111222","test",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
else {
cout << "Successfully connected." << endl;
}
What does this sample code print ?

Redis: Can I store a c / c ++ structure type containing pointers to array in Redis?

I have a struct like this:
struct father {
int child_n; //the number of children
int *child_age; //childrens' age
}
Can I store this kind of struct that have variable-length array in Redis?
I solved it with nested *Redis list, here is an example:
#include <iostream>
#include <string>
#include <cstring>
#include <hiredis/hiredis.h>
typedef struct stu {
int stu_num;
int* stu_age;
} stu;
using namespace std;
void put_stus(string key, stu students) {
// connect to Redis server
redisContext *connect = redisConnect("127.0.0.1",6379);
if(!connect) {
cout << "connect to redis fail" << endl;
exit(-1);
}
// upload students' age
redisReply *r;
for(int i = 0;i < students.stu_num;i++) {
r = (redisReply *)redisCommand(connect, "RPUSH %s:age %b",key,&students.stu_age[i],sizeof(students.stu_age[i]));
if(r->type == REDIS_REPLY_ERROR) {
cout << "put_stus error" << endl;
exit(-1);
}
freeReplyObject(r);
}
// upload struct
r = (redisReply *)redisCommand(connect, "SET %s %b",key,&students,sizeof(students));
if(r->type == REDIS_REPLY_ERROR) {
cout << "put_stus error" <<endl;
exit(-1);
}
freeReplyObject(r);
redisFree(connect);
}
void get_stus(string key, stu *r_students) {
// connect to Redis Server
redisContext *connect = redisConnect("127.0.0.1",6379);
if(!connect) {
cout << "connect to redis fail" << endl;
exit(-1);
}
// Get sturct
redisReply *r;
r = (redisReply *)redisCommand(connect, "GET %s",key);
if(r->type == REDIS_REPLY_ERROR) {
cout << "get_stus error" <<endl;
exit(-1);
}
memcpy(r_students,r->str,r->len);
// Get students' age
r_students->stu_age = (int *)malloc(r_students->stu_num * sizeof(int));
if(!r_students->stu_age) {
cout << "malloc error" << endl;
exit(-1);
}
r = (redisReply *)redisCommand(connect, "LRANGE %s:age 0 -1",key);
if(r->type == REDIS_REPLY_ERROR) {
cout << "get_stus error" <<endl;
exit(-1);
}
else if(r->type == REDIS_REPLY_ARRAY) {
for(int i = 0;i < r_students->stu_num;i++) {
memcpy(r_students->stu_age + i, r->element[i]->str, r->element[i]->len);
}
}
freeReplyObject(r);
redisFree(connect);
}
int main() {
stu stu_a,stu_b;
stu_a.stu_num = 3;
int ages[3] = {1,2,3};
stu_a.stu_age = ages;
put_stus("test1",stu_a);
get_stus("test1",&stu_b);
cout << "stu_b.stu_num is " << stu_b.stu_num << endl;
for(int i = 0;i < stu_b.stu_num;i++) {
cout << "stu_b.stu_age[" << i << "] is " << stu_b.stu_age[i] << endl;
}
}

C++ Removing the command-line

when i run the compieled .exe-file it shows the commandline, and I wish to remove it.. But I don't know nothing about C++, so I wonder how one can do it?
This is not my script... Just so you know.
#include <iostream>
#include <windows.h>
using namespace std;
typedef long (*GetFunctionCount) (void) __attribute__((stdcall));
typedef long (*GetFunctionInfo)(int, void*&, char*&) __attribute__((stdcall));
typedef void (*Setup)(char*,char*,long,long,char*) __attribute__((stdcall));
int main(int argc, char** argv) {
HMODULE libsmart = LoadLibrary("./libsmart.dll");
cout << "Library: " << libsmart << '\n';
cout << "GetFunctionCount: " << (void*)GetProcAddress(libsmart, "GetFunctionCount") << '\n';
cout << "GetFunctionInfo: " << (void*)GetProcAddress(libsmart, "GetFunctionInfo") << '\n';
GetFunctionCount count = (GetFunctionCount) GetProcAddress(libsmart, "GetFunctionCount");
GetFunctionInfo info = (GetFunctionInfo) GetProcAddress(libsmart, "GetFunctionInfo");
int exports = count();
cout << "#Exports = " << count() << '\n';
for (int i = 0; i < exports; i++) {
char* def = new char[1024];
void* addr;
info(i,addr,def);
cout << '\t' << addr << " = " << def << '\n';
delete def;
}
cout << "Starting SMART...\n";
Setup setup = (Setup) GetProcAddress(libsmart, "std_setup");
setup((char*)"http://world19.runescape.com/", (char*)",f5", 765, 503,(char*)"");
while (true) Sleep(1000);
return 0;
}
#define _WIN32_WINNT 0x0500
#include <iostream>
#include <windows.h>
using namespace std;
typedef long (*GetFunctionCount) (void) __attribute__((stdcall));
typedef long (*GetFunctionInfo)(int, void*&, char*&) __attribute__((stdcall));
typedef void (*Setup)(char*,char*,long,long,char*) __attribute__((stdcall));
int main(int argc, char** argv) {
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );
HMODULE libsmart = LoadLibrary("./libsmart.dll");
cout << "Library: " << libsmart << '\n';
cout << "GetFunctionCount: " << (void*)GetProcAddress(libsmart, "GetFunctionCount") << '\n';
cout << "GetFunctionInfo: " << (void*)GetProcAddress(libsmart, "GetFunctionInfo") << '\n';
GetFunctionCount count = (GetFunctionCount) GetProcAddress(libsmart, "GetFunctionCount");
GetFunctionInfo info = (GetFunctionInfo) GetProcAddress(libsmart, "GetFunctionInfo");
int exports = count();
cout << "#Exports = " << count() << '\n';
for (int i = 0; i < exports; i++) {
char* def = new char[1024];
void* addr;
info(i,addr,def);
cout << '\t' << addr << " = " << def << '\n';
delete def;
}
cout << "Starting SMART...\n";
Setup setup = (Setup) GetProcAddress(libsmart, "std_setup");
setup((char*)"http://world19.runescape.com/", (char*)",f5", 765, 503,(char*)"");
while (true) Sleep(1000);
return 0;
}
Should work. Don't forget the definition at the top.
If you wish to remove the part that shows the commandline, any line that starts with cout << is showing something to the screen.
However, the command line is primarily stored in argv, and I don't see any reference to that in your program.
You will want to compile and link it to the windows subsystem, rather than the console subsystem.

How enable dragging a file on the *.exe and get it as parameter?

What do I have to do to make my program use a file that has been dragged and dropped onto its icon as a parameter?
My current main method looks like this:
int main(int argc, char* argv[])
{
if (argc != 2) {
cout << "ERROR: Wrong amount of arguments!" << endl;
cout << "\n" << "Programm closed...\n\n" << endl;
exit(1);
return 0;
}
Converter a(argv[1]);
// ...
cout << "\n" << "Programm finished...\n\n" << endl;
// cin.ignore();
return 0;
}
What I'd really like to be able to do is select 10 (or so) files, drop them onto the EXE, and process them from within my application.
EDIT:
The incomming parameter is used as filename, constructed in the cunstructor.
Converter::Converter(char* file) {
// string filename is a global variable
filename = file;
myfile.open(filename.c_str(), ios_base::in);
}
The method where the textfile gets read:
string Converter::readTextFile() {
char c;
string txt = "";
if (myfile.is_open()) {
while (!myfile.eof()) {
myfile.get(c);
txt += c;
}
} else {
error("ERROR: can't open file:", filename.c_str());
}
return txt;
}
EDIT2:
deleted
Update:
I got again to this point.
Actual Main method:
// File path as argument
int main(int argc, char* argv[]) {
if (argc < 2) {
cout
<< "ERROR: Wrong amount of arguments! Give at least one argument ...\n"
<< endl;
cout << "\n" << "Programm closed...\n\n" << endl;
cin.ignore();
exit(1);
return 0;
}
vector<string> files;
for (int g = 1; g < argc; g++) {
string s = argv[g];
string filename = "";
int pos = s.find_last_of("\\", s.size());
if (pos != -1) {
filename = s.substr(pos + 1);
cout << "argv[1] " << argv[1] << endl;
cout << "\n filename: " << filename << "\n pos: " << pos << endl;
files.push_back(filename);
}
files.push_back(s);
}
for (unsigned int k = 0; k < files.size(); k++)
{
cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl;
Converter a(files.at(k).c_str());
a.getATCommandsFromCSV();
}
cout << "\n" << "Programm finished...\n\n" << endl;
cin.ignore();
return 0;
}
Actually the console window apears for maybe 0.5 sec and closes again.
It doen't stop on any of my cin.ignore(); Maybe it doesn't get there?
Can anyone help?
Your program does not need to do anything special apart from handling command-line arguments. When you drag-drop a file onto an application in Explorer it does nothing more than to pass the file name as argument to the program. Likewise for multiple files.
If all you expect is a list of file names, then just iterate over all arguments, do whatever you want with them and be done. This will work for zero to almost arbitrarily many arguments.
Maybe you could write a test program like this:
int main(int argc, char* argv[])
{
// argv[0] is not interesting, since it's just your program's path.
for (int i = 1; i < argc, ++i)
cout << "argv[" << i << "] is " << argv[i] << endl;
return 0;
}
And see what happens after you throw different files at it.
EDIT: Just look at Joey's answer.
Answer to the main question
TO SEE THE ANSWER TO YOUR LAST PROBLEM SEE BOTTOM OF THIS ANSWER
All drag&dropped files are get-able as argv[orderOfTheFile] (orderOfTheFile is from 1-n),
however how does windows create that order, now that is a real mystery...
Anyway let's say I would create 26 plain text files ( *.txt ), from a.txt to z.txt on my Desktop,
now if I would drag&dropped them on my ArgsPrinter_c++.exe located directly on C:\ drive,
an output would be similar to this:
argc = 27
argv[0] = C:\ArgsPrinter_c++.exe
argv[1] = C:\Users\MyUserName\Desktop\c.txt
argv[2] = C:\Users\MyUserName\Desktop\d.txt
argv[3] = C:\Users\MyUserName\Desktop\e.txt
argv[4] = C:\Users\MyUserName\Desktop\f.txt
argv[5] = C:\Users\MyUserName\Desktop\g.txt
argv[6] = C:\Users\MyUserName\Desktop\h.txt
argv[7] = C:\Users\MyUserName\Desktop\i.txt
argv[8] = C:\Users\MyUserName\Desktop\j.txt
argv[9] = C:\Users\MyUserName\Desktop\k.txt
argv[10] = C:\Users\MyUserName\Desktop\l.txt
argv[11] = C:\Users\MyUserName\Desktop\m.txt
argv[12] = C:\Users\MyUserName\Desktop\n.txt
argv[13] = C:\Users\MyUserName\Desktop\o.txt
argv[14] = C:\Users\MyUserName\Desktop\p.txt
argv[15] = C:\Users\MyUserName\Desktop\q.txt
argv[16] = C:\Users\MyUserName\Desktop\r.txt
argv[17] = C:\Users\MyUserName\Desktop\s.txt
argv[18] = C:\Users\MyUserName\Desktop\t.txt
argv[19] = C:\Users\MyUserName\Desktop\u.txt
argv[20] = C:\Users\MyUserName\Desktop\v.txt
argv[21] = C:\Users\MyUserName\Desktop\w.txt
argv[22] = C:\Users\MyUserName\Desktop\x.txt
argv[23] = C:\Users\MyUserName\Desktop\y.txt
argv[24] = C:\Users\MyUserName\Desktop\z.txt
argv[25] = C:\Users\MyUserName\Desktop\a.txt
argv[26] = C:\Users\MyUserName\Desktop\b.txt
My ArgsPrinter_c++.exe source code:
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "argc = " << argc << endl;
for(int i = 0; i < argc; i++)
cout << "argv[" << i << "] = " << argv[i] << endl;
std::cin.ignore();
return 0;
}
Your last problem
I have created a simple program that creates only a sceleton of your class so it can be used, and the program's main itself ran JUST FINE => if your program exits too soon, the problem will be in your class...
Tested source code:
#include <iostream>
#include <vector>
using namespace std;
class Converter{
public:
Converter(const char* f){ cout << f << endl; }
void getATCommandsFromCSV(){ cout << "called getATCommandsFromCSV" << endl; }
};
int main(int argc, char* argv[]) {
vector<string> files;
for (int g = 1; g < argc; g++) {
string s = argv[g];
string filename = "";
int pos = s.find_last_of("\\", s.size());
if (pos != -1) {
filename = s.substr(pos + 1);
cout << "argv[1] " << argv[1] << endl;
cout << "\n filename: " << filename << "\n pos: " << pos << endl;
files.push_back(filename);
}
files.push_back(s);
}
for (unsigned int k = 0; k < files.size(); k++)
{
cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl;
Converter a(files.at(k).c_str());
a.getATCommandsFromCSV();
}
cout << "\n" << "Programm finished...\n\n" << endl;
cin.ignore();
return 0;
}