今天给大家演示在linux下如果用C++操作mysql1:安装MySQL挂载光盘:
mkdir /cdrom
mount /dev/hdc /cdrom
cd /cdrom/Server
rpm -ivh perl-DBI-1.52-2.el5.i386.rpm
rpm -ivh mysql-5.0.45-7.el5.i386.rpm mysql-bench-5.0.45-7.el5.i386.rpm mysql-devel-5.0.45-7.el5.i386.rpm
rpm -ivh perl-DBD-MySQL-3.0007-2.el5.i386.rpm
rpm -ivh mysql-server-5.0.45-7.el5.i386.rpm mysql-test-5.0.45-7.el5.i386.rpm查看mysql服务状态:
service mysqld status启动服务:
service mysqld start连接数据库:
mysql2:安装gcc工具包rpm -ivh gcc* --force --nodeps 强制安装3:进入mysqlcreate table t1 (id int,name varchar(30));t1表插入数据:
1.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "insert into t1 (id, name) values (1, 'java1');";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}
g++ -o 1.out 1.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql 更新mysql数据;
1.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "insert into t1 (id,name) values (2, 'java2'),(3, 'java3');";
mysql_query(&mysql, sql.c_str());
sql = "update t1 set name = 'java33' where id = 3;";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}
g++ -o 1.out 1.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./1.out mysql 存储过程:
mysql>delimiter //
>create procedure p01()
>begin
>insert into t1 (id,name) values (66, 'java66');
>end;
>//应用存储过程:mysql>delimiter //
>create procedure p01()
>begin
>insert into t1 (id,name) values (66, 'java66');
>end;
>//触发器:
新建t2表:
mysql>delimiter //
>create trigger tr1 after insert on t1 for each row
>begin
>insert into t2 (id,name) values (new.id,new.name);
>end;
>//
>delete from t1 where id = 66;
>//
>delimeter ;vi 3.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
string sql = "call p01();";
mysql_query(&mysql, sql.c_str());
mysql_close(&mysql);
}
g++ -o 3.out 3.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./3.outmysql>delimeter ;
>select * from t1;
>select * from t2;
>存在数据(66, 'java66')查看表中数据的总数:vi 4.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string sql = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout mysql_close(&mysql);
}
g++ -o 4.out 4.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./4.out查处字段的总数:vi 5.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
string str = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout int fieldcount = mysql_num_fields(result);
cout mysql_close(&mysql);
}
g++ -o 5.out 5.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./5.out列出具体字段名:vi 6.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
MYSQL_FIELD *field = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", "test", 3306, NULL, 0);
string str = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout int fieldcount = mysql_num_fields(result);
cout for(int i = 0; i {
field = mysql_fetch_field_direct(result,i);
cout name }
cout mysql_close(&mysql);
}
g++ -o 6.out 6.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./6.out显示表中所有数据:vi 7.cpp
#include
#include
#include
using namespace std;
main()
{
MYSQL mysql;
MYSQL_RES *result = NULL;
MYSQL_FIELD *field = NULL;
mysql_init(&mysql);
mysql_real_connect(&mysql, "localhost", "root", "root", 3306, NULL, 0);
string str = "select id,name from t1;";
mysql_query(&mysql, sql.c_str());
result = mysql_store_result(&mysql);
int rowcount = mysql_num_rows(result);
cout int fieldcount = mysql_num_fields(result);
cout for(int i = 0; i {
field = mysql_fetch_field_direct(result,i);
cout name }
cout MYSQL_ROW row = NULL;
row = mysql_fetch_row(result);
while(NULL != row)
{
for(int i=1; i
cout }
cout row = mysql_fetch_row(result);
}
mysql_close(&mysql);
}
g++ -o 7.out 7.cpp -lmysqlclient -I/usr/include/mysql/ -L/usr/lib/mysql
./out

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment