2 messages in com.mysql.lists.win32Re: MySQL and C| From | Sent On | Attachments |
|---|---|---|
| Michael Louie Loria | 09 Aug 2005 20:47 | |
| Marcel Lanz | 09 Aug 2005 23:53 |
| Subject: | Re: MySQL and C![]() |
|---|---|
| From: | Marcel Lanz (marc...@ds9.ch) |
| Date: | 08/09/2005 11:53:43 PM |
| List: | com.mysql.lists.win32 |
Could you give some sample codes?
g++ -o mysql_sample `../mysql/usr/bin/mysql_config --cflags --libs`
mysql_sample.c
adapt path to mysql_config
// $host = "localhost"; // $dbuser = "your-db"; // $dbpassword = "mimi"; // $database = "your-db";
//#if defined(_WIN32) || defined(_WIN64) //#include <windows.h> //#endif
#include <stdio.h> #include <stdlib.h> #include "mysql.h"
#define SELECT_QUERY "select * from marke"
int main(int argc, char **argv) { int count, num; MYSQL mysql,*sock; MYSQL_RES *result; char qbuf[160];
mysql_init(&mysql);
if (!(sock =
mysql_real_connect(&mysql,"127.0.0.1","your-db","mimi","your-db",3306,NULL,0)))
{
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
perror("");
exit(1);
}
count = 0; num = atoi(argv[2]);
sprintf(qbuf,SELECT_QUERY);
if(mysql_query(sock,qbuf)) { fprintf(stderr,"Query failed (%s)\n",mysql_error(sock)); exit(1); } if (!(result=mysql_store_result(sock))) { fprintf(stderr,"Couldn't get result from %s\n", mysql_error(sock)); exit(1); }
MYSQL_ROW row; int num_fields = mysql_num_fields(result);
printf("num_fields: %i\n", num_fields);
while ((row = mysql_fetch_row(result))) { unsigned long *lengths; lengths = mysql_fetch_lengths(result); for(int i = 0; i < num_fields; i++) { printf("[%.*s] ", (int) lengths[i], row[i] ? row[i] : "NULL"); } printf("\n"); }
mysql_close(sock); exit(0); return 0; /* Keep some compilers happy */ }




