Linux Programing -- ch8 -- MySQL
来源:互联网 发布:数据库系统导论 答案 编辑:程序博客网 时间:2024/06/10 12:09
1. 配置文件路径
/etc/mysql/my.cnf
2. 查看变量
$ mysqladmin variables
3. 修改密码
mysqladmin -u root password newpassword
(这种方法会泄密,shell 历史记录)
mysql>SET password=PASSWORD('yourpassword')
4. 导出数据
mysqldump -u root -p dbname > backup.dump
5.数据类型
6. APIs
1. 例程
#include <stdlib.h>#include <stdio.h>#include <mysql/mysql.h>int main(int argc, char *argv[]){ MYSQL *conn_ptr; conn_ptr = mysql_init(NULL); if(!conn_ptr){ fprintf(stderr, "mysql_int failed\n"); return EXIT_FAILURE; } conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "root", "test", 0, NULL, 0); if(conn_ptr){ printf("Connection success\n"); }else{ printf("Connection failed\n"); } mysql_close(conn_ptr); return EXIT_SUCCESS;}
2. 连接数据库
#include <mysql.h>MYSQL *mysql_init(MYSQL *);MYSQL *mysql_real_connect(MYSQL *connection, const char *servar_host, const char *sql_user_name, const char *sql_password, const char *db_name, unsigned int port_number, const char *unix_socket_name, unsigned int flags);void mysql_close(MYSQL *connection);
3. 数据库连接选项
int mysql_options(MYSQL *connection, enum option_to_set, const char *argument);
连接选项只能在 mysql_init 和 mysql_real_connct 之间调用,每次只能设置一个选项
4. 错误处理
unsigned int mysql_errno(MYSQL *connection);char *mysql_error(MYSQL *connection);
5. 执行 SQL 语句
执行查询, 其中 mysql_query 不能查询二进制字段
int mysql_query(MYSQL *mysql, const char *stmt_str);int mysql_real_query(MYSQL *mysql, const char* stmt_str)
获取受影响数
my_ulonglong mysql_affected_rows(MYSQL *mysql);
返回受影响行数,注意:只查询有修改的行数,如果需要返回传统的匹配的行数连接时使用标记:CLIENT_FOUND_ROWS
5. 自动插入内容
当使用了AUTO_INCREMENT
选项建表后,可以使用LAST_INSERT_ID()
获取插入的内容
CREATE TABLE children( childno INTEGER AUTO_INCREMENT NOT NULLPRIMARY KEY, fname VARCHAR(10), age INTEGER);mysql>SELECT LAST_INSERT_ID();
获取结果,mysql_store_result 为一次获取结果,mysql_use_result 为游标方式
MYSQL_RES *mysql_store_result(MYSQL *mysql);MYSQL_RES *mysql_use_result(MYSQL *mysql);void mysql_free_result(MYSQL *result);
游标定位 & 行数获取
void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset); // offset 为行号MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result); // 获取当前位置,注意不是行号,只能用于 mysql_store_resultMYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset); // 移动当前位置,返回之前位置, 只能用于 mysql_store_resultmy_ulonglong mysql_num_rows(MYSQL_RESS *result); // 获取行数,只能用于 mysql_store_result
提取行 & 获取行字段数
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);unsigned int mysql_field_count(MYSQL *mysql);
提取字段信息
MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL *result, MYSQL_FIELD_OFFSET offset);MYSQL *mysql_fetch_field(MYSQL_RES *result);
MYSQL_FIELD 结构定义
typedef struct st_mysql_field { char *name; /* Name of column */ char *org_name; /* Original column name, if an alias */ char *table; /* Table of column if column was a field */ char *org_table; /* Org table name, if table was an alias */ char *db; /* Database for table */ char *catalog; /* Catalog for table */ char *def; /* Default value (set by mysql_list_fields) */ unsigned long length; /* Width of column (create length) */ unsigned long max_length; /* Max width for selected set */ unsigned int name_length; unsigned int org_name_length; unsigned int table_length; unsigned int org_table_length; unsigned int db_length; unsigned int catalog_length; unsigned int def_length; unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */ unsigned int charsetnr; /* Character set */ enum enum_field_types type; /* Type of field. See mysql_com.h for types */ void *extension;} MYSQL_FIELD;
enum enum_field_types { MYSQL_TYPE_DECIMAL, MYSQL_TYPE_TINY, MYSQL_TYPE_SHORT, MYSQL_TYPE_LONG, MYSQL_TYPE_FLOAT, MYSQL_TYPE_DOUBLE, MYSQL_TYPE_NULL, MYSQL_TYPE_TIMESTAMP, MYSQL_TYPE_LONGLONG,MYSQL_TYPE_INT24, MYSQL_TYPE_DATE, MYSQL_TYPE_TIME, MYSQL_TYPE_DATETIME, MYSQL_TYPE_YEAR, MYSQL_TYPE_NEWDATE, MYSQL_TYPE_VARCHAR, MYSQL_TYPE_BIT, MYSQL_TYPE_TIMESTAMP2, MYSQL_TYPE_DATETIME2, MYSQL_TYPE_TIME2, MYSQL_TYPE_NEWDECIMAL=246, MYSQL_TYPE_ENUM=247, MYSQL_TYPE_SET=248, MYSQL_TYPE_TINY_BLOB=249, MYSQL_TYPE_MEDIUM_BLOB=250, MYSQL_TYPE_LONG_BLOB=251, MYSQL_TYPE_BLOB=252, MYSQL_TYPE_VAR_STRING=253, MYSQL_TYPE_STRING=254, MYSQL_TYPE_GEOMETRY=255};
预定义宏 IS_NUM
用于判断是否为数字型
0 0
- Linux Programing -- ch8 -- MySQL
- linux socket programing
- The linux module programing guide
- Begining Linux Programing Chapter 3
- SHELL programing study in linux
- Linux Programing -- ch12-- POSIX 线程
- Begining Linux Programing Chapter 1,2
- The environment of C++ Programing in Linux
- Linux Programing -- ch11-- 进程和信号
- Linux Programing -- ch13-- 进程间通信:管道
- Linux Command Line and....ch8(管理文件系统)
- Linux Programing -- ch14-- 信号量、共享内存、消息队列
- C programing
- Dynamic Programing
- Generic programing
- concurrence programing
- Programing TIPS
- Functional Programing
- 第6周项目1 建立顺序栈算法库
- 第六周项目3-括号的匹配
- LCYZ NOIP模拟赛 —— By t14t41t[大神的题都是四星up]
- 第6周项目3括号的匹配
- 程序设计模式
- Linux Programing -- ch8 -- MySQL
- camera摄像原理之二:色彩空间
- 第四周项目3 单链表应用(2)
- 第七周上机实践项目5——排队看病模拟
- 第七周--项目4队列数组
- 第八周 项目1 建立顺序串
- 第七周 项目3-负数把正数赶出队列
- camera摄像原理之三:色温和自动白平衡
- 第七周项目1--建立顺序环形队列算法库