search
HomeDatabaseMysql TutorialC语言连接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"。

C语言连接MySQL

由于程序里面包含了"mysql.h"文件,所以在编译的时候要加一些东西。

本文永久更新链接地址:

linux

Statement
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
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),