1.mysql的安装和密码修改:https://blog.csdn.net/qq_41848006/article/details/88295973
2.cmd使用“管理员身份”登陆dos:
- cd E:\mysql-8.0.11-winx64\bin,常见操作:
登陆操作流程:
1.打开网络:net start mysql
2.登陆数据库:mysql - u root - p
3.输入密码
4.... ... /*dos操作*/
5.退出数据库:quit
6.关闭网络:net stop mysql
常见的dos操作:
1.创建数据库DB:create database if not exist 数据库名;
2.删除数据库DB:drop database if exist 数据库名;
3.创建数据表TABLE:create table 表名(列名1 类型, 列名2 类型, ... ...);
4.删除数据表TABLE:drop table 表名;
5.查询数据库DB&TABLE:select * from 表名;
- mysql操作教程:http://c.biancheng.net/view/2574.html
3.使用VS2013,配置mysql环境:
- 新建一个工程(根据需求),在“项目属性”---->"配置引用"----->“VC++目录”:
- 如果有很多项目在此电脑上开发,可以将mysql.dll放入到mysql-8.0.18-winx64\bin,在前面“1.”配置过此环境变量
- 注意一定要匹配好版本,eg,我用的64位,则要将win32改成x64
4.C++操作mysql
- 定制数据库的表特性,最好使用可视化工具实现,毕竟方便,eg,naviat等,代码里封装创建表只是提供思路;
- 只是简单封装(一定要设置主键约束),后面根据需求再扩展。
- sql.h
#ifndef _SQL_H__
#define _SQL_H__
#include "mysql.h"
#include "winsock.h"
#include <vector>
#include <string>
using namespace std;
#pragma comment(lib, "libmysql.lib")
#define QUERY_TYPE_CREATEDB "create database if not exists "
#define QUERY_TYPE_DELETEDB "drop database if exist "
#define QUERY_TYPE_CREATETABLE "create table "
#define QUERY_TYPE_DELETETABLE "drop table if exist "
#define QUERY_TYPE_INSERT "insert into "
#define QUERY_TYPE_UPDATE "update "
#define QUERY_TYPE_DELETE "delete from "
#define QUERY_TYPE_UTF8 "set names GB2312"
#define QUERY_TYPE_SELECT "select "
#define QUERY_TYPE_PRIMARYKEY " PRIMARY KEY"
#define QUERY_COM_FROM " from "
#define QUERY_COM_VALUE "values("
#define QUERY_COM_START "("
#define QUERY_COM_END ")"
#define QUERY_COM_SET " set "
#define QUERY_COM_WHERE " where "
#define QUERY_COM_SPLIT ","
#define QUERY_COM_KEYSPLIT "='"
#define QUERY_COM_VALUESPLIT "'"
#define QUERY_COM_SPACE " "
typedef enum
{
EN_MYSQL_SUCCESS = 0x00,
EN_MYSQL_INIT,
EN_MYSQL_UTF8,
EN_MYSQL_CONNECT,
EN_MYSQL_COMMIT,
EN_MYSQL_INSERT,
EN_MYSQL_UPDATE,
EN_MYSQL_DELETE,
EN_MYSQL_QUERY,
EN_MYSQL_CRATEDB,
EN_MYSQL_SLECTDB,
EN_MYSQL_CREATETABLE,
EN_MYSQL_DELETETABLE,
}EN_MYSQL_ERROR;
typedef struct
{
string strKey;
string strKeyValue;
}ST_MYSQL_KEY, *PST_MYSQL_KEY;
typedef vector<string> VECFIELD;
typedef vector<ST_MYSQL_KEY> VECKEY;
typedef vector<VECKEY> VECKEYARRAY;
class CDBMysql
{
private:
MYSQL *m_pstMysql;
private:
UINT SetUTF8(string strUTF8);
UINT Commit(string strSql);
UINT GetQueryData(VECKEYARRAY *pvecvRecvBuf);
public:
/*动态创建数据库及数据表,可以追加接口对表特性进行定制*/
UINT ConnectSql(const char *pcHost, const char *pcUser, const c