[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] C++ og mysql



Prøv dette eksempel. Der er lige et par ting, som skal ændres og 
tilpasses din egen maskine, eller hvor det nu skal køres henne. Hent 
"MySQL Reference Manual" hos TcX, som har lavet MySQL – og udskriv det 
hele. Den er virkelig god som reference til SQL og C api'et.

Dette eksempel er godt nok kun i C, men den samme kode kan jo bare bruges 
i de rette klasser i C++. 

- Ture Munter


>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 4/15/2001, 11:14:01 AM, "Kjeld Vang Larsen" <sslug@sslug> wrote 
regarding [CPROG] C++ og mysql:


> Halløj

> Jeg kunne godt tænkt mig at bruge MySQL og C++ sammen - hvis
> det kan gøres!? Er der nogen der ligger inde med et simpelt eks.
> på hvordan jeg connecter til databasen?

> MVH

> Kjeld
# Generated automatically from Makefile.in by configure.
##
##  $Id: Makefile.in,v 1.8 1999/05/14 17:57:17 sbooth Exp $
##
##  Copyright (C) 1996, 1997, 1998, 1999 Stephen F. Booth
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##

##
## Sample Makefile for a cgi application written using GNU Cgicc
##

##
## The name of the cgi target
TARGET=mysql_test

##
## The name(s) of your source file(s)
SOURCES=mysql_test.c

##
## The name(s) of the object file(s)
OBJECTS=	$(SOURCES:%.c=%.o)

##
## Don't change anything below this point unless you know what you're doing
##

##
## Directory info
prefix= 	/usr/local
exec_prefix= 	${prefix}
libdir= 	${exec_prefix}/lib
includedir= 	${prefix}/include

##
## Compiler setup
CXX=		g++
LIBS= 		 -L$(libdir) -L/usr/local/mysql/lib -lmysqlclient
DEFS=		-DHAVE_CONFIG_H

INCLUDES= 	-I$(includedir) -I/usr/local/mysql/include

## On Solaris (probably SVR4 systems), use -R
#LD_FLAGS=	 -R$(libdir)

## On Linux (probably BSD systems), use -rpath
#LD_FLAGS=	 -rpath$(libdir)

CXXFLAGS= 	-g -O2
CPPFLAGS=	

COMPILE= 	$(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS)

##
## Target information
all: $(TARGET)

$(TARGET): $(OBJECTS)
	$(CXX) $(LD_FLAGS) -o $(TARGET) $(OBJECTS) $(LIBS)
       
clean:
	-rm -f $(OBJECTS) $(TARGET)

##
## Compilation rules
%.o : %.cc
	$(COMPILE) -c $<

// dette path skal måske ændres, alt efter hvor mysql er installeret
#include "/usr/local/mysql/include/mysql.h"
#include <stdio.h>

void main()
{
	MYSQL* mysql_handle = mysql_init(NULL);
	MYSQL_RES *result;

  printf("Content-Type: text/html\n\n");

	
	// Ikke nok memory til at allokere object
	if(mysql_handle == NULL)
	  { printf("MySQL error: %s", mysql_error(mysql_handle));
			exit(1);
	  }
	  
  // Connect til MYSQL-databasen
  // Her skal du selvfølgelig ændre user, passwd og database til det respektive
  // brugernavn, kodeord og navnet på databasen.
	if(!mysql_real_connect(mysql_handle, "localhost", "user", "passwd", "database", 0, NULL, 0))
	  { 
	  	printf("MySQL error: %s", mysql_error(mysql_handle));
	    exit(1);
	  }
	
	// FÅ DATABASEN TIL AT GØRE NOGET
	// Her skal så bare stå noget SQL
	if(mysql_query(mysql_handle, "SELECT nummer,navn, brugernavn, kodeord FROM medlemmer"))
	  {
 			// error
	  	printf("MySQL error: %s", mysql_error(mysql_handle));
			exit(1);
	  }	
	else // ingen fejl -- udskriv resultater fra søgning.
	  {
	  	MYSQL_ROW row;
			unsigned int num_fields;
			unsigned int i;
			
	  	result = mysql_store_result(mysql_handle);
			num_fields = mysql_num_fields(result);
			while((row = 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[i] ? row[i] : "NULL");
					}
					printf("<BR>");
			}
		}
	
	// RYD OP!
	mysql_free_result(result);	
	mysql_close(mysql_handle);	
}


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *