Aşağıdaki kodun çalışabilmesi için mysql librarylerinin kurulu ve local de bir mysql sunucusunun çalışır durumda olduğu kabul edilmektedir.
Not : Debian Linux’ta gcc ile compile edip denenmiştir.
Kod: Kodu kopyalamak için üzerine çift tıklayın!
/*
* File: main.c
* Author: tanero
*
* Created on 28 Şubat 2010 Pazar, 16:59
* MYSQL denemeleri
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <mysql/mysql.h>
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "parola";
char *database = "taner_db";
void kisi_ekle() {
int i;
for (i = 0; i < 20; i++) {
if (!mysql_query(&mysql, "insert into kisiler (adi,soyadi) values (‘Kemal’,'Sunal’)")) {
fprintf(stderr, "%s\n", mysql_error(&mysql));
return (1);
}
}
}
void dbyarat() {
/* send SQL query */
if (mysql_query(&mysql, "create database taner_db")) {
fprintf(stderr, "%s\n", mysql_error(&mysql));
//return (1);
}
}
void tabloyarat() {
if (mysql_query(&mysql, "create table kisiler (adi varchar(20))")) {
fprintf(stderr, "%s\n", mysql_error(&mysql));
//return (1);
}
}
///////////////////////////////// Listeleme Fonksiyonları //////////////////////
/**
* Kişileri Listeler
*/
void kisi_list() {
if (mysql_query(&mysql, "select * from kisiler")) {
fprintf(stderr, "%s\n", mysql_error(&mysql));
//return (1);
}
//res = mysql_use_result(&mysql);
res = mysql_store_result(&mysql);
printf("Kayit: %lu \n", (unsigned long) mysql_num_rows(res));
/* output table name */
printf("Kisiler:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("ID:%s \n", row[0]);
printf("ADI:%s \n", row[1]);
printf("SOYADI:%s \n", row[2]);
}
mysql_free_result(res);
}
/**
* Kişileri Listeler
*/
void kisi_list2() {
unsigned int num_fields;
unsigned int num_rows;
char *sql = "select * from kisiler";
if (mysql_real_query(&mysql, sql, (unsigned int) strlen(sql))) {
// query sonucu 1 olursa hata oluşmuştur.
fprintf(stderr, "%s\n", mysql_error(&mysql));
//return (1);
} else {
// query başarılı
// Result set e dolduruluyor.
res = mysql_store_result(&mysql);
if (res) {
// there are rows
num_fields = mysql_num_fields(res);
printf("Alan: %d \n",num_fields);
printf("Kayit: %lu \n", (unsigned long) mysql_num_rows(res));
printf("Kisiler:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("ID:%s \n", row[0]);
printf("ADI:%s \n", row[1]);
printf("SOYADI:%s \n", row[2]);
}
mysql_free_result(res);
} else { // mysql_store_result() returned nothing; should it have?
if (mysql_field_count(&mysql) == 0) {
// query does not return data
// (it was not a SELECT)
num_rows = mysql_affected_rows(&mysql);
} else // mysql_store_result() should have returned data
{
fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
}
}
}
}
/**
* Tabloları Listeler
*/
void table_list() {
if (mysql_query(&mysql, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(&mysql));
//return (1);
}
res = mysql_use_result(&mysql);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL) {
printf("%s \n", row[0]);
}
mysql_free_result(res);
}
/**
* Karakter Listelemesi Yapar.
*/
void karakterset() {
// Karakter set bilgileri
MY_CHARSET_INFO cs;
mysql_get_character_set_info(&mysql, &cs);
printf("Karakter Bilgileri:\n");
printf("character set+collation number: %d\n", cs.number);
printf("character set name: %s\n", cs.name);
printf("collation name: %s\n", cs.csname);
printf("comment: %s\n", cs.comment);
printf("directory: %s\n", cs.dir);
printf("multi byte character min. length: %d\n", cs.mbminlen);
printf("multi byte character max. length: %d\n", cs.mbmaxlen);
}
void karakter_set() {
// Karakter set
/*
if (!mysql_set_character_set(&mysql, "utf8")) {
printf("New client character set: %s\n", mysql_character_set_name(&mysql));
}
*/
}
/**
* Main İşlemleri
*/
int main(int argc, char** argv) {
//ilk işlem bu olmalı
mysql_init(&mysql);
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
if (!mysql_real_connect(&mysql, server, user, password, database, 0, NULL, 0)) {
fprintf(stderr, "Connection Error: %s\n", mysql_error(&mysql));
}
mysql_query(&mysql,"set names utf8");
printf("New client character set: %s\n", mysql_character_set_name(&mysql));
//printf("Stat: %s\n",mysql_stat(&mysql));
//kisi_ekle();
//table_list();
kisi_list2();
//karakterset();
/* close &mysql connection object */
mysql_close(&mysql);
return 0;
}