Home Database Mysql Tutorial Mysql入门系列:客户机程序1―连接到MYSQL服务器_MySQL

Mysql入门系列:客户机程序1―连接到MYSQL服务器_MySQL

Jun 01, 2016 pm 01:56 PM
how server program

  6.2 客户机程序1―连接到服务器

  我们的第一个MySQL客户机程序很简单:连接到服务器、断开,并退出。它本身并不是非常有用,但是必须知道如何做它,因为实际上用MySQL数据库做任何事情都必须与服务器连接。这是一个公用的操作,开发创建连接的代码是编写每个客户机程序都将使用的代码。除此之外,这项任务带给我们一些简单开始的事情。以后,我们可以增加这个客户机来做一些更有用的事情。我们第一个客户机程序的源代码,客户机程序1,包括一个单独的文件, client.c:

  这个源文件以包括stdio.h 和mysql.h 开始。MySQL客户机可能包括其他的头文件,但是一般来说至少有两个是最基本的。

  主机名称、用户名称、口令和数据库名称的缺省值固定在代码内部,使事情变得简单。以后,我们将参数化这些值,以便可以在选项文件或命令行中指定它们。程序的main() 函数创建和终止与服务器的连接。建立连接使用如下两个步骤:

  1) 调用mysql_init() 来获取连接处理程序。MYSQL 数据类型是一个包括连接信息的结构。这种类型的变量称为连接处理程序。当我们将NULL 传递给mysql_init() 时,它分配一个MYSQL 变量,初始化它,然后返回一个指向它的指针。

  2) 调用mysql_real_connect() 来创建与服务器的连接。mysql_real_connect() 可有任意数量的参数,例如:

  ■ 连接处理程序的指针。这不能为NULL;它应该是由mysql_init() 返回的值。

  ■ 服务器主机。如果指定NULL 或主机“localhost”,则客户机连接到在本地主机使用UNIX 套接字运行的服务器上。如果指定一个主机名称或主机的IP 地址,则客户机连接到使用TCP/IP 连接命名的主机上。

  在Windows 上,除了使用TCP/IP 连接而不是用UNIX 套接字以外,这种操作是类似的(在Windows NT 上,如果主机为NULL,则在TCP/IP 以前,先试着使用一个指定的管道来连接)。

  ■ 用户名称和口令。如果名称为NULL,则客户机库将逻辑名称发送给服务器。如果口令为NULL,则不发送口令。

  ■ 端口号和套接字文件。这些指定为0 或NULL,来告知客户机库使用它的缺省值。如果不指定端口和套接字,则根据希望连接到的主机确定这些缺省值。附录F 中的mysql_real_connect() 的描述给出有关这些的详细情况。

  ■ 标志值。因为我们不使用任何特定的连接操作,因此它是0。这个参数可用的选项在附录F 中的mysql_real_connect() 的项目中讨论详细情况。要想终止这个连接,可将连接处理程序的指针传递给mysql_ close()。当将连接处理程序传递mysql_close() 来终止个连接时,由mysql_init() 自动分配的连接处理程序自动地释放。

  要想测试客户机程序1,可使用本章前面建立客户机程序时给出的指导来编译和连接,然后运行它:

  % client1  程序连接到服务器、断开并退出。这一点都不令人兴奋,但它是一个开始。然而,它只是一个开始,因为有两个重要的缺点:

  ■ 客户机没有错误检查,所以并不真正地知道实际上它是否在工作!

  ■ 连接参数(主机名称,用户名称等)在源代码内部固定。如果允许用户通过指定选项文件或命令行中的参数来解决这个问题则更好一些。这些问题的处理都不困难。我们将在下面专门解决它们。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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 to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to solve the problem that eMule search cannot connect to the server How to solve the problem that eMule search cannot connect to the server Jan 25, 2024 pm 02:45 PM

Solution: 1. Check the eMule settings to make sure you have entered the correct server address and port number; 2. Check the network connection, make sure the computer is connected to the Internet, and reset the router; 3. Check whether the server is online. If your settings are If there is no problem with the network connection, you need to check whether the server is online; 4. Update the eMule version, visit the eMule official website, and download the latest version of the eMule software; 5. Seek help.

Detailed explanation of CentOS installation fuse and CentOS installation server Detailed explanation of CentOS installation fuse and CentOS installation server Feb 13, 2024 pm 08:40 PM

As a LINUX user, we often need to install various software and servers on CentOS. This article will introduce in detail how to install fuse and set up a server on CentOS to help you complete the related operations smoothly. CentOS installation fuseFuse is a user space file system framework that allows unprivileged users to access and operate the file system through a customized file system. Installing fuse on CentOS is very simple, just follow the following steps: 1. Open the terminal and Log in as root user. 2. Use the following command to install the fuse package: ```yuminstallfuse3. Confirm the prompts during the installation process and enter `y` to continue. 4. Installation completed

Solution to the inability to connect to the RPC server and the inability to enter the desktop Solution to the inability to connect to the RPC server and the inability to enter the desktop Feb 18, 2024 am 10:34 AM

What should I do if the RPC server is unavailable and cannot be accessed on the desktop? In recent years, computers and the Internet have penetrated into every corner of our lives. As a technology for centralized computing and resource sharing, Remote Procedure Call (RPC) plays a vital role in network communication. However, sometimes we may encounter a situation where the RPC server is unavailable, resulting in the inability to enter the desktop. This article will describe some of the possible causes of this problem and provide solutions. First, we need to understand why the RPC server is unavailable. RPC server is a

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

Best Practice Guide for Building IP Proxy Servers with PHP Best Practice Guide for Building IP Proxy Servers with PHP Mar 11, 2024 am 08:36 AM

In network data transmission, IP proxy servers play an important role, helping users hide their real IP addresses, protect privacy, and improve access speeds. In this article, we will introduce the best practice guide on how to build an IP proxy server with PHP and provide specific code examples. What is an IP proxy server? An IP proxy server is an intermediate server located between the user and the target server. It acts as a transfer station between the user and the target server, forwarding the user's requests and responses. By using an IP proxy server

How to configure Dnsmasq as a DHCP relay server How to configure Dnsmasq as a DHCP relay server Mar 21, 2024 am 08:50 AM

The role of a DHCP relay is to forward received DHCP packets to another DHCP server on the network, even if the two servers are on different subnets. By using a DHCP relay, you can deploy a centralized DHCP server in the network center and use it to dynamically assign IP addresses to all network subnets/VLANs. Dnsmasq is a commonly used DNS and DHCP protocol server that can be configured as a DHCP relay server to help manage dynamic host configurations in the network. In this article, we will show you how to configure dnsmasq as a DHCP relay server. Content Topics: Network Topology Configuring Static IP Addresses on a DHCP Relay D on a Centralized DHCP Server

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

See all articles