LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mysql c-api client with connection parameters at runtime (https://www.linuxquestions.org/questions/programming-9/mysql-c-api-client-with-connection-parameters-at-runtime-181668/)

rotkaffm 05-15-2004 04:50 AM

mysql c-api client with connection parameters at runtime
 
I use all Software from SUSE Linux 9.0. If I try to use connection parameters reading them at runntime I alwas have an accessviolation (Speicherzugriffsfehler) Reading the data from the mysql table is still done but with an (Speicherzugriffsfehler) at the end. Using srtcpy makes the problem. The commented version is ok.
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
cout << "Hello World" << endl;
MYSQL *mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int t,r;
char* db="zahlen";
unsigned int error;



mysql=mysql_init(NULL);
if(!mysql_real_connect(mysql,"localhost","root","hase",db,0,NULL,0))
{
printf("Error connecting\n");
}else printf("Connected ... \n");


// # # # # # # # # # # # # # #
//query="select * from nummern";
strcpy(query,"select * from nummern");
cout << "strlen(query)=" << strlen(query) << endl;
for (int i=0;i<strlen(query)+10;i++)
{
cout << query[i];
}
cout << endl << "ende query[i]" << endl;
t=mysql_real_query(mysql,query,(unsigned int) strlen(query));
if(t)
{
printf("error quera");
}else printf("Query made \n");



res=mysql_use_result(mysql);

while (row=mysql_fetch_row(res))
{
for(t=0;t<mysql_num_fields(res);t++){
printf("%s ",row[t]);
}
printf("\n");
error = mysql_errno(mysql);
cout << "myFehler: " << error << endl;
}

}

fatah 05-15-2004 07:16 AM

char *query; /* and */ strcpy(query,"select * from nummern"); // erm...
you should first allocate a size of memory for (char *)query.
by using
query = new char[BUFSIZ]; // (++ version) or...
query = (char *)malloc(BUFSIZ * sizeof(char));


All times are GMT -5. The time now is 03:06 PM.