Mysql & Languages: C C++ Java PHP Perl Python

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

MySQL & Languages

C C++ Java PHP Perl Python

C API
mysql.h

( Structure & Function Declarations)


libmysqlclient

( UNIX / LINUX Systems) mysqlclient.lib ( Windows Systems) It contains actual C Functions

Header Files Required


#include<stdio.h> #include<mysql.h>

STRUCTURES REQUIRED
MYSQL mysql
primary point for all interaction with the

server

MYSQL_RES *result MYSQL_ROW row

Results returned by server are stored Records extracted from result are stored

MYSQL_FIELD * field

Field(column) info. of a record is stored i.e., name,length,data type etc..

Initialize & Establish The Connection


mysql_init(&mysql)

initialize MYSQL structure

mysql_real_connect(&mysql,localhost,un,pwd,db,0,NULL,0);

Connect to Database &mysql : connected info stored here localhost : Name/IP of the server to connect

un

: User Name db : Database to be Used NULL : Socket Name


If SUCCESS : UNSUCCESS

pwd : Password 0 : Port Number 0 : optional FLAG

Returns Connection Handle : It returns FALSE

QUERY EXECUTION
mysql_query(&mysql, SELECT statement); mysql_query(&mysql, INSERT statement); mysql_query(&mysql, DELETE statement); mysql_query(&mysql, UPDATE statement); SUCCESS : Returns Zero UNSUCCESS : Returns Non-Zero

Resultset Processing
result = mysql_store_result(&mysql);
It

is called on MYSQL connection handler Retrieves Complete Resultset returned by last Query Result is stored in a MYSQL_RES structure
SUCCESS : returns resultset UNSUCCESS : returns NULL

Process Resultset
while ((row = mysql_fetch_row(result))) { printf(%s \t %s, row[0],row[1]); }
mysql_fetch_row()

reads records one at a time into a variable MYSQL_ROW structure


MYSQL_ROW

is an array with each element representing one field of the record


Returns

NULL after all records are returned

Close Connection
mysql_free_result(result); mysql_close(&mysql);
Deallocates memory used by resultset
Closes the database connection

#include<stdio.h> #include<mysql.h > int main() {

EXAMPLE

If ( !(result=mysql_store_result(&mysql))) { printf(Error in Query : %s\n,mysql_error(&mysql)); exit(); } else { while ((row = mysql_fetch_row(result))) { printf(%s \t %s\n, row[0],row[1]); } } mysql_free_result(result); mysql_close(&mysql); }

Compile & Execute


Compile C program /usr/bin/gcc prg1.c o prg1.bin -I /usr/include/mysql -L /usr/lib/mysql lmysqlclient lz Execute ./prg1.bin

Other Functions
mysql_select_db(&mysql,newDbName);

Select a new database after you connect to MYSQL number of rows in resultset number of columns in resultset

int r = mysql_num_rows(result);
Returns

int c = mysql_num_fields(result);
Returns

Example # 1
int nrows = mysql_num_rows(&mysql);

for(i=0; i<nrows;i++) { row =mysql_fetch_row(result); printf(%s - %s \n,row[0],row[1]); }

Example # 2
int nrows = mysql_num_rows(result); int ncols = mysql_num_fields(result); for( i=0; i<nrows; i++ ) { row=mysql_fetch_row(result); for(j=0; j<ncols; j++) { printf( %s \t,row[ j ]); } printf(\n); }

FIELD INFORMATION
MYSQL_FIELD *field mysql_query(&mysql,Select * from emp); result=mysql_store_result(&mysql); While((field = mysql_fetch_field(result))) { printf(Name : %s, field->name); printf(Length : %d, field->length); printf(Data Type : %s, field->type); printf(Max.Width: %s, field->max_length); printf(Null Allowed : %s, IS_NOT_NULL(field->flags)?NO : YES); printf(Primary Key : %s, IS_PRI_KEY(field->flags)?NO : YES); }

LIST DATABASES & TABLES


MYSQL mysql; MYSQL_RES *dbs; MYSQL_ROW db; MYSQL_RES *tbls; MYSQL_ROW tbl; dbs = mysql_list_dbs(&mysql,NULL); mysql_select_db(&mysql,dbname);
Returns all databases in the resultset Select a particular database

tbls = mysql_list_tables(&mysql,NULL);
Returns all Table Names in the resultset

dbs = mysql_list_dbs(&mysql,NULL); while (db = mysql_fetch_row(dbs)) { printf(\nDataBase Name : %s \n, db[0]); mysql_select_db(&mysql,db[0]); tbls = mysql_list_tables(&mysql,NULL); while(tbl = mysql_fetch_row(tbls)) { printf(%s,tbl[0]); } mysql_free_result(tbls); } mysql_free_result(dbs);

THE END

You might also like