MySQL API( c ) & SQL2

Download Report

Transcript MySQL API( c ) & SQL2

MySQL API( c ) & SQL2
[email protected]
강동훈
Contents



Overview
How to use MySQL(SQL2)
How to use MySQL API (in C)
Overview

SQL






Structured Query Language
based on the relational database.
Data definition
Data manipulation
Query
MySQL
How to use MySQL #1

Connect to MySQL Server
shell> mysql -h host -u user -p <사용할 DB> <Enter>
password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 459 to server version: 3.22.20a-log
Type 'help' for help.
mysql>
How to use MySQL #2

Data Definition 1
mysql> SHOW DATABASES;
Database
mysql
test
tmp
How to use MySQL #3

Data Definition 2
mysql> CREATE DATABASE docmanager;
mysql> USE docmanager
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec)
mysql>
형식 : CREATE DATABASE <DATABASE NAME>;
USE <사용할 DB NAME>
How to use MySQL #4

Data definition 3
mysql> CREATE TABLE docmanager (title VARCHAR(40), description
-> VARCHAR(40),file_name VARCHAR(20),
-> enrolled DATE, updated DATE, owner VARCHAR(20));
(CF) desc
형식 : CREATE TABLE 테이블명(컬럼의 형식);
How to use MySQL #5

Data Manipulation 1
mysql> INSERT INTO docmanager
-> VALUES(‘MySQL Seminar', ‘MySQL?', ‘MySQL.ppt',
‘2000-07‘> 04',’2000-07-05’, ‘please’);
mysql> UPDATE docmanager SET updated=“2000-07-06"
WHERE title=“MySQL Seminar";
형식: INSERT INTO 테이블명 VALUES(컬럼의 데이터 값..);
UPDATE 테이블명 SET 컬럼명 = 식,… [WHERE 조건];
How to use MySQL #5

Data manipulation 2
mysql> SELECT * FROM docmanager;
title
decription file_name
enrolled
updated
owner
MySQL
Seminar
MySQL?
2000-07-04
2000-07-06
please
MySQL.ppt
mysql> SELECT * FROM docmanager WHERE owner = “please";
mysql> SELECT owner FROM docmanager;
형식: SELECT 컬럼명 … from 테이블명 [WHERE 조건];
How to use MySQL API(in C) #1

Process
1.connect to SQL server
2.Quest Queries
3.manipulate result sets
How to use MySQL API(in C) #2
Mysql_init()
Mysql_store_result()
Mysql_use_result()
Mysql_real_connect()
Mysql_fetch_row()
Mysql_query()
Mysql_real_query()
Mysql_free_result()
If result set exists(only to “SELECT” Query)
Mysql_close()
#include <stdio.h>
#include "mysql.h“
Int main()
{
MYSQL mysql;
MYSQL_RES *myresult;
MYSQL_ROW row;
unsigned int num_fields;
unsigned int num_rows;
char * string_query;
mysql_init(&mysql);
mysql_real_connect(&mysql,"localhost","please",“********","please
,0,NULL,0);
string_query = "select * from docmanager\n";
mysql_query(&mysql,string_query);
myresult = mysql_store_result(&mysql);
while(row = mysql_fetch_row(myresult))
printf("%s\t %s\n",row[0],row[1]);
mysql_free_result(myresult);
mysql_close(&mysql);
return 0;
}
Gcc –g –o mysqlapiexample mysqlapiexample.c –L/usr/lib/mysql –lmysqlclient
How to use MySQL API(in C) #4
 C API Function Description
function
prototype
mysql_init()
MYSQL *mysql_init(MYSQL *mysql)
mysql_real_connect()
MYSQL *mysql_real_connect(MYSQL *mysql,const
char *host,const char *user,const char
*passwd,const char *db, uint port,const char
*unix_socket,uint client_flag)
mysql_query()
Int mysql_query(MYSQL *mysql,const char * query)
mysql_real_query()
Int mysql_real_query(MYSQL *mysql,const char *
query, unsigned int length)
mysql_store_result()
MYSQL_RES *mysql_store_result(MYSQL *mysql)
mysql_use_result()
MYSQL_RES *musql_use_result(MYSQL *mysql)
mysql_fetch_row()
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
mysql_free_result()
Void mysql_free_result(MYSQL_RES *result)
mysql_close()
Void mysql_close(MYSQL *mysql)