phpbar.de logo

Mailinglisten-Archive

MySQL mit C Problem

MySQL mit C Problem

Patrik Lehmann mysql_(at)_lists.phpcenter.de
Tue, 10 Apr 2001 13:54:23 +0200


Hallo,

ich habe ein Problem mit dem angehängten (Test-) Programm:
in der Zeile 147 (Abfrage mit select)  bekomme ich folgenden Fehler:

Program received signal SIGSEGV, Segmentation fault. chunk_alloc
(ar_ptr=0x401112c0, nb=40) at malloc.c:2805 2805

Backtrace:
#0  chunk_alloc (ar_ptr=0x401112c0, nb=40) at malloc.c:2805
#1  0x4008583c in malloc () at malloc.c:2181
#2  0x40028efa in my_malloc () from /usr/lib/libmysqlclient.so.6
#3  0x40024d9f in read_rows () from /usr/lib/libmysqlclient.so.6
#4  0x400263c2 in mysql_real_query () from /usr/lib/libmysqlclient.so.6
#5  0x400273f8 in mysql_query () from /usr/lib/libmysqlclient.so.6
#6  0x8048eb5 in main () at main.c:147

Danke für die Hilfe,

Patrik Lehmann


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

int main(void)
{
  MYSQL *db;
  MYSQL_RES *result;
  MYSQL_FIELD *fields;
  MYSQL_ROW *row,*last_row;
  unsigned i,j,jend, num_fields;
  unsigned long **lengths;
  // Initialisation
  if ((db=mysql_init(db))==NULL) {
 printf("Not enough memory\n");
 exit (1);
  }

  if (!(db=mysql_connect(db,"localhost",NULL,NULL))){
 fprintf(stderr, "Failed to send options: Error: %s\n", mysql_error(db));
 exit (2);
  }

  // DB Select
  if (mysql_select_db(db, "test")!=0) {
 fprintf(stderr, "Failed to selct database: Error: %s\n", mysql_error(db));
 exit (3);
  }
  row=(MYSQL_ROW *) malloc(sizeof(row));
  last_row=(MYSQL_ROW *)malloc(sizeof(last_row));
  lengths=(unsigned long  **)malloc(sizeof(lengths));
  // List tables
  if ((result=mysql_list_tables(db,""))==NULL) {
   fprintf(stderr, "Failed to list tables: Error: %s\n", mysql_error(db));
 exit (4);
  }
  num_fields = mysql_num_fields(result);
  fields = mysql_fetch_fields(result);
  for(i = 0; i < num_fields; i++)
  {
 printf("%s:\n", fields[i].name);
  }
  row=(MYSQL_ROW *) malloc(sizeof(row));
  while ((row[0] = mysql_fetch_row(result)))
  {
 unsigned long *lengths;
 lengths = mysql_fetch_lengths(result);
 for(i = 0; i < num_fields; i++)
 {
  printf("[%.*s] ", (int) lengths[i], row[0][i] ? row[0][i] : "NULL");
 }
 printf("\t");
  }
  printf("\n");
  printf("\n");
  // Load Tables
  if ((mysql_query(db,"select pcid,typ, buydate,vname from PC left join
hersteller using (vid);")) != 0) {
    fprintf(stderr, "Failed to select Data: Error: %s\n", mysql_error(db));
  exit (5);
  }
  if ((result=mysql_store_result(db)) == NULL) {
 fprintf(stderr, "Failed to store result: Error: %s\n", mysql_error(db));
 exit (6);
  }

  // Show fields
  num_fields = mysql_num_fields(result);
  fields = mysql_fetch_fields(result);
  for(i = 0; i < num_fields; i++)
  {
   printf("[%s]:\t", fields[i].name);
  }
  printf("\n");

  // Show Tables
  last_row=(MYSQL_ROW *)malloc(sizeof(last_row));
  lengths=(unsigned long  **)malloc(sizeof(lengths));
  for(i = 0; i < num_fields; i++) lengths[0][i]=0;
  j=1;

  while ((row[j] = mysql_fetch_row(result)))
  {
 lengths[j]=mysql_fetch_lengths(result);
 j++;
  }
  jend=j;
  for (j=1; j < jend; j++) {
 for (i=0; i < num_fields; i++) {
  lengths[0][i]= strlen(row[j][i] ? row[j][i] : "NULL");
  printf("[%.*s] ", (int) lengths[j][i], row[j][i] ? row[j][i] : "NULL");
  printf("%*.0d ", (int)( lengths[0][i]-lengths[j][i]),0);
 }
 printf("\n");
  }
  printf("\n");

  // CREATE Table
  printf("creating table:\n");
  if ((mysql_query(db,"create table users (user_id integer AUTO_INCREMENT NOT
NULL, uname varchar(40), pcid integer references PC, job varchar(20), primary
key(user_id))")) != 0) {
  fprintf(stderr, "Failed to create table: Error: %s\n", mysql_error(db));

  }
  // ADD Rows
  printf("adding rows(1):\n ");
  if ((mysql_query(db,"insert into users values ( 0,'Heinz',
1,'Personalchef')")) != 0) {
   fprintf(stderr, "Failed to iser into table: Error: %s\n", mysql_error(db));

  }
  printf("adding rows(2):\n ");
  if ((mysql_query(db,"insert into users values ( 0,'Paul', 3,'DB-Admin')")) !=
0) {
   fprintf(stderr, "Failed to iser into table: Error: %s\n", mysql_error(db));

  }
  // Load Tables

 printf("loading table:\n ");
  if ((mysql_query(db,"select user_id, uname, job, typ, buydate from users left
join PC using(pcid)")) != 0) {
   fprintf(stderr, "Failed to select Data: Error: %s\n", mysql_error(db));
 exit (1);
  }
  printf("Done:\n ");
  result =malloc(sizeof(result));
  printf("Storing result:\n ");
  if ((result=mysql_store_result(db)) == NULL) {
 fprintf(stderr, "Failed to store result: Error: %s\n", mysql_error(db));
 exit (7);
  }

  //Show fields
  printf("Showing fields:\n ");
  num_fields = mysql_num_fields(result);
  fields = mysql_fetch_fields(result);
  for(i = 0; i < num_fields; i++)
  {
 printf("[%s]:\t", fields[i].name);
  }
  printf("\n");


  // Show Tables
  free(lengths);
  lengths=(unsigned long  **)malloc(sizeof(lengths));
  //free(result);
  result=(MYSQL_RES*)malloc(sizeof(result));

  for(i = 0; i < num_fields; i++) lengths[0][i]=0;
  j=1;

  while ((row[j] = mysql_fetch_row(result)))
  {
 lengths[j]=mysql_fetch_lengths(result);
 j++;
  }
  jend=j;
  for (j=1; j < jend; j++) {
 for (i=0; i < num_fields; i++) {
  lengths[0][i]= strlen(row[j][i] ? row[j][i] : "NULL");
  printf("[%.*s] ", (int) lengths[j][i], row[j][i] ? row[j][i] : "NULL");
  printf("%*.0d ", (int)( lengths[0][i]-lengths[j][i]),0);
 }
 printf("\n");
  }


  printf("\n");
  printf("droping tables:\n ");
  if ((mysql_query(db,"drop table users")) != 0) {
   fprintf(stderr, "Failed to drop table: Error: %s\n", mysql_error(db));
 exit (8);
  }
  mysql_close(db);
  free(result);
  free(lengths);
  free(last_row);
  free(row);

  return EXIT_SUCCESS;
}


---
*** Weitere Infos zur Mailingliste und MySQL unter http://www.4t2.com/mysql 



php::bar PHP Wiki   -   Listenarchive