[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: Mysql C Api



Subject: [linux-delhi] Mysql C Api


> Hello folks,
> How can I create a table in MySQL using C APIs. I
> cannnot find any API in the manual which can do the
> same.
> Thanks in advance,
> Sunil

Here is an extract.....
I hope it helps.
please remember....u need to compile it with
-lmysqlclient -lz options.


#include <string.h>
#include <stdio.h>
#include "/usr/include/mysql/mysql.h"


MYSQL  imysql ;
MYSQL_RES * mydata;
unsigned int num_fields;
unsigned int num_rows;
unsigned long *lengths;


MYSQL_ROW row,;
MYSQL_FIELD *fields;

char  a[241]="";
char  querystring[241]="";


void mysqlinit(char user[20], char password[20], char db[20]);

void main()
{
mysqlinit("username","userpassword","database");

 a[0] ='\0';
 strcat(a,"Create Table ");
 strcat(a,tablename);
 strcat(a,"(pepsi bigint Not Null Primary,coke bigint Not Null);");
 printf(a);
 strcpy(querystring,a);

if (!mysql_real_query(&imysql,querystring,strlen(querystring)))
    {    mydata = mysql_store_result(&imysql);    }
    else
    {   printf("Oops ! Error %d:
%s\n",mysql_errno(&imysql),mysql_error(&imysql));
 mysql_close(&imysql);
 exit(0);
    }

mysql_close(&imysql);
}


void mysqlinit(char user[20], char password[20], char db[20])
{
mysql_init(&imysql);
mysql_options(&imysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
if (!mysql_real_connect(&imysql,"",user,password,db,0,NULL,0))
    {
    fprintf(stderr,"FAILED !!!!   Error %s\n",mysql_error(&imysql));
    }
    else
    {
    printf( "Connection with Database established !\n");
    }
}