简单例子测试Ubuntu安装的mysql开发包SDK,附编译选项说明

首先确认环境已经安装好了。

1、创建一个数据库,并创建几个表。

2、修改代码里面数据库名称,编译运行。

终端命令:

zjy@ubuntu:~/code$ mysql -uroot -proot

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 19

Server version: 5.7.19-0ubuntu0.16.04.1 (Ubuntu)


Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> prompt \u@\h \d>

PROMPT set to '\u@\h \d>'

root@localhost (none)>SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)


root@localhost (none)>CREATE DATABASE test;

Query OK, 1 row affected (0.01 sec)


root@localhost (none)>SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

| test               |

+--------------------+

5 rows in set (0.00 sec)


root@localhost (none)>CREATE TABLE table1(name VARCHAR(20), sex CHAR(1));

ERROR 1046 (3D000): No database selected

root@localhost (none)>USE test;

Database changed

root@localhost test>CREATE TABLE table1(name VARCHAR(20), sex CHAR(1));

Query OK, 0 rows affected (0.02 sec)


root@localhost test>CREATE TABLE table3(id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20), sex CHAR(1));

Query OK, 0 rows affected (0.02 sec)


root@localhost test>CREATE TABLE table2(name VARCHAR(20), sex CHAR(1));         

Query OK, 0 rows affected (0.03 sec)


root@localhost test>DESCRIBE table3;

+-------+----------------------+------+-----+---------+----------------+

| Field | Type                 | Null | Key | Default | Extra          |

+-------+----------------------+------+-----+---------+----------------+

| id    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |

| name  | varchar(20)          | YES  |     | NULL    |                |

| sex   | char(1)              | YES  |     | NULL    |                |

+-------+----------------------+------+-----+---------+----------------+

3 rows in set (0.01 sec)


root@localhost test>quit;

Bye

zjy@ubuntu:~/code$ vim mysql.c 

zjy@ubuntu:~/code$ gcc mysql.c -o mysql $(mysql_config --cflags --libs)

zjy@ubuntu:~/code$ ./mysql

MySQL Tables in mysql database:

table1 

table2 

table3 

zjy@ubuntu:~/code


编译选项:

最好用 #gcc test.c -o test $(mysql_config --cflags --libs)

mysql_config:获取编译客户的编译选项
mysql_config 提供了关于编译MySQL客户端以及将其连接到MySQL的有用信息.支持下列选项:
--cflags:编译器标志,用于查找包含文件,以及编译libmysqlclient库时所要使用的关键编译器标志和定义.
--include:编译器选项,用于查找MySQL包含文件(注意,正常情况下应使用"--cflags",而不是该选项).
--libmysql-libs,--enbedded:与MySQL嵌入式服务器进行连接所需的库和选项.
--libs:与MySQL客户端库进行连接所需要的库和选项.
--libs_r:与线程安全MySQL客户端进行链接所需的库和选项.
--port:默认的tcp/ip端口号,配置MySQL时定义.
--socket:默认的Unix套接字文件,配置MySQL时定义.
--version:版本号以及MySQL分发版的版本.
其中,mysql_config在/usr/bin/mysql_config(这是rpm包安装后的默认目录), 若是源码安装,则一般在..../mysql/bin/mysql_config.




代码如下:

#include <stdio.h>

#include "mysql.h"


int main()

{

    MYSQL *mysql;

    MYSQL_RES *res;

    MYSQL_ROW row;

    

    char *server ="localhost";

    char *user ="root";

    char *password ="";

    char *database ="mysql";

    

    mysql = mysql_init(NULL);

    if (mysql ==NULL)

    {

        fprintf(stderr,"%s\n", mysql_error(mysql));

        return1;

    }

    

    if (!mysql_real_connect(mysql, server,user, password, database,0,NULL,0))

    {

        fprintf(stderr,"%s\n", mysql_error(mysql));

        return1;

    }

    

    if (mysql_query(mysql,"show tables"))

    {

        fprintf(stderr,"%s\n", mysql_error(mysql));

        return1;    }

    

    res = mysql_use_result(mysql);

    

    printf("MySQL Tables in mysql database:\n");

    while ((row = mysql_fetch_row(res)) !=NULL)

    {

        printf("%s \n", row[0]);

    }

    

    mysql_free_result(res);

    mysql_close(mysql);

    

    return0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值