




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一Mysql操作基础1. 测试服务器$ mysql u root mysql如果得出mysql提示符,服务器就正常运行了。2. 修改管理员密码:$ mysqladmin u root password newpassword3. 登入管理员帐号4. 创建新用户 创建来自192.168.181网段的登入 5创建并使用数据库6.创建表格7. 添加信息到表格里二. c语言访问mysql数据库1.以用户名neusoft和密码neusoft来连接本机服务器上名为neusoftdb的数据库 例程1:#include #include #include “mysql.h”int main(int argc, char *argv)MYSQL *conn_ptr;conn_ptr = mysql_init(NULL);if (!conn_ptr) fprintf(stderr, “mysql_init failedn”); return EXIT_FAILURE;conn_ptr = mysql_real_connect(conn_ptr, “localhost”, “neusoft”, “neusoft”, “neusoftdb”, 0, NULL, 0);if (conn_ptr) printf(“Connection successn”);else printf(“Connection failedn”);mysql_close(conn_ptr);return EXIT_SUCCESS;编译命令为 $gcc I /usr/include/mysql connect1.c lmysqlclient o connect1执行: $ ./connect1 Connection success $2. 反馈连接失败错误例程2:#include #include #include “mysql.h”int main(int argc, char *argv) MYSQL my_connection; mysql_init(&my_connection); if (mysql_real_connect(&my_connection, “localhost”, “neusoft”, “ “, “neusoftdb”, 0, NULL, 0) printf(“Connection successn”); mysql_close(&my_connection);else fprintf(stderr, “Connection failedn”); if (mysql_errno(&my_connection) fprintf(stderr, “Connection error %d: %sn”, mysql_errno(&my_connection), mysql_error(&my_connection); return EXIT_SUCCESS;执行:$ ./connect2Connection filedConnection error 1045: Access denied for user: neusoftlocalhost (Using password: YES)$3. 插入数据操作#include #include #include “mysql.h”int main(int argc, char *argv)MYSQL my_connection;int res;mysql_init(&my_connection);if (mysql_real_connect(&my_connection, “localhost”, “neusoft”, “neusoft”, “neusoftdb”, 0, NULL, 0) printf(“Connection successn”); res = mysql_query(&my_connection, “INSERT INTO children(fname, age) VALUES(Ann, 3)”); if (!res) printf(“Inserted %lu rowsn”, (unsigned long)mysql_affected_rows(&my_connection); else fprintf(stderr, “Insert error %d: %sn”, mysql_errno(&my_connection), mysql_error(&my_connection); mysql_close(&my_connection);else fprintf(stderr, “Connection failedn”); if (mysql_errno(&my_connection) fprintf(stderr, “Connection error %d: %sn”, mysql_errno(&my_connection), mysql_error(&my_connection); return EXIT_SUCCESS;4.查找插入数据内容 例程4:#include #include #include “mysql.h”int main(int argc, char *argv) MYSQL my_connection; MYSQL_RES *res_ptr; MYSQL_ROW sqlrow; int res; mysql_init(&my_connection); if (mysql_real_connect(&my_connection, “localhost”, “neusoft”, “neusoft”,”neusoftdb”, 0, NULL, 0) printf(“Connection successn”); res = mysql_query(&my_connection, “INSERT INTO children(fname, age) VALUES(Robert, 8)”); if (!res) printf(“Inserted %lu rowsn”, (unsigned long) mysql_affected_rows(&my_connection); else fprintf(stderr, “Insert error %d: %sn”, mysql_errno(&my_connection), mysql_error(&my_connection); res = mysql_query (&my_connection, “SELECT LAST_INSERT_ID()”); if (res) printf(“SELECT error: %sn”, mysql_error(&my_connection); else res_ptr = mysql_use_result(&my_connection); if (res_ptr) while (sqlrow = mysql_fetch_row(res_ptr) printf(“We inserted childNo %sn”, sqlrow0); mysql_free_result(res_ptr); mysql_close(&my_connection);else fprintf(stderr, “Connection failedn”); if (mysql_errno(&my_connecton) fprintf(stderr, “Connection error %d: %sn”, mysql_errno(&my_connection), mysql_error(&my_connection); return EXIT_SUCCESS;编译:$ gcc I/usr/include/mysql insert2.c lmysqlclient o insert2$ ./insert2Connection successInserted 1 rowsWe inserted childNo 3$./insert2Connection success Inserted 1 rowsWe inserted childNo 45提取数据例程5:#include #include #include “mysql.h”MYSQL my_connection;MYSQL_RES *res_ptr;MYSQL_ROW sqlrow;int main(int argc, char *argv)int res;mysql_init(&my_connection);if (mysql_real_connect(&my_connection, “localhost”, “neusoft”, “neusoft”, “neusoftdb”, 0, NULL, 0) printf(“Connection successn”);res = mysql_query(&my_connection, “SELECT childNo, fname, age FROM children WHERE age 5”);if (res) printf(“SELECT error: %sn”, mysql_error(&my_connection); else res_ptr = mysql_store_result(&my_connection); if (res_ptr) printf(“Retrieved %lu rowsn”, (unsigned long) mysql_num_rows(res_ptr); while (sqlrow = mysql_fetch_row(res_ptr) printf(“Fetched datan”); if (mysql_errno(&my_connection) fprintf(stderr, “Retrive error: %sn”, mysql_error(&my_connection); mysql_free_result(res_ptr);mysql_close(&my_connection);else fprintf(stderr, “Connection failedn”); if (mysql_errno(&my_connection) fprintf(stderr, “Connection error %d: %sn”, mysql_errno(&my_connection), mysql_error(&my_connection); retrun EXIT_SUCCESS;6. 显示数据#include #include #include “mysql.h”MYSQL my_connection;MYSQL_RES *res_ptr;MYSQL_ROW sqlrow;void display_header();void display_row();int main(int argc, char *argv)int res;int first_row = 1;mysql_init(&my_connection);if (mysql_real_connect(&my_connection, “localhost”, “neusoft”, “neusoft”, “neusoftdb”, 0, NULL, 0) printf(“Connection successn”); res = mysql_query(&my_connection, “SELECT childNo, fname, age FROM children WHERE age 5”); if (res) fprintf(stderr, “SELECT error: %sn”, mysql_error(&my_connection); else res_ptr = mysql_use_result(&my_connection); if (res_ptr) display_header(); while (sqlrow = mysql_fetch_row(res_ptr) if (first_row) display_header(); first_row = 0; display_row(); if (mysql_errno(&my_connection) fprintf(stderr, “Retrive error: %sn”, mysql_error(&my_connection); mysql_free_result(res_ptr); mysql_close(&my_connection); else fprintf(stderr, “Connection failedn”); if (mysql_errno(&my_connection) fprintf(stderr, “Connection error %d: %sn”,mysql_errno(&my_connection), mysql_error(&my_connection); re
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 保险业务销售及代理合作协议
- 房地产销售合同审核要点及风险提示
- 2025湖南现代冷链物流集团招聘法务主管1人备考考试题库附答案解析
- 土地农业开发使用合同
- 农业生产信息化解决方案合同
- 2025中国邮政集团有限公司安徽省分公司招聘备考考试题库附答案解析
- 2025年智慧化背景下乡村生态康养旅游度假区运营策略研究报告
- 2025云南昆明寻甸回族彝族自治县倘甸社会福利院编外工作人员招聘5人考试参考题库及答案解析
- 2025年河北邯郸涉县秋季博硕人才引进29人考试参考题库及答案解析
- 2025年8月广东广州市天河区枫叶幼儿园编外教辅人员招聘1人考试模拟试题及答案解析
- 2025初中人教版七年级历史上册全册教案
- 汽车文化课件小学生
- 如何当好幼儿园班主任幼儿园班主任培训
- 商务接待培训课件
- 紧急物料采购协议书范本
- 10.1机械能【2025秋北师大新版九上物理主题式课件】
- 2025安全生产法解读与实践
- 脑循环功能障碍治疗仪讲课件
- 呼吸衰竭患者的急救及护理
- 《创新创业教育》说课课件
- 卡丁车俱乐部管理制度
评论
0/150
提交评论