C语言连接MySQL数据库
今天在网上查询了一些关于C语言连接MySQL的一些资料,现在做一下总结。首先,自己的电脑要安装MySQL,具体的做法是打开终端(Ct
今天在网上查询了一些关于C语言连接MySQL的一些资料,现在做一下总结。
首先,自己的电脑要安装MySQL,具体的做法是打开终端("Ctrl + Alt + t"),然后输入命令"sudo apt-get install mysql",再连按2下Tab键,目的是查看自己当前的软件源中有关MySQL的信息。
@linux:~$ sudo apt-get install mysql
mysql-client mysql-mmm-monitor mysql-source-5.6
mysql-client-5.5 mysql-mmm-tools mysqltcl
mysql-client-5.6 mysql-proxy mysql-testsuite
mysql-client-core-5.5 mysql-server mysql-testsuite-5.5
mysql-client-core-5.6 mysql-server-5.5 mysql-testsuite-5.6
mysql-common mysql-server-5.6 mysqltuner
mysql-common-5.6 mysql-server-core-5.5 mysql-utilities
mysql-mmm-agent mysql-server-core-5.6 mysql-workbench
mysql-mmm-common mysql-source-5.5 mysql-workbench-data
@linux:~$ sudo apt-get install mysql
然后选择安装MySQL服务器和MySQL客户端
@linux:~$ sudo apt-get install mysql-client mysql-server
[sudo] password for zhiniaobu:
这样还不够,,我们还需要再安装一些文件,输入命令"sudo apt-get install libmysql",再连按2下Tab键,目的是查看自己当前的软件源中有关MySQL的信息。
@linux:~$ sudo apt-get install libmysql
libmysql++3 libmysqlcppconn7 libmysql++-doc
libmysql6.4-cil libmysqlcppconn-dev libmysqld-pic
libmysql-cil-dev libmysqld-dev libmysql-java
libmysqlclient18 libmysql++-dev libmysql-ocaml
libmysqlclient-dev libmysql-diff-perl libmysql-ocaml-dev
@linux:~$ sudo apt-get install libmysql
然后我们选择安装libmysqlclient18(选择安装libmysqlclient-dev也可以)
@linux:~$ sudo apt-get install libmysqlclient18
[sudo] password for zhiniaobu:
到现在,准备工作已经完成了。为什么我喜欢连按2下Tab键,因为在网上查询的一些方法不一定适合自己,原因是自己的操作系统、软件源中的软件可能和网上的作者不一样以及资料过于陈旧等外在因素,所以我们要结合自己的实际情况来操作,而Tab键正好可以自动补全命令,帮助我们查看自己所需要的清单。
--------------------------------------分割线 --------------------------------------
Ubuntu 14.04下安装MySQL
《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF
Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL
Ubuntu 14.04下搭建MySQL主从服务器
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb
--------------------------------------分割线 --------------------------------------
接下来就写一个简单的c语言程序来连接MySQL(如果没有编译器的话,就自己动手安装一下吧)。
#include
#include
int main(void)
{
MYSQL *conn_ptr = mysql_init(NULL);//初始化MYSQL结构
if(!conn_ptr)
{
printf("分配失败\n");
return 0;
} // 主机ip 用户 密码 要连接的数据库名
conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","mysql","ceshi",0,NULL,0);
if(conn_ptr)
printf("连接成功\n");
else
printf("连接失败\n");
mysql_close(conn_ptr);
return 0;
}
@linux:~$ cd Zhuo_Mian/
@linux:~/Zhuo_Mian$ ls
c语言连接MySQL.c MySQL photoshop
@linux:~/Zhuo_Mian$ gcc -I /usr/include/mysql c语言连接MySQL.c -L /usr/lib/mysql -l mysqlclient
@linux:~/Zhuo_Mian$ ls
a.out c语言连接MySQL.c MySQL photoshop
@linux:~/Zhuo_Mian$ ./a.out
连接成功
@linux:~/Zhuo_Mian$
PS:
我的mysql用户的名字是"root",密码是"mysql",里面有个数据库叫"ceshi"。
由于程序里面包含了"mysql.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



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

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())

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.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

This article will provide a detailed introduction to how to install and register a Bitcoin trading application. The Bitcoin trading app allows users to manage and trade cryptocurrencies such as Bitcoin. The article guides users through the installation and registration process step by step, including downloading applications, creating accounts, performing identity verification, and first deposit. The goal of the article is to provide beginners with clear and easy-to-understand guidelines to help them easily enter the world of Bitcoin trading.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

This article recommends the top ten digital currency trading apps in the world, including Binance, OKX, Huobi Global, Coinbase, Kraken, Gate.io, KuCoin, Bitfinex, Gemini and Bitstamp. These platforms have their own characteristics in terms of transaction pair quantity, transaction speed, security, compliance, user experience, etc. For example, Binance is known for its high transaction speed and extensive services, while Coinbase is more suitable for novices. Choosing a platform that suits you requires comprehensive consideration of your own needs and risk tolerance. Learn about the world's mainstream digital currency trading platforms to help you conduct digital asset trading safely and efficiently.

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.
