Linux下用C语言操作MySQL

Posted zieckey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下用C语言操作MySQL相关的知识,希望对你有一定的参考价值。

Linux下用C语言操作mysql

作者:zieckey@yahoo.com.cn

注:下面的所有例子在Red Hat 9 linux下测试通过

1,使用c语言操作mysql之前,先在mysql里头创建一个数据库,一个表,在表里头添加数据如下:

启动MySQL数据库服务器:
[root@zieckey root]# safe_mysqld -u mysql&
[1] 5288
[root@zieckey root]# Starting mysqld daemon with databases from /var/lib/mysql

创建数据库,库名为cusemysql:
mysql>create database cusemysql;
创建表,表名为:
mysql>use cusemysql;
mysql>create table children(childno int not null unique,fname varchar(20),age int);
添加一点数据哦:
mysql> insert into children values(5,"花儿",10);
对拉,为了方便起见,把表的大致样子给大家看看

mysql>  select * from children;
+---------+-------+------+
| childno | fname | age  |
+---------+-------+------+
|       5 | 花儿  |   10 |
+---------+-------+------+
1 row in set (0.02 sec)
 
mysql>

2 ,下面进行具体的操作

插入:insert   

好的,我们现编辑一段c代码,取名为insert.c

///
/*  insert.c */
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h"
/*注意哦,上面也可以是mysql.h的绝对地址,一般在mysql下的include目录下,仔细看看你的在哪里?*/

int main(int argc, char *argv[])
{
    MYSQL my_connection;

    int res;

    mysql_init(&my_connection);

    /*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
    if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS))
    {
            printf("Connection success/n");
        res = mysql_query(&my_connection, "insert into children values(11,'Anny',5)");

        if (!res)
        {   
            printf("Inserted %lu rows/n",(unsigned long)mysql_affected_rows(&my_connection));
        /*里头的函数返回受表中影响的行数*/
        }
        else
        {
        //分别打印出错误代码及详细信息
        fprintf(stderr, "Insert error %d: %s/n",mysql_errno(&my_connection),mysql_error(&my_connection));
        }
        mysql_close(&my_connection);
    }

    else
    {
        fprintf(stderr, "Connection failed/n");

        if (mysql_errno(&my_connection))
        {
                fprintf(stderr, "Connection error %d: %s/n",mysql_errno(&my_connection),mysql_error(&my_connection));
        }
    }
    return EXIT_SUCCESS;
}
/
代码写完了,要编译哦
[root@zieckey mysql]# gcc -o insert insert.c
/tmp/ccyHfsX2.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/ccyHfsX2.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/ccyHfsX2.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/ccyHfsX2.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/ccyHfsX2.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/ccyHfsX2.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[root@zieckey mysql]#
头文件和库文件位置没有指定
[root@zieckey mysql]# gcc -o insert insert.c -I/usr/include/mysql/ -L/usr/lib/mysql/
/tmp/cc4gdmlp.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/cc4gdmlp.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/cc4gdmlp.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/cc4gdmlp.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/cc4gdmlp.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/cc4gdmlp.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[root@zieckey mysql]#

gcc还是找不到头文件,下面我们可以这样:

[root@zieckey mysql]# gcc -o insert insert.c -I/usr/include/mysql/ -L/usr/lib/mysql/ `mysql_config --cflags --libs`

ok,现在我们执行看看
[root@zieckey mysql]# ./insert
Connection Success
Inserted 1 rows

year,果然可以,呵呵
不信到mysql下看看表children中是否多了刚才插入的那一行数据

mysql>  select * from children;
+---------+---------+------+
| childno | fname   | age  |
+---------+---------+------+
|       5 | 花儿    |   10 |
|      10 | Ann     |    5 |
|       7 | zieckey |   22 |
+---------+---------+------+
3 rows in set (0.00 sec)
 


技巧:这里使用mysql_config是一个好习惯,它可以帮助gcc得到许多有关MySQL的信息。

 

以上是关于Linux下用C语言操作MySQL的主要内容,如果未能解决你的问题,请参考以下文章

如何在Linux下用C/C++语言操作数据库sqlite3

linux下用简单c语言代码怎么实现实现文件夹所有内容的复制

linux下用c语言写出复制黏贴文件

如何在Linux下用C语言操作数据库sqlite3

Linux环境下C编程指南的内容简介

如何在Linux下用C/C++语言操作数据库sqlite3