初入mysql的学习
最近几天在完成教练的寒假作业,写一个带mysql的管理系统,写完感觉好多东西要记
所以整理一下,花了两天时间粗糙地完成的作业,并写了随笔(写随笔的时间比写作业的时间长.jpg)
第一部分:mysql的一些简答命令
第二部分:写管理系统的一些准备
第三部分:连接数据库的一些代码
第一部分:
下载mysql,就各种问题,好在还算顺利,就重搞了两次。T-T
接着开始启动mysql,在管理员身份下cmd里输入net start MYSQL56,如果(要关闭就输入net stop MYSQL56)因为我的版本是5.6
然后进mysql下载的地方C:\Program Files\MySQL\MySQL Server 5.6\bin,那一栏输入cmd回车,开始我的初入mysql之旅
输入mysql -u(用户名) -p(密码),比如你的账号设置的分别是root,12345,那么就输入mysql -uroot -p12345
然后就会出现mysql> 这样就算登入成功了(如图1.1所示)
(图1.1,这条没有加;(分号) ! 牢记 ! 所有命令后面都要加;)
至于show databases;这条命令是指查看数据库,有数据库的名字,于是偷懒的我就选择现有的库了,叫mysql的库,输入use mysql;,弹出database changed就可以进行操作了
这里记住要有想要输入中文,要进库后查看mysql>show variables like 'character%';这条命令查看了所有的字符设置是不是utf-8,是的话,就要修改,按照图1.2下面步骤
进入SQLyog,然后进入你的MySQL->右键mysql点击改变数据库,将里面的utf-8改成gbk,这样就应该ok(偷懒的我没有尝试(尝试了不知道为什么无效??),索性直接输入了英文,如果有大佬知道,望评论指点,谢谢)
(图1.2)
现在进入了mysql数据库,进入数据库后想显示里面有啥数据表 show tables;
进入主题!!!! :我们要创造一个数据表,于是
使用create语句创建一张表的键,如下图1.3所示stuinfo这张表
Create table stuinfo(
Stuid int,
Stuname varchar(20),
Gender char,
Borndate datetime);
(图1.3)
这里的stuid,stuname,gender,borndate,是随便取的名字,但后面的int,varchar(20),char,datetime,指的是类型.
现在show tables;就会有一个名叫stuinfo的表,你也可以desc stuinfo; 显示刚创建表的细节
现在我们要添加数据,添加数据的命令则是insert into stuinfo values(1,'baby','nan','2012-1-1');
select * from stuinfo; 查表
alter table stuinfo add column email varchar(20); 插入新列(多一个email地址)
delete from stuinfo where stuid=1; 删除数据
还有更改stuid的名字啊什么的,ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型;
altre table stuinfo change stuid number int;
Drop table stuinfo 删除这张表,黑框框的学习就到了这,(学得一手好皮毛,可能皮毛也不是)
第二部分:
因为暂时一直用c和c++写代码,所以就用c++写管理系统了,只会瓜皮算法的我,搞东搞西,因为数据库是x64,所以编译器也要x64,还要可以使用头文件mysql.h和winsock.h
先做准备工作,我用的是vs,点击项目,最底下那个属性,里面有配置属性->c/c++->常规->附加包含目录修改,修改成如图2.1所示
(图2.1)
下面有一个链接器->常规->附加库目录,修改成如图2.2所示
(图2.2)
链接器->输入->附加依赖项,修改成图2.3
(图2.3)
还要配置管理器,图2.3的右上角,把平台改成x64(这个很重要哦!!)
还要把libmysql.dll(如图2.4)放到你生成的exe旁边
(图2.4)
这样就可以开始写代码了
第三部分:
我先打了一个框架,然后就是写连接数据库的代码了,如图3.1,虽然这里都是个人电脑,密码其实不用打码,但是奈何这是我的众多有用密码,所以就码了
(图3.1)
MYSQL contest(){ MYSQL conn; mysql_init(&conn); if (!mysql_real_connect(&conn, "localhost", "root", "mima", "mysql", 3306, NULL, 0)) { fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&conn)); } else { fprintf(stderr, "Successfully connected to Database.\n"); return conn; } }
有了这个连接数据库的代码,就可以开始写了
这里就展示一个功能吧,uLine指有多少行数据,uField指是一行有多少个数据
inline void All(){ MYSQL conn = contest(); char ans[25] = "select * from stuinfo"; if (!mysql_query(&conn, ans)){ MYSQL_RES *result; unsigned uLine = 0, uField = 0; MYSQL_ROW row; MYSQL_FIELD *pField(NULL); if (!(result = mysql_store_result(&conn))){ cout << "Data acquisition failed:" << mysql_error(&conn) << endl; return; } uLine = (unsigned)mysql_num_rows(result); uField = (unsigned)mysql_num_fields(result); pField = mysql_fetch_fields(result); cout << " OJ NUMBER POINT TYPE TIME\n"; for (int i = 0; i<(int)uLine; i++){ row = mysql_fetch_row(result); for (int j = 0; j<(int)uField; j++){ printf("|%-20s", row[j]); } printf("\n"); } mysql_free_result(result); } else{ cout << "Data acquisition failed:" << mysql_error(&conn) << endl; return; } }
就这样作业在偷懒以及急赶的情况下完成了
这里放一张成品图,以及源码吧
(贴一张帅气捉鱼学长发我的mysql指南)
#include<iostream> #include<string> #include<string.h> #include<stdio.h> #include<winsock.h> #include<mysql.h> #include<set> using namespace std; const string uroot = "1"; const string proot = "1"; inline void Interface(){ system("cls"); cout << " ################# Welcome to title record #################\n"; cout << " 1.Check if there is such a problem\n"; cout << " 2.Insert the solution thinking for this question\n"; cout << " 3.Check the number of questions\n"; cout << " 4.Check all\n"; cout << " 0.Sign out\n"; cout << " ###########################################################\n"; } MYSQL contest(){ MYSQL conn; mysql_init(&conn); if (!mysql_real_connect(&conn, "localhost", "root", "", "mysql", 3306, NULL, 0)) { fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&conn)); } else { fprintf(stderr, "Successfully connected to Database.\n"); return conn; } } inline void Check(){ char OJ[20], NUMBER[20]; MYSQL conn = contest(); cout << " Oj Name: ";cin >> OJ; cout << " Number: "; cin >> NUMBER; char ans[25] = "select * from stuinfo"; if (!mysql_query(&conn, ans)){ MYSQL_RES *result; unsigned uLine = 0; MYSQL_ROW row; MYSQL_FIELD *pField(NULL); if (!(result = mysql_store_result(&conn))){ cout << " Data acquisition failed:" << mysql_error(&conn) << endl; return; } uLine = (unsigned)mysql_num_rows(result); int zong = 0; for (int i = 0; i<(int)uLine; i++){ row = mysql_fetch_row(result); //cout << row[0] << row[1] <<OJ<<NUMBER<< endl; if (!strcmp(row[0],OJ) && !strcmp(row[1],NUMBER)){ zong++; } } mysql_free_result(result); if (zong){ cout <<" YES,I did this."<< endl; } else cout << " NO" << endl; } else{ cout << " Data acquisition failed:" << mysql_error(&conn) << endl; return; } } inline void Insert(){ MYSQL conn = contest(); char query[150],oj[20],nu[20],po[20],ty[30],ti[20]; cout << " Oj Name: "; cin >> oj; cout << " Number: "; cin >> nu; cout << " Point: "; cin >> po; cout << " Type: "; cin >> ty; cout << " Ac Time: "; cin >> ti; char zs[150] = "insert into stuinfo values ('"; strcat_s(zs, oj); strcat_s(zs, "','"); strcat_s(zs, nu); strcat_s(zs, "','"); strcat_s(zs, po); strcat_s(zs, "','"); strcat_s(zs, ty); strcat_s(zs, "','"); strcat_s(zs, ti); strcat_s(zs, "');"); //cout << zs << endl; if (!mysql_query(&conn, zs)){ printf(" Insert success\n");return; } else{ cout << " Data acquisition failed:" << mysql_error(&conn) << endl; return; } } inline void Sum(){ set<string>st; MYSQL conn = contest(); char ans[25] = "select * from stuinfo"; if (!mysql_query(&conn, ans)){ MYSQL_RES *result; unsigned uLine = 0; MYSQL_ROW row; MYSQL_FIELD *pField(NULL); if (!(result = mysql_store_result(&conn))){ cout << "Data acquisition failed:" << mysql_error(&conn) << endl; return; } uLine = (unsigned)mysql_num_rows(result); for (int i = 0; i<(int)uLine; i++){ row = mysql_fetch_row(result); string c = ""; for (int i = 0; i < strlen(row[0]); i++){ c += row[0][i]; } for (int i = 0; i < strlen(row[1]); i++){ c += row[1][i]; } st.insert(c); } mysql_free_result(result); cout << "How many questions have you completed:" << st.size() << endl; } else{ cout << "Data acquisition failed:" << mysql_error(&conn) << endl; return; } } inline void All(){ MYSQL conn = contest(); char ans[25] = "select * from stuinfo"; if (!mysql_query(&conn, ans)){ MYSQL_RES *result; unsigned uLine = 0, uField = 0; MYSQL_ROW row; MYSQL_FIELD *pField(NULL); if (!(result = mysql_store_result(&conn))){ cout << "Data acquisition failed:" << mysql_error(&conn) << endl; return; } uLine = (unsigned)mysql_num_rows(result); uField = (unsigned)mysql_num_fields(result); pField = mysql_fetch_fields(result); cout << " OJ NUMBER POINT TYPE TIME\n"; for (int i = 0; i<(int)uLine; i++){ row = mysql_fetch_row(result); for (int j = 0; j<(int)uField; j++){ printf("|%-20s", row[j]); } printf("\n"); } mysql_free_result(result); } else{ cout << "Data acquisition failed:" << mysql_error(&conn) << endl; return; } } int main(){ system("color 5E"); int flag = 10; cout << " W E L C O M E T O T H E T I T L E R E C O R D " << endl; cout << " we have the best oj calculation guide" << endl; while (flag){ flag--; string s1, s2; cout << endl << "Account Name:"; cin >> s1; cout << "Password:"; cin >> s2; if (s1 == uroot && s2 == proot){ break; } else if (s2 != proot){ cout << "Password Error QAQ" << endl; } else{ cout << "Check No Number QAQ" << endl; } } if (!flag){ int kk = 10; while (kk>0){ system("cls"); printf("\n(○^ε^○)\nSystem Lock:%d\n", kk); Sleep(1000); kk--; } return 0; } Interface(); cout << endl; int num; flag = 1; while (flag){ cout << " Your Choice Number:"; cin >> num; switch (num){ case 0:system("cls"), cout << "\n\n\n\n\n\n\n Thank you for your support" << endl, Sleep(5000); return 0; case 1:Check(); break; case 2:Insert(); break; case 3:Sum(); break; case 4:All(); break; default:cout << " Instruction Error QAQ" << endl; break; } } system("pause"); return 0; }
终于写完了,应该没有遗漏的吧,其实,还有好多操作,我其实看过,但用到的却只有这些,假期的额外延长,导致我拖延症晚期病发,直到昨天教练说要交作业,才把上周下载的mysql打开,开始狂写.上周一个空闲的日子下载mysql(其实是我装歪了,装了两遍,中间各种问题,废了我一天时间),昨天狂赶作业.一种回到高中一天补完寒假作业.jpg
差点来不及,后面的操作指南啊,代码分析啊都写得有些乱糟糟,不过看到队友还在炉石酒馆泡着,替他头大.jpg