使用c语言访问mysql数据库

首先在linux上安装mysql

 1 [email protected]:~$ mysql -u root
 2 Welcome to the MySQL monitor.  Commands end with ; or \g.
 3 Your MySQL connection id is 9
 4 Server version: 5.6.24-0ubuntu2 (Ubuntu)
 5
 6 Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
 7
 8 Oracle is a registered trademark of Oracle Corporation and/or its
 9 affiliates. Other names may be trademarks of their respective
10 owners.
11
12 Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
13
14 mysql> create database foo;
15 Query OK, 1 row affected (0.00 sec)
16
17 mysql> use foo ;
18 Database changed
19 mysql> exit
20 Bye

上面的代码是进入mysql创建数据库foo供测试

 1 -这是create_children.sql的源码
 2 create table children(
 3     childno int(11) not null auto_increment,
 4     fname varchar(30),
 5     age int(11),
 6     primary key (childno));
 7
 8 INSERT INTO children VALUES (1,‘Jenny‘,17);
 9 INSERT INTO children VALUES (2,‘Andrew‘,13);
10 INSERT INTO children VALUES (3,‘Gavin‘,4);
11 INSERT INTO children VALUES (4,‘Duncan‘,2);
12 INSERT INTO children VALUES (5,‘Emma‘,0);
13 INSERT INTO children VALUES (6,‘Alex‘,11);
14 INSERT INTO children VALUES (7,‘Adrian‘,5);

在mysql中调用执行这段源码

 1 mysql> source /home/jason/c_program/544977-blp3e/chapter08/create_children.sql
 2 Query OK, 0 rows affected (0.48 sec)
 3
 4 Query OK, 1 row affected (0.08 sec)
 5
 6 Query OK, 1 row affected (0.03 sec)
 7
 8 Query OK, 1 row affected (0.12 sec)
 9
10 Query OK, 1 row affected (0.11 sec)
11
12 Query OK, 1 row affected (0.05 sec)
13
14 Query OK, 1 row affected (0.06 sec)
15
16 Query OK, 1 row affected (0.05 sec)
17
18 mysql> show tables;
19 +---------------+
20 | Tables_in_foo |
21 +---------------+
22 | children      |
23 +---------------+
24 1 row in set (0.00 sec)

执行之后查看执行的效果:

 1 mysql> select * from  children;
 2 +---------+--------+------+
 3 | childno | fname  | age  |
 4 +---------+--------+------+
 5 |       1 | Jenny  |   17 |
 6 |       2 | Andrew |   13 |
 7 |       3 | Gavin  |    4 |
 8 |       4 | Duncan |    2 |
 9 |       5 | Emma   |    0 |
10 |       6 | Alex   |   11 |
11 |       7 | Adrian |    5 |
12 +---------+--------+------+
13 7 rows in set (0.00 sec)

这样就完成数据库的准备工作

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3
 4 #include "mysql.h"
 5
 6 int main(int argc, char *argv[])
 7 {
 8    MYSQL *conn_ptr;
 9
10    conn_ptr = mysql_init(NULL);
11    if (!conn_ptr) {
12       fprintf(stderr, "mysql_init failed\n");
13       return EXIT_FAILURE;
14    }
15
16    conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "","foo", 0, NULL, 0);
17
18    if (conn_ptr) {
19       printf("Connection success\n");
20    } else {
21       printf("Connection failed\n");
22    }
23
24    mysql_close(conn_ptr);
25
26    return EXIT_SUCCESS;
27 }

上面是初始化链接和建立链接的函数的使用

 1 [email protected]:~/c_program/544977-blp3e/chapter08$ gcc -I/usr/include/mysql connect1.c -L/usr/lib/mysql -lmysqlclient -o connect1
 2 connect1.c:4:19: fatal error: mysql.h: 没有那个文件或目录
 3  #include "mysql.h"
 4                    ^
 5 compilation terminated.
 6 //缺少头文件
 7 [email protected]:~/c_program/544977-blp3e/chapter08$ sudo apt-get install mysql-devel
 8 [sudo] password for jason:
 9 正在读取软件包列表... 完成
10 正在分析软件包的依赖关系树
11 正在读取状态信息... 完成
12 E: 未发现软件包 mysql-devel
13 //不匹配uubuntu情况
14 [email protected]:/$ sudo apt-get install libmysqlclient-dev
15 正在读取软件包列表... 完成
16 正在分析软件包的依赖关系树
17 正在读取状态信息... 完成
18 下列【新】软件包将被安装:
19   libmysqlclient-dev
20 //成功

下面是编译这个代码:

1 [email protected]:~/c_program/544977-blp3e/chapter08$ gcc -I/usr/include/mysql connect1.c -L/usr/lib/mysql -lmysqlclient -o connect1
2 [email protected]:~/c_program/544977-blp3e/chapter08$ ./connect1
3 Connection failed

这个失败是因为原来的.c文件中的权限设置给的不合适

 1 mysql> select user, host, password from mysql.user;
 2 +------------------+-----------+-------------------------------------------+
 3 | user             | host      | password                                  |
 4 +------------------+-----------+-------------------------------------------+
 5 | root             | localhost |                                           |
 6 | root             | t61       |                                           |
 7 | root             | 127.0.0.1 |                                           |
 8 | root             | ::1       |                                           |
 9 | debian-sys-maint | localhost | *16AEF7D29C488DB8597DCBF25DCE8097E57558B7 |
10 +------------------+-----------+-------------------------------------------+
11 5 rows in set (0.00 sec)

修改相应的账号或者密码:

1 [email protected]:~/c_program/544977-blp3e/chapter08$ gcc -I/usr/include/mysql connect1.c -L/usr/lib/mysql -lmysqlclient -o connect1
2 [email protected]:~/c_program/544977-blp3e/chapter08$ ./connect1
3 Connection success
4 [email protected]:~/c_program/544977-blp3e/chapter08$
 1 #include <stdlib.h>
 2 #include <stdio.h>
 3
 4 #include "mysql.h"
 5
 6 int main(int argc, char *argv[]) {
 7    MYSQL my_connection;
 8
 9    mysql_init(&my_connection);
10    if (mysql_real_connect(&my_connection, "localhost", "root", "", "foo", 0, NULL, 0)) {
11       printf("Connection success\n");
12       mysql_close(&my_connection);
13    } else {
14       fprintf(stderr, "Connection failed\n");
15       if (mysql_errno(&my_connection)) {
16           fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
17       }
18    }
19
20    return EXIT_SUCCESS;
21 }

这段代码有错误处理的能力可以把相应的错误信息打印出来:

1 //首先给错误的账号密码
2 [email protected]:~/c_program/544977-blp3e/chapter08$ gcc -I/usr/include/mysql connect2.c -L/usr/lib/mysql -lmysqlclient -o connect2
3 [email protected]:~/c_program/544977-blp3e/chapter08$ ./connect2
4 Connection failed
5 Connection error 1045: Access denied for user ‘rick‘@‘localhost‘ (using password: YES)
6 [email protected]:~/c_program/544977-blp3e/chapter08$ gcc -I/usr/include/mysql connect2.c -L/usr/lib/mysql -lmysqlclient -o connect2
7 [email protected]:~/c_program/544977-blp3e/chapter08$ ./connect2
8 Connection success

//2015年06月28日 星期日 14时54分19秒

时间: 2024-08-11 07:39:03

使用c语言访问mysql数据库的相关文章

【Linux】Ubuntu下C语言访问MySQL数据库入门

使用的系统是Ubuntu 11.10.数据库是MySQL. MySQL数据库环境配置 首先需要安装MySQL客户端和服务器,命令行安装方式为: [cpp] view plaincopyprint? sudo apt-get install mysql-server mysql-client 然后,要使用C语言编程访问数据库,需要另外安装一个开发包: [cpp] view plaincopyprint? sudo apt-get install libmysqlclient15-dev 在MySQ

C语言访问MySQL数据库的方法

1.添加头文件路径(MySQL安装路径中的include路径) 2.添加库文件(直接从MySQL安装路径中copy libmysql.lib即可) 3.编程操作数据库 代码 // AccessToMySQL.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include <mysql.h> #pragma comment(lib,"libmysql.lib")

C 语言访问MySQL数据库的简单实验

首先,建立一个MySQL用户rick,密码设置为6452079,登录地点设置为本地登录localhost. 为rick用户创建一个数据库foo. 在数据库foo里创建一个表children. 表的结构如下: 添加3条简单的记录后,表为: 实验C 代码: #include <stdio.h> #include <stdlib.h> #include "mysql.h" MYSQL my_connection; MYSQL_RES *res_ptr; MYSQL_R

c语言操作mysql数据库

c语言操作Mysql数据库,主要就是为了实现对数据库的增.删.改.查等操作,操作之前,得先连接数据库啊,而连接数据库主要有两种方法.一.使用mysql本身提供的API,在mysql的安装目录中可可以看到大量的头文件.lib文件.dll文件,这说明mysql原生就支持了c语言,操作起来相当简单.二.使用win32 api(这里只探讨windows平台),主要是ODBC. ODBC API 接口是(Open Database Connectivity)开放式数据库接口,它建立了一组规范,并提供了一组

PHP访问MySQL数据库

第9章 PHP访问MySQL数据库 1.  PHP访问MySQL数据库服务器的流程 之前学习mysql的时候,采用的是"客户机/服务器"的体系结构 mysql>select * from a;    -------->MySQL数据库服务器 < --------- PHP访问MySQL数据库的时候,我们的PHP则充当了客户机的角色 PHP充当了客户机的角色----------->MySQL数据库服务器 <----------- 接下来我们可以通过phpin

C语言连接MySQL数据库(课程设计总结)

刚结束课程设计,也预示着假期马上就要到来了.本次课程设计并不算难,无非就是让做一个XXX系统,实现用户的注册.登录.菜单管理.超级用户等等一些功能,到现在为止已经做过好几个了,所以基本流程都熟悉了!我觉的最值得总结的地方就是:C语言与数据库连接,这块内容!因为之前都是用文件实现的. ★平台 这次课程设计主要用到: ● VC ++ 6.0 ● mysql-5.0.18-win32 ● Navicat for MySQL(MySQL 图形化的工具) ★配置 在写代码之前首先要让编译器知道MySQL数

关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作

PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","","0710_test"); //写SQL语句$sql = "select * from student";//检测连接数据库是否成功,失败返回"连接失败",并退出程序 if(mysqli_connect_error()){    die("连

C语言连接mysql数据库

操作系统:win7/64 编译软件:VS2010 数据库:5.7.11 从C语言连接mysql数据库包含两个步骤: 1 初始化连接句柄结构 2 实际创建连接 测试代码1: #include "stdafx.h" #include <WinSock2.h> /*socket通信,系统头文件*/ #include <windows.h> #include <stdio.h> #include "mysql.h" #pragma com

[JavaWeb基础] 003.JAVA访问Mysql数据库

上面两篇讲解了简单的JSP + Servlet的搭建和请求,那么后面我们肯定要用到数据交互,也就是操纵数据库的数据,包括对数字的增加,删除,修改,查询.我们就用简单的MySql来做例子 我们需要引入驱动包mysql-connector-java.jar,自行去网上下载,有很多. 下面我跟着代码看看怎么进行增删改查 1.打开数据库 // 驱动程序名 private String driver = "com.mysql.jdbc.Driver"; // URL指向要访问的数据库名scutc