在 Ubuntu 上安装 MySQL
https://blog.csdn.net/liang19890820/article/details/105071479
使用mysql
参考了:
MySQL 教程 https://www.runoob.com/mysql/mysql-tutorial.html
以下命令简单的演示了创建数据库的过程,数据名为 RUNOOB:
'##从命令行中连接mysql服务器'
@ubuntu16:/home/newdisk/sqltext$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.32-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create DATABASE TESTDB; '##创建数据库的过程,数据名为 TESTDB:'
'##选取了数据库 TESTDB:'
mysql> use TESTDB;
Database changed
'##创建数据表runoob_tbl 删除是 DROP TABLE runoob_tbl'
mysql> CREATE TABLE IF NOT EXISTS `runoob_tbl`(
-> `runoob_id` INT UNSIGNED AUTO_INCREMENT,
-> `runoob_title` VARCHAR(100) NOT NULL,
-> `runoob_author` VARCHAR(40) NOT NULL,
-> `submission_date` DATE,
-> PRIMARY KEY ( `runoob_id` )
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_TESTDB |
+------------------+
| runoob_tbl |
+------------------+
1 row in set (0.00 sec)
mysql> desc runoob_tbl;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| runoob_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| runoob_title | varchar(100) | NO | | NULL | |
| runoob_author | varchar(40) | NO | | NULL | |
| submission_date | date | YES | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
'##插入数据'
mysql> INSERT INTO runoob_tbl(runoob_title,runoob_author,submission_date) VALUES("学习 PHP", "菜鸟教程", NOW());
Query OK, 1 row affected, 1 warning (0.07 sec)
mysql> INSERT INTO runoob_tbl(runoob_title,runoob_author,submission_date) VALUES("学习 MYSQL", "菜鸟教程01", NOW());
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> INSERT INTO runoob_tbl(runoob_title,runoob_author,submission_date) VALUES("学习 C++", "菜鸟教程02", NOW());
Query OK, 1 row affected, 1 warning (0.00 sec)
'##查询数据'
mysql> select * from runoob_tbl;
+-----------+--------------+----------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+----------------+-----------------+
| 1 | 学习 PHP | 菜鸟教程 | 2020-12-06 |
| 2 | 学习 MYSQL | 菜鸟教程01 | 2020-12-06 |
| 3 | 学习 C++ | 菜鸟教程02 | 2020-12-06 |
+-----------+--------------+----------------+-----------------+
3 rows in set (0.00 sec)
使用mysql API进行编程
安装sudo apt-get install mysql-client客户端
碰到问题:dpkg: error processing package XXX (--configure)
解决:https://blog.csdn.net/code_segment/article/details/78559717
安装api 库
方法1:sudo apt-get install libmysqld-dev
方法2:去github上源码安装
需要下面两个文件,放入工程支持mysql api接口
/usr/include/mysql/mysql.h
/usr/lib/x86_64-linux-gnu/libmysqlclient.a
具体参考:MySQL数据库API编程讲义.docx database第四天.one MySQL 5.1参考手册.chm
编译引用了库的应用程序。
gcc hello.c -o hello -I/usr/include/mysql/ -L/usr/lib64/mysql/ -lmysqlclient
MySQL API常用函数
使用MySQL库API函数的一般步骤:
a. 初始化. MYSQL *mysql_init(MYSQL *mysql)
b. 错误处理 unsigned int mysql_errno(MYSQL *mysql)
char *mysql_error(MYSQL *mysql);
c. 建立连接. MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd,const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag);
d. 执行SQL语句 int mysql_query(MYSQL *mysql, const char *stmt_str)
e. 获取结果 MYSQL_RES *mysql_store_result(MYSQL *mysql)
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result)
f. 释放内存 void mysql_free_result(MYSQL_RES *result)
g. 关闭连接 void mysql_close(MYSQL *mysql)