cancel
Showing results for 
Search instead for 
Did you mean: 

mysql sample code

SOLVED
Go to solution
steven chang_1
Regular Advisor

mysql sample code

hello:

Is anybody write mysql application through it's C API. Would you mind send me the code about insert. Thank you!! Here is the query sample code I write!!
#include
#include

int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query,*input;
int t;
unsigned long r;
char *insert_value[2];


mysql_init(&mysql);
if(!mysql_real_connect(&mysql,"localhost","mysql1","mysql1","mysql1",0,NULL,0))
printf("Error connecting to database:%s\n",mysql_error(&mysql));
else
printf("Mysql Connected ...\n");
query="select * from test1";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error makeing query:%s\n",mysql_error(&mysql));
else
printf("Query made.....\n");
res=mysql_store_result(&mysql);
printf("the number rows of result set is %u\n",(unsigned long)mysql_num_rows(res));
for(r=0;r<(unsigned long)mysql_num_rows(res);r++)
{
row=mysql_fetch_row(res);
if(row<0) break;
for(t=0;t {
printf("%s ",row[t]);
insert_value[t]=row[t];
}
printf(" name is %s, and salary is %d\n",insert_value[0],atoi(insert_value[1]));
printf("\n");
}
mysql_free_result(res);
mysql_close(&mysql);
}

steven
4 REPLIES
Jean-Luc Oudart
Honored Contributor
Solution

Re: mysql sample code

steven chang_1
Regular Advisor

Re: mysql sample code

hi:

I was misleading by the function name "mysql_real_query". You could use insert,delete, update, select for this function.The follwoing is the sample code for insert, update.

#include
#include

int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int t;
unsigned long r;


mysql_init(&mysql);
if(!mysql_real_connect(&mysql,"localhost","mysql1","mysql1","mysql1",0,NULL,0))
printf("Error connecting to database:%s\n",mysql_error(&mysql));
else
printf("Mysql Connected ...\n");
query="insert into test values('insert')";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making insert table test:%s\n",mysql_error(&mysql));
else
printf("Insert made.to test table....\n");
query="update test set name='update' where name='insert'";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making update table test:%s\n",mysql_error(&mysql));
else
printf("Update made.to test table....\n");
query="select * from test";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error makeing query:%s\n",mysql_error(&mysql));
else
printf("Query made.....\n");
res=mysql_store_result(&mysql);
printf("the number rows of result set is %u\n",(unsigned long)mysql_num_rows(res));
for(r=0;r<(unsigned long)mysql_num_rows(res);r++)
{
row=mysql_fetch_row(res);
if(row<0) break;
for(t=0;t printf("%s ",row[t]);
printf("\n");
}
mysql_free_result(res);
mysql_close(&mysql);
}
steven
steven chang_1
Regular Advisor

Re: mysql sample code

transactional table syntax:

#include
#include

int main()
{
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int t;
unsigned long r;


mysql_init(&mysql);
if(!mysql_real_connect(&mysql,"localhost","mysql1","mysql1","mysql1",0,NULL,0))
printf("Error connecting to database:%s\n",mysql_error(&mysql));
else
printf("Mysql Connected ...\n");
/* query="insert into test values('insert')";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making insert table test:%s\n",mysql_error(&mysql));
else
printf("Insert made.to test table....\n");
query="update test set name='update' where name='insert'";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making update table test:%s\n",mysql_error(&mysql));
else
printf("Update made.to test table....\n"); */
// query="start transaction";
query="set autocommit=0";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making start transactions:%s\n",mysql_error(&mysql));
else
printf("Transaction start.....\n");
query="delete from test where name='å¼µå ç '";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making delete operation:%s\n",mysql_error(&mysql));
else
printf("Delete made.....\n");
query="delete from test1 where name='å¼µå ç '";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making delete operation:%s\n",mysql_error(&mysql));
else
printf("Delete made.....\n");
query="rollback";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making rollback operation:%s\n",mysql_error(&mysql));
else
printf("Rollback delete made.....\n");
query="select * from test1";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
if(t)
printf("Error making query:%s\n",mysql_error(&mysql));
else
printf("Query made.....\n");
res=mysql_store_result(&mysql);
printf("the number rows of result set is %u\n",(unsigned long)mysql_num_rows(res));
for(r=0;r<(unsigned long)mysql_num_rows(res);r++)
{
row=mysql_fetch_row(res);
if(row<0) break;
for(t=0;t printf("%s ",row[t]);
printf("\n");
}
mysql_free_result(res);
mysql_c
steven
steven chang_1
Regular Advisor

Re: mysql sample code

coding is not a bad job
steven