C++链接mysql
报错
sudo apt-get update
sudo apt-get install libmysqlclient-dev
指令编译
g++ -o mysql_example mysql_example.cpp -I/usr/include/mysql -lmysqlclient
g++ mysql_test.cpp `mysql_config --cflags --libs`
安装mysql
sudo apt update
sudo apt install mysql-server mysql-client
sudo apt install libmysqlcppconn-dev
以下连接mysql
service mysql start
mysql -uroot -p
输入密码 ******
SQL error: Access denied for user 'root'@'localhost'
1. 数据库操作
创建数据库
CREATE DATABASE mydatabase;
使用数据库
USE mydatabase;
删除数据库
DROP DATABASE mydatabase;
2. 表操作
创建表
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
position VARCHAR(255),
salary DECIMAL(10, 2),
hire_date DATE
);
删除表
DROP TABLE employees;
3. 数据操作
插入数据
INSERT INTO employees (name, position, salary, hire_date)
VALUES ('Alice', 'Engineer', 70000.00, '2020-01-15');
3.查询数据
查询所有数据
SELECT * FROM employees;
查询特定列的数据
SELECT name, position FROM employees;
带条件的查询
SELECT * FROM employees WHERE salary > 50000;
排序查询结果
SELECT * FROM employees ORDER BY salary DESC;
限制查询结果的数量
SELECT * FROM employees LIMIT 5;
更新数据
UPDATE employees SET salary = 75000 WHERE name = 'Alice';
删除数据
DELETE FROM employees WHERE name = 'Alice';