C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查_MySQL
一、开发环境
ReadHat6.332位、mysql5.6.15、gcc4.4.6
二、编译
gcc-I/usr/include/mysql-L/usr/lib-lmysqlclient main.c -o main.out
-I:指定mysql头文件所在目录(默认去/usr/include目录下寻找所用到的头文件)
-L:指定mysql动态库文件所在目录(默认从/usr/lib目录查找)
-l:链接libmysqlclient.so动态库
-o:生成的可执行文件名
三、完整示例
////main.c//mysql数据库编程////Created by YangXin on 14-5-22.//Copyright (c) 2014年 yangxin. All rights reserved.//#include <stdio.h>#include <stdlib.h>#include <string.h>#include <mysql.h>MYSQL mysql;// 查询int query();// 修改int update();// 添加数据my_ulonglong add();// 参数化添加数据my_ulonglong addByParams();// 删除数据my_ulonglong delete();// 打印数据库服务器信息void printMySqlInfo();int main(int argc, const char * argv[]){ /*连接之前,先用mysql_init初始化MYSQL连接句柄*/ mysql_init(&mysql); /*使用mysql_real_connect连接服务器,其参数依次为MYSQL句柄,服务器IP地址, 登录mysql的用户名,密码,要连接的数据库等*/ if(!mysql_real_connect(&mysql, "localhost", "root", "yangxin", "test", 0, NULL, 0)) { printf("connecting to Mysql error:%d from %s/n",mysql_errno(&mysql), mysql_error(&mysql)); return -1; }else { printf("Connected Mysql successful!/n"); } printMySqlInfo(); // 设置编码 mysql_query(&mysql, "set names utf8"); // 参数化添加数据 addByParams(); // 查询 query(); // 修改 update(); // 添加 add(); // 删除 delete(); /*关闭连接*/ mysql_close(&mysql); return 0;}// 查询int query(){ int flag, i; const char *sql = NULL; MYSQL_RES *res = NULL; MYSQL_ROW row = NULL; MYSQL_FIELD *fields = NULL; sql = "select * from t_user" ; flag = mysql_real_query(&mysql, sql, (unsigned int)strlen(sql)); if (flag) { printf("query error:%d from %s/n",mysql_errno(&mysql),mysql_error(&mysql)); return -1; } // 将查询结果读取到内存当中,如果数据很多的情况会比较耗内存 res = mysql_store_result(&mysql); // res = mysql_use_result(&mysql); // 需要用到的时候,每次从服务器中读取一行 // 字段数量 unsigned int field_count = mysql_field_count(&mysql); printf("field_cout:%d/n",field_count); // 查询总数 my_ulonglong rows = mysql_num_rows(res); printf("%lld/n",rows); // 获取所有字段 fields = mysql_fetch_fields(res); for (int i = 0; i 10"; flag = mysql_real_query(&mysql, sql, strlen(sql)); if (flag) { printf("delete data error:%d from %s/n",mysql_errno(&mysql), mysql_error(&mysql)); return -1; } my_ulonglong affected_rows = mysql_affected_rows(&mysql); printf("删除的行数:%lld/n",affected_rows); printf("success delete %lld record data !/n",affected_rows); return affected_rows;}void printMySqlInfo(){ const char *stat = mysql_stat(&mysql); const char *server_info = mysql_get_server_info(&mysql); const char *clientInfo = mysql_get_client_info(); unsigned long version = mysql_get_client_version(); const char *hostinfo = mysql_get_host_info(&mysql); unsigned long serverversion = mysql_get_server_version(&mysql); unsigned int protoinfo = mysql_get_proto_info(&mysql); printf("stat:%s/n",stat); printf("server_info:%s/n",server_info); printf("clientInfo:%s/n",clientInfo); printf("version:%ld/n",version); printf("hostinfo:%s/n",hostinfo); printf("serverversion:%ld/n",serverversion); printf("protoinfo:%d/n",protoinfo); const char *charactername = mysql_character_set_name(&mysql); printf("client character set:%s/n",charactername); if (!mysql_set_character_set(&mysql, "utf8")) { printf("New client character set: %s/n", mysql_character_set_name(&mysql)); }}</mysql.h></string.h></stdlib.h></stdio.h>

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

macOSandLinuxbothofferuniquestrengths:macOSprovidesauser-friendlyexperiencewithexcellenthardwareintegration,whileLinuxexcelsinflexibilityandcommunitysupport.macOS,developedbyApple,isknownforitssleekinterfaceandecosystemintegration,whereasLinux,beingo
