第14章学习笔记
第14章:MySQL数据库系统
知识点归纳总结:
本章讨论了MySQL关系数据库系统;介绍了MySQL并指出了它的重要性;
展示了如何在Linux机器上安装和运行MySQL;演示了如何使用MySQL在命令模式和批处理模式下使用SQL脚本创建和管理数据库;
说明了如何将MySQL与C编程相结合;演示了如何将MySQL与PHP集成,通过动态Web页面创建和管理数据库。
其中让我最有收获的几个部分如下:
- MySQL 简介
- 安装 MySQL
- 使用 MySQL
- C语言MySQL编程
mysql简介:
MySQL (MySQL2018 )是一个关系数据库系统(Codd 1970)c在关系数据库中,数据存储在表中。每个表由多个行和列组成。表中的数据相互关联。表也可能与其他表有关联。关系结构使得可在表上运行查询来检索信息并修改数据库中的数据。关系数据库系统的标准查询语言是SQL(结构化查询语言),包括MySQL。
MySQL是一个开源数据库管理系统,由服务器和客户机组成。在将客户机连接到服 务器后,用户可向服务器输入SQL命令,以便创建数据库,删除数据库,存储、组织和检索数据库中的数据。MySQL有广泛的应用。除了提供标准的数据库系统服务外,MySQL和PHP(PHP 2018)已成为大多数数据管理和在线商务网站的主干网。本章介绍了MySQLo我们将介绍MySQL的基础知识,包括如何在Linux中安装/配置MySQL,如何使用MySQL创建和管理简单数据库,以及如何在C语言和PHP编程环境中与MySQL交互。
mysql显示数据库
C语言mysql编程:
#include <stdio.h>
#include <my_global.h>
#include <mysql.h>
int main(int argc, char *argc[])
{
printf("MySQL client version is : %s\n", mysql_get_client_info());
}
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>
int main(int argc, char *argv[ ])
{
// 1. define a connection object
MYSQL con;
// 2. Initialize the connection object
if (mysql_init(&con))
{
("Connection handle initialized\n");
}
else
{
printf("Connection handle initialization failed\n");
exit(1);
}
// 3. Connect to MySQL server on localhost
if (mysql_real_connect(&con, "localhost","root",NULL,"filetransfer",3306, NULL, 0))
{
printf("Connection to remote MySQL server OK\n");
}
else
{
printf("Connection to remote MySQL failed\n");
exit(1);
}
mysql_close(&con);
}
// C14.1.c: build MySQL database in C
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>
MYSQL *con; // connection object pointer
void error()
{
printf("errno = %d %s\n", mysql_errno(con), mysql_error(con));
mysql_close(con);
exit(1);
}
int main(int argc, char *argv[ ])
{
con = mysql_init(NULL); // will allocate and initialize it
if (con == NULL)
error();
printf("connect to mySQL server on localhost using database cs360\n");
if (mysql_real_connect(con, "localhost", "root", NULL,"cs360", 0, NULL, 0) == NULL)
error();
printf("connection to server OK\n");
printf("drop students table if exists\n");
if (mysql_query(con, "DROP TABLE IF EXISTS students"))
error();
printf("create students tables in cs360\n");
if (mysql_query(con, "CREATE TABLE students(Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name CHAR(20) NOT NULL, score INT)"))
error();
printf("insert student records into students table\n");
if (mysql_query(con, "INSERT INTO students VALUES(1001,’Baker’,50)"))
error();
if (mysql_query(con, "INSERT INTO students VALUES(1002,’Miller’,65)"))
error();
if (mysql_query(con, "INSERT INTO students VALUES(2001,’Miller’,75)"))
error();
if (mysql_query(con, "INSERT INTO students VALUES(2002,’Smith’,85)"))
error();
printf("all done\n");
mysql_close(con);
}
// C14.2 Program: build MySQL database with mysql_real_query()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <my_global.h>
#include <mysql.h>
#define N 5
int id[ ] = { 1001, 1002, 2001, 2002, 2003};
char name[ ][20] = { "Baker", "Miller", "Smith", "Walton", "Zach"};
int score[ ] = { 65, 50, 75, 95, 85};
MYSQL *con;
void error()
{
printf("errno = %d %s\n", mysql_errno(con), mysql_error(con));
mysql_close(con);
exit(1);
}
int main(int argc, char *argv[ ])
{
int i;
char buf[1024]; // used to create commands for MySQL
con = mysql_init(NULL); // MySQL server will allocate and init con
if (con == NULL)
error();
printf("connect to mySQL server on localhost using database cs360\n");
if (mysql_real_connect(con, "localhost", "root", NULL,"cs360", 0, NULL, 0) == NULL)
error();
printf("connected to server OK\n");
printf("drop students table if exists\n");
if (mysql_query(con, "DROP TABLE IF EXISTS students"))
error();
printf("create students tables in cs360\n");
if (mysql_query(con, "CREATE TABLE students(Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name CHAR(20) NOT NULL, score INT)"))
error();
printf("insert student records into students table\n");
for (i=0; i<N; i++){
printf("id=%8d name=%10s, score=%4d\n",id[i],name[i],score[i]);
sprintf(buf, "INSERT INTO students VALUES (%d, ’%s’, %d);",id[i], name[i], score[i]);
if (mysql_real_query(con, buf, strlen(buf)))
error();
}
printf("all done\n");
mysql_close(con);
}
// C14.3 file: Retrieve MySQL query results
#include <my_global.h>
#include <mysql.h>
#include <string.h>
#define N 5
int id[ ] = { 1001, 1002, 2001, 2002, 2003};
char name[ ][20] = {"baker", "Miller", "Smith", "Walton", "Zach"};
int score[ ] = { 65, 50, 75, 95, 85};
int grade[ ] = { 'D', 'F', 'C', 'A', 'B'};
MYSQL *con;
void error()
{
printf("errno = %d %s\n", mysql_errno(con), mysql_error(con));
mysql_close(con);
exit(1);
}
int main(int argc, char **argv)
{
int i, ncols;
MYSQL_ROW row;
MYSQL_RES *result;
MYSQL_FIELD *column;
char buf[1024];
con = mysql_init(NULL);
if (con == NULL)
error();
printf("connect to mySQL server on localhost using database cs360\n");
if (mysql_real_connect(con, "localhost", "root", NULL,"cs360", 0, NULL, 0) == NULL)
error();
printf("connected to server OK\n");
printf("drop students table if exists\n");
if (mysql_query(con, "DROP TABLE IF EXISTS students"))
error();
printf("create students tables in cs360\n");
if (mysql_query(con, "CREATE TABLE students (Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name CHAR(20) NOT NULL, score INT, grade CHAR(2))"))
error();
printf("insert student records into students table\n");
for (i=0; i<N; i++){
printf("id =%4d name =%-8s score =%4d %c\n",id[i], name[i], score[i], grade[i]);
sprintf(buf, "INSERT INTO students VALUES (%d, '%s', %d, '%c');",id[i], name[i], score[i], grade[i]);
if (mysql_real_query(con, buf, strlen(buf)))
error();
}
printf("retrieve query results\n");
mysql_query(con, "SELECT * FROM students");
result = mysql_store_result(con); // retrieve result
ncols = mysql_num_fields(result); // get number of columns in row
printf("number of columns = %d\n", ncols);
for (i=0; i<ncols; i++){
column = mysql_fetch_field(result); // get each column
printf("column no.%d name = %s\n", i+1, column->name);
}
mysql_query(con, "SELECT * FROM students");
result = mysql_store_result(con);
ncols = mysql_num_fields(result);
printf("columns numbers = %d\n", ncols);
while( row = mysql_fetch_row(result) ){
for (i=0; i<ncols; i++)
printf("%-8s ", row[i]);
printf("\n");
}
printf("all done\n");
mysql_close(con);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?