LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-15-2004, 03:50 AM   #1
rotkaffm
LQ Newbie
 
Registered: May 2004
Posts: 1

Rep: Reputation: 0
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;
}

}
 
Old 05-15-2004, 06:16 AM   #2
fatah
LQ Newbie
 
Registered: May 2004
Location: Shah Alam
Posts: 2

Rep: Reputation: 0
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));

Last edited by fatah; 05-15-2004 at 06:18 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Client API I_AM Linux - Newbie 1 06-27-2005 02:52 AM
mysql client api I_AM Linux - Newbie 1 06-23-2005 11:48 PM
linking C api for mysql toships Programming 3 04-08-2004 11:29 PM
msql++ c++ api for mysql nakkaya Linux - Software 0 04-19-2003 03:56 PM
c++ and mysql api nakkaya Programming 4 03-28-2003 11:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration