Table of Contents
C API数据类型
3、MYSQL_ROW这是一个行数据的类型安全(type-safe)的表示。当前它实现为一个计数字节的字符串数组。(如果字段值可能包含二进制数据,你不能将这些视为空终止串,因为这样的值可以在内部包含空字节) 行通过调用mysql_fetch_row()获得。
 
Home Database Mysql Tutorial CAPI--linux上c语言读取数据库内容(mysql)_MySQL

CAPI--linux上c语言读取数据库内容(mysql)_MySQL

Jun 01, 2016 pm 01:04 PM
content database language

要想知道怎么通过c语言操作数据库,必须要了解这几个结构体的含义,这里只是简单说一下如果想要了解的更深入可以到网上搜一下

 

C API数据类型


 

1、MYSQL结构代表一个数据库连接的句柄,包含有关服务器的连接状态的信息,几乎所有的函数均使用到他。要连接MySQL,必须建立MYSQL实例,通过mysql_init初始化方能开始连接。

2、MYSQL_RES结果代表返回的查询结果(select,show等)。也将查询的返回的信息成为“结果集”。在c的API里对应的就是MYSQL_RES了,从数据库读取数据,最后从MYSQL_RES中读取数据。

3、MYSQL_ROW这是一个行数据的类型安全(type-safe)的表示。当前它实现为一个计数字节的字符串数组。(如果字段值可能包含二进制数据,你不能将这些视为空终止串,因为这样的值可以在内部包含空字节) 行通过调用mysql_fetch_row()获得。

 

这里归纳了C API可使用的函数,详见MYSQL手册

函数

描述

mysql_affected_rows()

返回上次UPDATE、DELETE或INSERT查询更改/删除/插入的行数。

mysql_autocommit()

切换 autocommit模式,ON/OFF

mysql_change_user()

更改打开连接上的用户和数据库。

mysql_charset_name()

返回用于连接的默认字符集的名称。

mysql_close()

关闭服务器连接。

mysql_commit()

提交事务。

mysql_connect()

连接到MySQL服务器。该函数已不再被重视,使用mysql_real_connect()取代。

mysql_create_db()

创建数据库。该函数已不再被重视,使用SQL语句CREATE DATABASE取而代之。

mysql_data_seek()

在查询结果集中查找属性行编号。

mysql_debug()

用给定的字符串执行DBUG_PUSH。

mysql_drop_db()

撤销数据库。该函数已不再被重视,使用SQL语句DROP DATABASE取而代之。

mysql_dump_debug_info()

让服务器将调试信息写入日志。

mysql_eof()

确定是否读取了结果集的最后一行。该函数已不再被重视,可以使用mysql_errno()或mysql_error()取而代之。

mysql_errno()

返回上次调用的MySQL函数的错误编号。

mysql_error()

返回上次调用的MySQL函数的错误消息。

mysql_escape_string()

为了用在SQL语句中,对特殊字符进行转义处理。

mysql_fetch_field()

返回下一个表字段的类型。

mysql_fetch_field_direct()

给定字段编号,返回表字段的类型。

mysql_fetch_fields()

返回所有字段结构的数组。

mysql_fetch_lengths()

返回当前行中所有列的长度。

mysql_fetch_row()

从结果集中获取下一行

mysql_field_seek()

将列光标置于指定的列。

mysql_field_count()

返回上次执行语句的结果列的数目。

mysql_field_tell()

返回上次mysql_fetch_field()所使用字段光标的位置。

mysql_free_result()

释放结果集使用的内存。

mysql_get_client_info()

以字符串形式返回客户端版本信息。

mysql_get_client_version()

以整数形式返回客户端版本信息。

mysql_get_host_info()

返回描述连接的字符串。

mysql_get_server_version()

以整数形式返回服务器的版本号。

mysql_get_proto_info()

返回连接所使用的协议版本。

mysql_get_server_info()

返回服务器的版本号。

mysql_info()

返回关于最近所执行查询的信息。

mysql_init()

获取或初始化MYSQL结构。

mysql_insert_id()

返回上一个查询为AUTO_INCREMENT列生成的ID。

mysql_kill()

杀死给定的线程。

mysql_library_end()

最终确定MySQL C API库。

mysql_library_init()

初始化MySQL C API库。

mysql_list_dbs()

返回与简单正则表达式匹配的数据库名称。

mysql_list_fields()

返回与简单正则表达式匹配的字段名称。

mysql_list_processes()

返回当前服务器线程的列表。

mysql_list_tables()

返回与简单正则表达式匹配的表名。

mysql_more_results()

检查是否还存在其他结果。

mysql_next_result()

在多语句执行过程中返回/初始化下一个结果。

mysql_num_fields()

返回结果集中的列数。

mysql_num_rows()

返回结果集中的行数。

mysql_options()

为mysql_connect()设置连接选项。

mysql_ping()

检查与服务器的连接是否工作,如有必要重新连接。

mysql_query()

执行指定为“以Null终结的字符串”的SQL查询。

mysql_real_connect()

连接到MySQL服务器。

mysql_real_escape_string()

考虑到连接的当前字符集,为了在SQL语句中使用,对字符串中的特殊字符进行转义处理。

mysql_real_query()

执行指定为计数字符串的SQL查询。

mysql_refresh()

刷新或复位表和高速缓冲。

mysql_reload()

通知服务器再次加载授权表。

mysql_rollback()

回滚事务。

mysql_row_seek()

使用从mysql_row_tell()返回的值,查找结果集中的行偏移。

mysql_row_tell()

返回行光标位置。

mysql_select_db()

选择数据库。

mysql_server_end()

最终确定嵌入式服务器库。

mysql_server_init()

初始化嵌入式服务器库。

mysql_set_server_option()

为连接设置选项(如多语句)。

mysql_sqlstate()

返回关于上一个错误的SQLSTATE错误代码。

mysql_shutdown()

关闭数据库服务器。

mysql_stat()

以字符串形式返回服务器状态。

mysql_store_result()

检索完整的结果集至客户端。

mysql_thread_id()

返回当前线程ID。

mysql_thread_safe()

如果客户端已编译为线程安全的,返回1。

mysql_use_result()

初始化逐行的结果集检索。

mysql_warning_count()

返回上一个SQL语句的告警数。

下面看一下到底怎么通过C API操作数据库

下面的例子为读取数据库的表中的数据

#include <stdio.h>
#include <mysql.h>
#include <string.h>
int main()
{
MYSQL mysql; //mysql连接
MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
MYSQL_ROW row; //一个行数据的类型安全(type-safe)的表示
char *query; //查询语句
int t,r;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost", "root", NULL, "test",0,NULL,0))
{


printf( "Error connecting to database: %s",mysql_error(&mysql));


}
else
{
printf("Connected...\n");
}
query=" select * from qq";
t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));//执行指定为计数字符串的SQL查询。
if(t)
{
printf("执行显示时出现异常: %s",mysql_error(&mysql));
}
res=mysql_store_result(&mysql);//检索完整的结果集至客户端。
printf("姓名\t学号\t年龄\t\n");
while(row=mysql_fetch_row(res))
{


for(t=0;t<mysql_num_fields(res);t++)
{
printf("%s\t" ,row[t]);
}
printf("\n");


}
mysql_free_result(res);//释放结果集使用的内存。
mysql_close(&mysql);
return 0;
}
Copy after login

运行的命令和代码如下:\

其实它是很简单的东西。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Apr 11, 2024 pm 08:52 PM

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Thoughts and practice on assisted generation of B-end front-end code under large models Thoughts and practice on assisted generation of B-end front-end code under large models Apr 18, 2024 am 09:30 AM

1. Code specifications during background reconstruction work: During the B-end front-end development process, developers will always face the pain point of repeated development. The element modules of many CRUD pages are basically similar, but they still need to be developed manually, and time is spent on simple element construction. This reduces the development efficiency of business requirements. At the same time, because the coding styles of different developers are inconsistent, it makes it more expensive for others to get started during iterations. AI replaces simple brainpower: With the continuous development of large AI models, it has simple understanding capabilities and can convert language into instructions. General instructions for building basic pages can meet the needs of daily basic page building and improve the efficiency of business development in general scenarios. 2. Generate link list. B-side page lists, forms, and details can all be generated. Links can be roughly divided into the following categories:

See all articles