赞
踩
windows下vscode连接Linux中的MySQL教程见:https://blog.csdn.net/weixin_47156401/article/details/129469835?spm=1001.2014.3001.5501
分布式数据库,顾名思义:分布式+数据库。用一句话总结为:由多个独立实体组成,并且彼此通过网络进行互联的数据库。
使用vscode连接Linux中的mysql,并进行mysql开发,对于初学者来讲多少有些摸不着头脑,下面给出mysql的建库、建表,以及增、删、改、查等基本操作。
- #include<iostream>
- #include<mysql.h>
- #include<cstring>
- #include<istream>
- #include<vector>
-
- using namespace std;
-
- MYSQL mysql; //mysql连接
- MYSQL_FIELD*fd; //字段列数组
- char field[32][32]; //存字段名二维数组
- MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
- MYSQL_ROW column; //一个行数据的类型安全(type-safe)的表示,表示数据行的列
-
- bool ConnectDatabase(); //函数声明
- void FreeConnect();
- bool InsertData(); //增
- bool ModifyData(); //改
- bool DeleteData(); //删
-
-
- int main(){
- ConnectDatabase();
- InsertData();
- ModifyData();
- DeleteData();
- mysql_close(&mysql);
- return 0;
- }
- //连接mysql并创建库和表
- bool ConnectDatabase(){
- //初始化mysql连接
- mysql_init(&mysql); //等价于&mysql=mysql_init(NULL);
-
- const char host[]="localhost";
- const char user[]="root";
- const char psw[]="123456";
- //const char table[]="test"; //需要链接的数据库,如果已经创建了直接连接即可
-
- const int port =3306;
-
- //返回false则连接失败,返回true则连接成功
- if(!mysql_real_connect(&mysql, host, user, psw, NULL, port, NULL,0))
- {
- //中间分别是主机,用户名,密码,数据库名,端口号(可以写默认0或者3306等),可以先写成参数再传进去
- cout<<"Error connecting to database:"<<mysql_error(&mysql)<<endl;
- return false;
- }
- else
- {
- cout<<"Connected..."<<endl;
- }
-
- //-------------创建库-------------//
- char *drop_db_query = "DROP DATABASE IF EXISTS my_database";
- mysql_query(&mysql, drop_db_query);
-
- string db_name= "CREATE DATABASE IF NOT EXISTS my_database";
-
- //执行库创建语句,返回0表示执行成功,返回1表示执行失败
- int result=mysql_query(&mysql,db_name.c_str());
-
- if(result){
- cout<<"库创建失败!"<<endl;
- return false;
- }else{
- cout<<"student库创建成功......"<<endl;
- }
-
- // //-------------连接数据库-------------//
- // if(!mysql_real_connect(&mysql, host, user, psw, "my_database", port, NULL,0))
- // {
- // //中间分别是主机,用户名,密码,数据库名,端口号(可以写默认0或者3306等),可以先写成参数再传进去
- // cout<<"Error connecting to database:"<<mysql_error(&mysql)<<endl;
- // return false;
- // }
- // else
- // {
- // cout<<"Connected..."<<endl;
- // return true;
- // }
-
- //选择数据库
- if (mysql_select_db(&mysql, "my_database")) {
- cout<<"选择数据库失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- //-------------在my_database库中创建表-------------//
- char *drop_table_query = "DROP TABLE IF EXISTS my_table";
- mysql_query(&mysql, drop_table_query);
-
- string table="CREATE TABLE IF NOT EXISTS my_table (id int, name VARCHAR(20), number VARCHAR(20), email VARCHAR(20))";
-
- if (mysql_query(&mysql, table.c_str())) {
- cout << "user创建失败!" << mysql_error(&mysql) << endl;
- return false;
- }else{
- cout << "user创建成功......"<<endl;
- }
- }
- //插入数据
- bool InsertData(){
-
- //插入数据
- string query="INSERT INTO my_table (id, name, number, email) VALUES (1, 'liubei', '33333333', '165438490@qq.com')";
-
- //执行SQL语句
- if(mysql_query(&mysql, query.c_str())){
- cout<<"数据插入失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
- else
- {
- cout<<"数据插入成功!"<<endl;
- }
-
- //插入数据
- string data1="insert into my_table values(2, 'guanyu', '22222222', '163438490@qq.com');";
- mysql_query(&mysql, data1.c_str());
-
- //插入数据
- string data2="insert into my_table values(3, 'zhangfei', '11111111', '164438490@qq.com');";
- mysql_query(&mysql, data2.c_str());
-
- // select and print data
- string query2 = "SELECT * FROM my_table";
-
- if (mysql_query(&mysql, query2.c_str())) {
- cout<<"数据查询失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- MYSQL_RES *res = mysql_use_result(&mysql);
- MYSQL_ROW row;
-
- while ((row = mysql_fetch_row(res)) != NULL) {
- cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<endl;
- }
- mysql_free_result(res);
- }
- //修改数据
- bool ModifyData(){
- //修改内容
- string query="UPDATE my_table SET email='166438490@qq.com' where name='liubei'";
-
- //执行SQL语句
- if(mysql_query(&mysql, query.c_str())){
- cout<<"修改数据失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }else{
- cout<<"数据修改成功!"<<endl;
- }
-
- // select and print data
- string query2 = "SELECT * FROM my_table";
-
- if (mysql_query(&mysql, query2.c_str())) {
- cout<<"数据查询失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- MYSQL_RES *res = mysql_use_result(&mysql);
- MYSQL_ROW row;
-
- while ((row = mysql_fetch_row(res)) != NULL) {
- cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<endl;
- }
- mysql_free_result(res);
- }
- //删除数据
- bool DeleteData(){
- /*eg: "DELETE FROM my_table where id=2";*/
- string query="DELETE FROM my_table where id=2";
- cout<<"please input the sql:"<<endl;
- getline(cin,query);//这里手动输入sql语句
-
- //执行SQL语句
- if(mysql_query(&mysql, query.c_str())){
- cout<<"删除数据失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }else{
- cout<<"成功删除数据!"<<endl;
- }
-
- // select and print data
- string query2 = "SELECT * FROM my_table";
-
- if (mysql_query(&mysql, query2.c_str())) {
- cout<<"数据查询失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- MYSQL_RES *res = mysql_use_result(&mysql);
- MYSQL_ROW row;
-
- while ((row = mysql_fetch_row(res)) != NULL) {
- cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<endl;
- }
- mysql_free_result(res);
- }
(6)释放资源
- //释放资源
- void FreeConnect(){
- mysql_free_result(res); //释放一个结果集合使用的内存。
- mysql_close(&mysql); //关闭一个服务器连接。
- }
- #include<iostream>
- #include<mysql.h>
- #include<cstring>
- #include<istream>
- #include<vector>
-
- using namespace std;
-
- MYSQL mysql; //mysql连接
- MYSQL_FIELD*fd; //字段列数组
- char field[32][32]; //存字段名二维数组
- MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
- MYSQL_ROW column; //一个行数据的类型安全(type-safe)的表示,表示数据行的列
-
- bool ConnectDatabase(); //函数声明
- void FreeConnect();
- bool InsertData(); //增
- bool ModifyData(); //改
- bool DeleteData(); //删
-
-
- int main(){
- ConnectDatabase();
- InsertData();
- ModifyData();
- DeleteData();
-
- mysql_close(&mysql);
- return 0;
- }
-
- //连接mysql并创建库和表
- bool ConnectDatabase(){
- //初始化mysql连接
- mysql_init(&mysql); //等价于&mysql=mysql_init(NULL);
-
- const char host[]="localhost";
- const char user[]="root";
- const char psw[]="123456";
- //const char table[]="test"; //需要链接的数据库,如果已经创建了直接连接即可
-
- const int port =3306;
-
- //返回false则连接失败,返回true则连接成功
- if(!mysql_real_connect(&mysql, host, user, psw, NULL, port, NULL,0))
- {
- //中间分别是主机,用户名,密码,数据库名,端口号(可以写默认0或者3306等),可以先写成参数再传进去
- cout<<"Error connecting to database:"<<mysql_error(&mysql)<<endl;
- return false;
- }
- else
- {
- cout<<"Connected..."<<endl;
- }
-
- //-------------创建库-------------//
- char *drop_db_query = "DROP DATABASE IF EXISTS my_database";
- mysql_query(&mysql, drop_db_query);
-
- string db_name= "CREATE DATABASE IF NOT EXISTS my_database";
-
- //执行库创建语句,返回0表示执行成功,返回1表示执行失败
- int result=mysql_query(&mysql,db_name.c_str());
-
- if(result){
- cout<<"库创建失败!"<<endl;
- return false;
- }else{
- cout<<"student库创建成功......"<<endl;
- }
-
- // //-------------连接数据库-------------//
- // if(!mysql_real_connect(&mysql, host, user, psw, "my_database", port, NULL,0))
- // {
- // //中间分别是主机,用户名,密码,数据库名,端口号(可以写默认0或者3306等),可以先写成参数再传进去
- // cout<<"Error connecting to database:"<<mysql_error(&mysql)<<endl;
- // return false;
- // }
- // else
- // {
- // cout<<"Connected..."<<endl;
- // return true;
- // }
-
- //选择数据库
- if (mysql_select_db(&mysql, "my_database")) {
- cout<<"选择数据库失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- //-------------在my_database库中创建表-------------//
- char *drop_table_query = "DROP TABLE IF EXISTS my_table";
- mysql_query(&mysql, drop_table_query);
-
- string table="CREATE TABLE IF NOT EXISTS my_table (id int, name VARCHAR(20), number VARCHAR(20), email VARCHAR(20))";
-
- if (mysql_query(&mysql, table.c_str())) {
- cout << "user创建失败!" << mysql_error(&mysql) << endl;
- return false;
- }else{
- cout << "user创建成功......"<<endl;
- }
- }
-
-
- //释放资源
- void FreeConnect(){
- mysql_free_result(res); //释放一个结果集合使用的内存。
- mysql_close(&mysql); //关闭一个服务器连接。
- }
-
- /******************************数程库操作***********************************/
-
- //插入数据
- bool InsertData(){
-
- //插入数据
- string query="INSERT INTO my_table (id, name, number, email) VALUES (1, 'liubei', '33333333', '165438490@qq.com')";
-
- //执行SQL语句
- if(mysql_query(&mysql, query.c_str())){
- cout<<"数据插入失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
- else
- {
- cout<<"数据插入成功!"<<endl;
- }
-
- //插入数据
- string data1="insert into my_table values(2, 'guanyu', '22222222', '163438490@qq.com');";
- mysql_query(&mysql, data1.c_str());
-
- //插入数据
- string data2="insert into my_table values(3, 'zhangfei', '11111111', '164438490@qq.com');";
- mysql_query(&mysql, data2.c_str());
-
- // select and print data
- string query2 = "SELECT * FROM my_table";
-
- if (mysql_query(&mysql, query2.c_str())) {
- cout<<"数据查询失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- MYSQL_RES *res = mysql_use_result(&mysql);
- MYSQL_ROW row;
-
- while ((row = mysql_fetch_row(res)) != NULL) {
- cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<endl;
- }
- mysql_free_result(res);
- }
-
- //修改数据
- bool ModifyData(){
- //修改内容
- string query="UPDATE my_table SET email='166438490@qq.com' where name='liubei'";
-
- //执行SQL语句
- if(mysql_query(&mysql, query.c_str())){
- cout<<"修改数据失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }else{
- cout<<"数据修改成功!"<<endl;
- }
-
- // select and print data
- string query2 = "SELECT * FROM my_table";
-
- if (mysql_query(&mysql, query2.c_str())) {
- cout<<"数据查询失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- MYSQL_RES *res = mysql_use_result(&mysql);
- MYSQL_ROW row;
-
- while ((row = mysql_fetch_row(res)) != NULL) {
- cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<endl;
- }
- mysql_free_result(res);
- }
-
- //删除数据
- bool DeleteData(){
- /*eg: "DELETE FROM my_table where id=2";*/
- string query="DELETE FROM my_table where id=2";
- cout<<"please input the sql:"<<endl;
- getline(cin,query);//这里手动输入sql语句
-
- //执行SQL语句
- if(mysql_query(&mysql, query.c_str())){
- cout<<"删除数据失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }else{
- cout<<"成功删除数据!"<<endl;
- }
-
- // select and print data
- string query2 = "SELECT * FROM my_table";
-
- if (mysql_query(&mysql, query2.c_str())) {
- cout<<"数据查询失败!"<<mysql_error(&mysql)<<endl;
- return false;
- }
-
- MYSQL_RES *res = mysql_use_result(&mysql);
- MYSQL_ROW row;
-
- while ((row = mysql_fetch_row(res)) != NULL) {
- cout<<row[0]<<" "<<row[1]<<" "<<row[2]<<" "<<row[3]<<endl;
- }
- mysql_free_result(res);
- }
若有帮到你,点个赞,留下你的脚印哦!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。