- A.M.P 설치 https://webnautes.tistory.com/1185

- php 샘플 https://webnautes.tistory.com/828

 

Android PHP MySQL 예제 - 데이터베이스에 데이터 입력하기

안드로이드 앱이 PHP 프로그램을 매개로 하여 MySQL 데이터베이스 서버에 데이터를 저장하는 간단한 예제입니다. 1. Apache2, MySQL, PHP7 설치 2. 데이터베이스 및 테이블 생성 3. 웹브라우저로 PHP 동작 테스트..

webnautes.tistory.com

 

day6/실습2: MySQL C-interface

MySQL client

https://dev.mysql.com/doc/refman/5.7/en/c-api-building-clients.html

 

mysql_config displays the options needed for compiling or linking:

shell> mysql_config --cflags

shell> mysql_config --libs

 

시작:

https://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-apiprogram.html

 

샘플:

http://zetcode.com/db/mysqlc/

 

MySQL C API programming tutorial

Home Subscribe MySQL C API programming tutorial This is a C programming tutorial for the MySQL database. It covers the basics of MySQL programming with the C API. You may also consider to look at the MySQL tutorial on ZetCode. About MySQL database MySQL is

zetcode.com

 

 

dbcon.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
 
    $host = 'localhost';
    $username = 'kim'# MySQL 계정 아이디
    $password = '1q2w3e4rA!!'# MySQL 계정 패스워드
    $dbname = 'db';  # DATABASE 이름
 
 
    $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
    
    try {
 
        $con = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8",$username$password);
    } catch(PDOException $e) {
 
        die("Failed to connect to the database: " . $e->getMessage()); 
    }
 
 
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
 
    if(function_exists('get_magic_quotes_gpc'&& get_magic_quotes_gpc()) { 
        function undo_magic_quotes_gpc(&$array) { 
            foreach($array as &$value) { 
                if(is_array($value)) { 
                    undo_magic_quotes_gpc($value); 
                } 
                else { 
                    $value = stripslashes($value); 
                } 
            } 
        } 
 
        undo_magic_quotes_gpc($_POST); 
        undo_magic_quotes_gpc($_GET); 
        undo_magic_quotes_gpc($_COOKIE); 
    } 
 
    header('Content-Type: text/html; charset=utf-8'); 
    #session_start();
?>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

insert.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php 
 
    error_reporting(E_ALL); 
    ini_set('display_errors',1); 
 
    include('dbcon.php');
 
 
    if( ($_SERVER['REQUEST_METHOD'== 'POST'&& isset($_POST['submit']))
    {
 
        $name=$_POST['name'];
        $address=$_POST['address'];
 
        if(empty($name)){
            $errMSG = "이름을 입력하세요.";
        }
        else if(empty($address)){
            $errMSG = "주소를 입력하세요.";
        }
 
        if(!isset($errMSG))
        {
            try{
                $stmt = $con->prepare('INSERT INTO Person(name, address) VALUES(:name, :address)');
                $stmt->bindParam(':name'$name);
                $stmt->bindParam(':address'$address);
 
                if($stmt->execute())
                {
                    $successMSG = "새로운 사용자를 추가했습니다.";
                }
                else
                {
                    $errMSG = "사용자 추가 에러";
                }
 
            } catch(PDOException $e) {
                die("Database error: " . $e->getMessage()); 
            }
        }
 
    }
?>
 
<html>
   <body>
        <?php 
        if (isset($errMSG)) echo $errMSG;
        if (isset($successMSG)) echo $successMSG;
        ?>
        
        <form action="<?php $_PHP_SELF ?>" method="POST">
            Name: <input type = "text" name = "name" />
            Address: <input type = "text" name = "address" />
            <input type = "submit" name = "submit" />
        </form>
   
   </body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

mysql-c-api.c

$ gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
 
int main() {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
 
    char *server = "localhost";
    char *user = "kim";
    char *password = "1q2w3e4rA!!"/* set me first */
    char *database = "db";
 
    conn = mysql_init(NULL);
 
    /* Connect to database */
    if (!mysql_real_connect(conn, server,
        user, password, database, 0NULL0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
 
    /* send SQL query */
    if (mysql_query(conn, "show tables")) {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
 
    res = mysql_use_result(conn);
 
    /* output table name */
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL)
        printf("%s \n", row[0]);
 
    /* close connection */
    mysql_free_result(res);
    mysql_close(conn);
    return 0;
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

 

 

create_test.c

gcc -o create_test $(mysql_config --cflags) create_test.c $(mysql_config --libs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <my_global.h>
#include <mysql.h>
 
void finish_with_error(MYSQL *con)
{
  fprintf(stderr, "%s\n", mysql_error(con));
  mysql_close(con);
  exit(1);        
}
 
int main(int argc, char **argv)
{
  MYSQL *con = mysql_init(NULL);
  
  if (con == NULL
  {
      fprintf(stderr, "%s\n", mysql_error(con));
      exit(1);
  }  
 
  if (mysql_real_connect(con, "localhost""kim""1q2w3e4rA!!"
          "testdb"0NULL0== NULL
  {
      finish_with_error(con);
  }    
  
  if (mysql_query(con, "DROP TABLE IF EXISTS Cars")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")) {      
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(1,'Audi',52642)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(2,'Mercedes',57127)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(3,'Skoda',9000)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(4,'Volvo',29000)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(5,'Bentley',350000)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(6,'Citroen',21000)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(7,'Hummer',41400)")) {
      finish_with_error(con);
  }
  
  if (mysql_query(con, "INSERT INTO Cars VALUES(8,'Volkswagen',21600)")) {
      finish_with_error(con);
  }
 
  mysql_close(con);
  exit(0);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

select_test.c

gcc -o select_test $(mysql_config --cflags) select_test.c $(mysql_config --libs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <my_global.h>
#include <mysql.h>
 
void finish_with_error(MYSQL *con)
{
  fprintf(stderr, "%s\n", mysql_error(con));
  mysql_close(con);
  exit(1);        
}
 
int main(int argc, char **argv)
{      
  MYSQL *con = mysql_init(NULL);
  
  if (con == NULL)
  {
      fprintf(stderr, "mysql_init() failed\n");
      exit(1);
  }  
  
  if (mysql_real_connect(con, "localhost""kim""1q2w3e4rA!!"
          "testdb"0NULL0== NULL
  {
      finish_with_error(con);
  }    
  
  if (mysql_query(con, "SELECT * FROM Cars")) 
  {
      finish_with_error(con);
  }
  
  MYSQL_RES *result = mysql_store_result(con);
  
  if (result == NULL
  {
      finish_with_error(con);
  }
 
  int num_fields = mysql_num_fields(result);
 
  MYSQL_ROW row;
  
  while ((row = mysql_fetch_row(result))) 
  { 
      for(int i = 0; i < num_fields; i++
      { 
          printf("%s ", row[i] ? row[i] : "NULL"); 
      } 
          printf("\n"); 
  }
  
  mysql_free_result(result);
  mysql_close(con);
  
  exit(0);
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

+ Recent posts