Linux学习笔记
bitsCN.comLinux下C++/C连接MySQL数据库(三)
--处理返回数据
一、通过返回结果集中的字段数
unsigned int mysql_field_count(MYSQL * connection);//将MYSQL_ROW的值作为一个存储了一行数据的数组...
示例:
//一次取一个值的情况,另一种情况与其类似,修改处会标出#include <iostream>#include <fstream>#include <cstdlib>#include <mysql/mysql.h>using namespace std;void mysql_err_function(MYSQL * connection);void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow);int main(){ MYSQL * connection; connection = mysql_init(NULL); if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0)) { cout << "Connection to MySQL Server is Succeed..." << endl; string query = "select * from tmp15"; //getline(cin,query); int res = mysql_query(connection,query.c_str()); if (res) { mysql_err_function(connection); } else { MYSQL_RES * my_res = mysql_use_result(connection); //将mysql_use_result改为mysql_store_result即可得到另一种情况的结果(其实是相同的...) if (my_res) { MYSQL_ROW sqlrow; while ((sqlrow = mysql_fetch_row(my_res))) { mysql_display(connection,sqlrow); } mysql_free_result(my_res); } else { mysql_err_function(connection); } } mysql_close(connection); cout << "Connection to MySQL Server is Closed!" << endl; } else { mysql_err_function(connection); }}void mysql_err_function(MYSQL * connection){ if (mysql_errno(connection)) { cout << "Error " << mysql_errno(connection) << " : " << mysql_error(connection) << endl; exit(-1); }}void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow){ for (unsigned int i = 0; i < mysql_field_count(mysql); ++i) { printf("%s ",sqlrow[i]); //cout << sqlrow[i] << ' '; //不知到为什么将printf换成cout之后,打印值就会出错...思考ing... } cout << endl;}
二、获取一个字段的信息
1、MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result); 2、MYSQL_FIELD定义:typedef struct st_mysql_field{ char *name; /* Name of column */ char *table; /* Table of column if column was a field */ char *org_table; /* Org table name if table was an alias */ char *db; /* Database for table */ char *def; /* Default value (set by mysql_list_fields) */ unsigned long length; /* Width of column */ unsigned long max_length; /* Max width of selected set */ unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */ enum enum_field_types type; /* Type of field. Se mysql_com.h for types *

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools