当前位置:Gxlcms > 数据库问题 > 笔记整理--Linux平台MYSQL的C语言

笔记整理--Linux平台MYSQL的C语言

时间:2021-07-01 10:21:17 帮助过:22人阅读


 

无非也是几个数据结构以及几个函数

先看一下数据结构,如下

MYSQL
这个结构表示对一个数据库连接的句柄,它被用于几乎所有的MySQL函数。 
MYSQL_RES
这个结构代表返回行的一个查询的(SELECT, SHOW, DESCRIBE, EXPLAIN)的结果。从查询返回的信息在本章下文称为结果集合
MYSQL_ROW
这是一个行数据的类型安全(type-safe)的表示。当前它实现为一个计数字节的字符串数组。(如果字段值可能包含二进制数据,你不能将这些视为空终止串,因为这样的值可以在内部包含空字节) 行通过调用mysql_fetch_row()获得。 
MYSQL_FIELD
这个结构包含字段信息,例如字段名、类型和大小。其成员在下面更详细地描述。你可以通过重复调用mysql_fetch_field()对每一列获得MYSQL_FIELD结构。字段值不是这个结构的部分;他们被包含在一个MYSQL_ROW结构中。

 

然后介绍几个最常用的api函数

 

mysql_affected_rows()返回被最新的UPDATE, DELETEINSERT查询影响的行数。
mysql_close()关闭一个服务器连接。
mysql_errno()返回最近被调用的MySQL函数的出错编号。
mysql_error()返回最近被调用的MySQL函数的出错消息。
mysql_fetch_row()从结果集合中取得下一行。
mysql_field_count()返回最近查询的结果列的数量。
mysql_init()获得或初始化一个MYSQL结构。
mysql_insert_id()返回有前一个查询为一个AUTO_INCREMENT列生成的ID。
mysql_num_rows()返回一个结果集合中的行的数量。
mysql_query()执行指定为一个空结尾的字符串的SQL查询。
mysql_real_connect()连接一个MySQL服务器。
mysql_real_query()执行指定为带计数的字符串的SQL查询。

[linux c]mysql 编程笔记 - co1d7urt - 博客园 - Google Chrome (2013/8/5 16:46:03)

[linux c]mysql 编程笔记

要进行linux下的mysql的C编程,需要安装mysql及mysql的开发包,ubuntu下直接apt-get install libmysql++安装开发包。

#include <mysql.h>

相关函数:

技术分享
MYSQL *mysql_init(MYSQL *);
//这里称之为载入函数吧,返回的MYSQL指针要用到后续的函数中

int mysql_options(MYSQL *connection, enum option_to_set,const char *argument);
//设置MYSQL*的一些属性,比如超时时间等

MYSQL *mysql_real_connect(MYSQL *connection,
                const char *server_host,
                const char *sql_user_name,
                const char *sql_password,
                const char *db_name,
                unsigned int port_number,//置0连接默认端口,一般为3306
                const char *unix_socket_name,//NULL
                unsigned int flags);//无另外属性时置0
//连接函数

void mysql_close(MYSQL *connection);
//关闭连接

unsigned int mysql_errno(MYSQL *connection);
//返回错误代码

char *mysql_error(MYSQL *connection);

//返回错误信息

int mysql_query(MYSQL *connection, const char *query);
//执行sql语句

my_ulonglong mysql_affected_rows(MYSQL *connection);
//返回执行语句过后受影响的行数

MYSQL_RES *mysql_store_result(MYSQL *connection);
//返回执行结果,适用于数据量较小时

my_ulonglong mysql_num_rows(MYSQL_RES *result);
//返回上面函数返回结果的行数

MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
//抽取一条记录,返回NULL时表示抽取完记录或者错误

void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);
//调整数据位置,offset为0时,下次调用mysql_fetch_row将返回result第一条记录

MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);
//返回当前的位置

MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);
//移动数据位置,并返回先前的位置,可以和上一个函数结合使用

void mysql_free_result(MYSQL_RES *result);
//释放result空间

MYSQL_RES *mysql_use_result(MYSQL *connection);
//返回执行结果,适用于数据量较大时

unsigned int mysql_field_count(MYSQL *connection);
//返回查询结果中的列数(column数)

MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);
//获得查询结果中的列名等信息(表头信息)
技术分享

例子:

创建测试数据库
mysql> create database test;
创建用户
mysql> grant all on test.* to test@‘localhost‘ identified by ‘test‘;

sql文件:

技术分享
--
-- Create the table children
--
CREATE TABLE children (
childno int(11) NOT NULL auto_increment,
fname varchar(30),
age int(11),
PRIMARY KEY (childno)
);
--
-- Populate the table ‘children’
--

INSERT INTO children(childno,fname,age) VALUES(1,Jenny,21);
INSERT INTO children(childno,fname,age) VALUES(2,Andrew,17);
INSERT INTO children(childno,fname,age) VALUES(3,Gavin,8);
INSERT INTO children(childno,fname,age) VALUES(4,Duncan,6);
INSERT INTO children(childno,fname,age) VALUES(5,Emma,4);
INSERT INTO children(childno,fname,age) VALUES(6,Alex,15);
INSERT INTO children(childno,fname,age) VALUES(7,Adrian,9);
技术分享

导入sql文件:

mysql -u test --password=test test<mysqlchildren.sql

导入后的情况:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| children |
+----------------+
1 row in set (0.00 sec)

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

C代码:

技术分享
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>

MYSQL *mysql_main;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
void display_header();
void display_row();
int main(int argc,char *argv[])
{
    int res;
    int first_row = 1;

    mysql_main = mysql_init(NULL);

    if(!mysql_main)
    {
        fprintf(stderr,"mysql_init failed\n");
        return EXIT_FAILURE;
    }
    mysql_main = mysql_real_connect(mysql_main,"localhost","test","test","test",0,NULL,0);
    if(mysql_main)
    {
        printf("Connection success:\n");
        res = mysql_query(mysql_main,"SELECT childno,fname,age FROM children WHERE age>5");
        if(res)
        {
            fprintf(stderr,"SELECT error: %s\n",mysql_error(mysql_main));
        }
        else
        {
            res_ptr = mysql_use_result(mysql_main);
            if(res_ptr)
            {
                while((sqlrow = mysql_fetch_row(res_ptr)))
                {
                    if(first_row)
                    {
                        display_header();
                        first_row = 0;
                    }
                    display_row();
                }
            }
        }
    }
    else
    {
        printf("Connection failed\n");
    }
    mysql_close(mysql_main);
    return EXIT_SUCCESS;
}
void display_header()
{
    MYSQL_FIELD *field_ptr;
    printf("Column details:\n");
    while((field_ptr = mysql_fetch_field(res_ptr))!=NULL)
    {
        printf("\t Name: %s\n",field_ptr->name);
        printf("\t Type: ");
        if(IS_NUM(field_ptr->type))
        {
            printf("Numeric filed\n");
        }
        else
        {
            switch(field_ptr->type)
            {
            case FIELD_TYPE_VAR_STRING:
                printf("VARCHAR\n");
                break;
            case FIELD_TYPE_LONG:
                printf("LONG\n");
                break;
            default:
                printf("Type is %d,check in mysql_com.h\n",field_ptr->type);
            }
        }
        printf("\t MAX width %ld\n",field_ptr->length);
        if(field_ptr->flags&AUTO_INCREMENT_FLAG)
            printf("\t Auto increments\n");
        printf("\n");
    }
}
void display_row()
{
    unsigned int field_count;

    field_count = 0;
    while(field_count<mysql_field_count(mysql_main))
    {
        if(sqlrow[field_count]) printf("%s ",sqlrow[field_count]);
        else printf("NULL");
        field_count++;
    }
    printf("\n");
}
技术分享

gcc -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -o mysqltest mysqltest.c

./test

结果如下:

Connection success:
Column details:
Name: childno
Type: Numeric filed
MAX width 11
Auto increments

Name: fname
Type: VARCHAR
MAX width 30

Name: age
Type: Numeric filed
MAX width 11

1 Jenny 21 
2 Andrew 17 
3 Gavin 8 
4 Duncan 6 
6 Alex 15 
7 Adrian 9

 

mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server - 鞋chen3888015.taobao.com - 51C (2013/8/5 15:49:09)

mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server2012-09-10 21:15:32标签:mysql 远程访问 mysql远程访问原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://chen3888015.blog.51cto.com/2693016/986841

mysql> update user set host = ‘%‘ where user = ‘root‘;
ERROR 1062 (23000): Duplicate entry ‘%-root‘ for key 1
mysql> select host,user from user;
+------------------+------+
| host             | user |
+------------------+------+
| %                | root |
| 127.0.0.1        | root |
| 192.168.33.110   | root |
| yunwei2.uid5a.cn | root |
+------------------+------+
4 rows in set (0.00 sec)

mysql> select host,user from user;
+------------------+------+
| host             | user |
+------------------+------+
| %                | root |
| 127.0.0.1        | root |
| 192.168.33.110   | root |
| yunwei2.uid5a.cn | root |
+------------------+------+
4 rows in set (0.00 sec)

mysql> grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘uid5a827‘ with g                                                                                                 rant option;
Query OK, 0 rows affected (0.00 sec)

mysql> flush;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that                                                                                                  corresponds to your MySQL server version for the right syntax to use near ‘‘ at                                                                                                  line 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to ‘root‘@‘buy2.w98-e5.ezwebtest.com‘ identified by ‘abcd1234‘ with grant option;
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

参考文章

http://till.iteye.com/blog/115659

如果你想连接你的mysql的时候发生这个错误:

ERROR 1130: Host ‘192.168.1.3‘ is not allowed to connect to this MySQL server

解决方法:
1。 改表法。可能是你的帐号不允许从远程登陆,只能在localhost。这个

人气教程排行