09-数据库编程day05( json)
目录:
一、学习目标
二、复习
三、作业
四、
1、安装
2、的简介
3、库的操作
4、新增文档
5、查看文档
6、更新文档
7、删除文档
8、的索引
9、聚合函数
10、副本集实验
11、API 使用
(1)vs配置方法
(2)通过API连接到
(3)通过API删除文档
(4)通过API新增文档
一、学习目标
1.的安装
2.库的操作
3.文档的增删改查
4.的索引
5.的api连接数据库
二、复习
mysql约束的种类? 主键,外键,非空,唯一
mysql如何解决主键冲突问题?
table(
id INT(11)key ,
name (20) ,
(15) not null,
INT(11),
key()(id)
);
》mysql中文乱码问题
--6个字符集
○ 登录可以引起字符集变化
○ LANG环境变量修改也会引起变化
》mysql-api编程的一般流程
--初始化
--连接
….do sth
○
○ 打印表头,打印数据
--关闭连接
三、作业
》分析代码(.c):
#include #include "mysql.h"#include #include #define STRING_SIZE 50 //create table test_table2(col1 int, col2 varchar(30), col3 smallint, col4 timestamp);//insert into test_table2(col1,col2,col3) values(10111,'yekai',10);//insert into test_table2(col1,col2,col3) values(10112,'yekai1',20);//insert into test_table2(col1,col2,col3) values(10113,'yekai1',20); #define SELECT_SAMPLE "SELECT col1, col2, col3, col4 FROM test_table2 where col1=?"void process_fetch(MYSQL *mysql);int main(){MYSQL *mysql=NULL;printf("欢迎来到mysql\n");mysql = mysql_init(NULL);if(mysql == NULL){printf("init err!\n");return -1;} mysql=mysql_real_connect(mysql,"localhost","root","123456","scott",0,NULL,0);if(mysql == NULL){printf("connect to mysql err!\n");return -1;}printf("connect to mysql ok!\n");process_fetch(mysql);mysql_close(mysql);return 0;}void process_fetch(MYSQL *mysql){MYSQL_STMT*stmt;MYSQL_BINDbind[4];//和结果集绑定MYSQL_BINDsbind;//查询条件MYSQL_RES*prepare_meta_result;MYSQL_TIMEts;unsigned long length[4];intparam_count, column_count, row_count;shortsmall_data;intint_data;charstr_data[STRING_SIZE];my_boolis_null[4];/* Prepare a SELECT query to fetch data from test_table */stmt = mysql_stmt_init(mysql);if (!stmt){fprintf(stderr, " mysql_stmt_init(), out of memory\n");exit(0);}if (mysql_stmt_prepare(stmt, SELECT_SAMPLE, strlen(SELECT_SAMPLE))){fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}fprintf(stdout, " prepare, SELECT successful\n");/* Get the parameter count from the statement */param_count= mysql_stmt_param_count(stmt);fprintf(stdout, " total parameters in SELECT: %d\n", param_count);//if (param_count != 0) /* validate parameter count *///{//fprintf(stderr, " invalid parameter count returned by MySQL\n");//exit(0);//}//bind param int sel_col1 ;memset(&sbind,0x00,sizeof(sbind));sbind.buffer_type= MYSQL_TYPE_LONG;sbind.buffer= (char *)&sel_col1;sbind.is_null= 0;sbind.length= &length[0];/* Bind the buffers */if (mysql_stmt_bind_param(stmt, &sbind)){fprintf(stderr, " mysql_stmt_bind_param() failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Fetch result set meta information */prepare_meta_result = mysql_stmt_result_metadata(stmt);if (!prepare_meta_result){fprintf(stderr," mysql_stmt_result_metadata(), returned no meta information\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Get total columns in the query */column_count= mysql_num_fields(prepare_meta_result);fprintf(stdout, " total columns in SELECT statement: %d\n", column_count);if (column_count != 4) /* validate column count */{fprintf(stderr, " invalid column count returned by MySQL\n");exit(0);}sel_col1 = 10111;/* Execute the SELECT query */if (mysql_stmt_execute(stmt)){fprintf(stderr, " mysql_stmt_execute(), failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Bind the result buffers for all 4 columns before fetching them */memset(bind, 0, sizeof(bind));/* INTEGER COLUMN */bind[0].buffer_type= MYSQL_TYPE_LONG;bind[0].buffer= (char *)&int_data;bind[0].is_null= &is_null[0];bind[0].length= &length[0];/* STRING COLUMN */bind[1].buffer_type= MYSQL_TYPE_STRING;bind[1].buffer= (char *)str_data;bind[1].buffer_length= STRING_SIZE;bind[1].is_null= &is_null[1];bind[1].length= &length[1];/* SMALLINT COLUMN */bind[2].buffer_type= MYSQL_TYPE_SHORT;bind[2].buffer= (char *)&small_data;bind[2].is_null= &is_null[2];bind[2].length= &length[2];/* TIMESTAMP COLUMN */bind[3].buffer_type= MYSQL_TYPE_TIMESTAMP;bind[3].buffer= (char *)&ts;bind[3].is_null= &is_null[3];bind[3].length= &length[3];/* Bind the result buffers */if (mysql_stmt_bind_result(stmt, bind)){fprintf(stderr, " mysql_stmt_bind_result() failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Now buffer all results to client */if (mysql_stmt_store_result(stmt))//预处理取回结果集{fprintf(stderr, " mysql_stmt_store_result() failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Fetch all rows */row_count= 0;fprintf(stdout, "Fetching results ...\n");while (!mysql_stmt_fetch(stmt)){row_count++;fprintf(stdout, "row %d\n", row_count);/* column 1 */fprintf(stdout, "column1 (integer): ");if (is_null[0])fprintf(stdout, " NULL\n");elsefprintf(stdout, " %d(%ld)\n", int_data, length[0]);/* column 2 */fprintf(stdout, "column2 (string): ");if (is_null[1])fprintf(stdout, " NULL\n");elsefprintf(stdout, " %s(%ld)\n", str_data, length[1]);/* column 3 */fprintf(stdout, "column3 (smallint) : ");if (is_null[2])fprintf(stdout, " NULL\n");elsefprintf(stdout, " %d(%ld)\n", small_data, length[2]);/* column 4 */fprintf(stdout, "column4 (timestamp): ");if (is_null[3])fprintf(stdout, " NULL\n");elsefprintf(stdout, " d-d-d d:d:d (%ld)\n",ts.year, ts.month, ts.day,ts.hour, ts.minute, ts.second,length[3]);fprintf(stdout, "\n");}/* Validate rows fetched */fprintf(stdout, " total rows fetched: %d\n", row_count);//if (row_count != 2)//{//fprintf(stderr, " MySQL failed to return all rows\n");//exit(0);//}/* Free the prepared result metadata */mysql_free_result(prepare_meta_result);//释放结果集sel_col1 = 10112;/* Execute the SELECT query */if (mysql_stmt_execute(stmt)){fprintf(stderr, " mysql_stmt_execute(), failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Now buffer all results to client */if (mysql_stmt_store_result(stmt)){fprintf(stderr, " mysql_stmt_store_result() failed\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}/* Fetch all rows */row_count= 0;fprintf(stdout, "Fetching results ...\n");while (!mysql_stmt_fetch(stmt)){row_count++;fprintf(stdout, "row %d\n", row_count);/* column 1 */fprintf(stdout, "column1 (integer): ");if (is_null[0])fprintf(stdout, " NULL\n");elsefprintf(stdout, " %d(%ld)\n", int_data, length[0]);/* column 2 */fprintf(stdout, "column2 (string): ");if (is_null[1])fprintf(stdout, " NULL\n");elsefprintf(stdout, " %s(%ld)\n", str_data, length[1]);/* column 3 */fprintf(stdout, "column3 (smallint) : ");if (is_null[2])fprintf(stdout, " NULL\n");elsefprintf(stdout, " %d(%ld)\n", small_data, length[2]);/* column 4 */fprintf(stdout, "column4 (timestamp): ");if (is_null[3])fprintf(stdout, " NULL\n");elsefprintf(stdout, " d-d-d d:d:d (%ld)\n",ts.year, ts.month, ts.day,ts.hour, ts.minute, ts.second,length[3]);fprintf(stdout, "\n");}/* Close the statement */if (mysql_stmt_close(stmt)){fprintf(stderr, " failed while closing the statement\n");fprintf(stderr, " %s\n", mysql_stmt_error(stmt));exit(0);}}
- 《最重要的事,只有一件》笔记——目标不是做得更多,而是让自己需要的事情更少
- 黑科技—Java实现根据一张照片进行精确定位
- 【C++ 函数式编程 】C++中的函数组合:用std::function实现编程
- 游戏开发网站
- c++ 质数——数学知识
- 并发编程学习笔记 基于Python
- 线控转向系统Carsim和Simulink联合仿真模型,带Carsim数据库
- 理论基础知识之————KB Kb Kbps 相关单位的区别和换算
- Python趣味编程:从入门到人工智能,从这35个案例开始,越学越有趣!
- 爬虫|Scrapy框架应用——以武汉大学新闻网为例