forked from Sunxy7/PracticeCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_api.cpp
More file actions
127 lines (123 loc) · 2.51 KB
/
Copy pathsql_api.cpp
File metadata and controls
127 lines (123 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include"sql_api.h"
sql_api::sql_api()
{
conn = mysql_init(NULL);
}
sql_api::~sql_api()
{
mysql_close(conn);
}
bool sql_api::sql_connect(string host,string user,string password,string dbname)
{
conn = mysql_real_connect(conn,host.c_str(),user.c_str(),password.c_str(), dbname.c_str(),0,NULL,0);
if(conn ==NULL)
{
cout<<"mysql_real_connect is failed"<<endl;
return false;
}
return true;
}
//插入数据
bool sql_api::Insert(const char *info)
{
string sql ="INSERT INTO myhttp_info(id,name,age,sex,school)values(";
sql += info;
sql += ");";
cout<<sql<<endl;
int ret =mysql_query(conn,sql.c_str());
if(ret != 0)
{
return false;
}
return true;
}
//删除数据
bool sql_api::Delete(const char *info)
{
string sql ="DELETE FROM myhttp_info WHERE(";
sql += info;
sql += ");";
cout<<sql<<endl;
int ret =mysql_query(conn,sql.c_str());
if (ret != 0)
{
return false;
}
return true;
}
//修改数据
bool sql_api::Update(const char * info)
{
string sql = "UPDATE myhttp_info SET ";
sql += info;
sql += ';';
cout<<sql<<endl;
int ret =mysql_query(conn,sql.c_str());
if(ret != 0)
{
return false;
}
return true;
}
//查找数据
bool sql_api::Select()
{
string sql="SELECT * FROM myhttp_info;";
int ret = mysql_query(conn,sql.c_str());
if (ret != 0)
{
return false;
}
MYSQL_RES *res =mysql_store_result(conn);
//"id" "name" "age" "sex" "school"
if (res == NULL)
{
return false;
}
int num_fields =mysql_num_fields(res);//行数
MYSQL_FIELD *field ;//
MYSQL_ROW rows;//结果集的下一行
while(field = mysql_fetch_field(res))
{
cout<<field->name<<' ';
}
cout<<endl;
while(rows = mysql_fetch_row(res))
{
int i=0;
for(i=0;i<num_fields;i++)
{
cout<<rows[i]<<' ';
}
cout<<endl;
}
mysql_free_result(res);
return true;
}
int main()
{
sql_api my_api;
if(!my_api.sql_connect("127.0.0.1","root","qaz","myhttp"))
{
return -1;
}
my_api.Select();
my_api.Insert("1,'张三',20,'男','SUST'");
my_api.Insert("2,'李四',21,'男','XATU'");
my_api.Insert("3,'王五',20,'男','清华'");
my_api.Insert("4,'小红',20,'男','北大'");
my_api.Insert("5,'小明',19,'女','西工大'");
my_api.Insert("6,'小李',22,'男','交大'");
my_api.Select();
my_api.Update("age=30 where name ='王五'");
my_api.Select();
my_api.Update("school ='xatu' where id =2");
my_api.Select();
my_api.Update("sex ='女' where name ='小红'");
my_api.Select();
my_api.Delete("name ='张三'");
my_api.Select();
my_api.Delete("school ='西工大'");
my_api.Select();
return 0;
}