linux 下C++连接mysql数据库
标签: <无>
代码片段(3)
[文件] mydb.h ~ 416B 下载(37)
01 | #ifndef __MY_DB_H |
02 |
03 | #include <mysql/mysql.h> |
04 | #include <stdlib.h> |
05 | #include <stdio.h> |
06 | #include <iostream> |
07 | #include <string.h> |
08 | using namespace std; |
09 | class CMyDB |
10 | { |
11 | public : |
12 | CMyDB(); |
13 | bool initDB(string server_host , string user, string password, string db_name); |
14 | bool executeSQL(string sql_str); |
15 | bool create_table(string table_str_sql); |
16 | ~CMyDB(); |
17 | private : |
18 | MYSQL *connection; |
19 | MYSQL_RES *res; |
20 | MYSQL_ROW row; |
21 | }; |
22 | #endif |
[文件] mydb.cpp ~ 2KB 下载(26)
01 | #include "mydb.h" |
02 |
03 | CMyDB::CMyDB() |
04 | { |
05 | //初始化连接数据库变量 |
06 | connection = mysql_init(NULL); |
07 | if (connection == NULL) |
08 | { |
09 | perror ( "mysql_init" ); |
10 | exit (1); |
11 | } |
12 | } |
13 | CMyDB::~CMyDB() |
14 | { |
15 | //关闭初始化连接数据库变量 |
16 | if (connection != NULL) |
17 | { |
18 | mysql_close(connection); |
19 | } |
20 | } |
21 | //初始化数据库 数据库连接 |
22 | bool CMyDB::initDB(string server_host , string user, string password , string db_name ) |
23 | { |
24 | //运用mysql_real_connect函数实现数据库的连接 |
25 | connection = mysql_real_connect(connection , server_host.c_str() , user.c_str() , password.c_str() , db_name.c_str() , 0 , NULL , 0); |
26 | if (connection == NULL) |
27 | { |
28 | perror ( "mysql_real_connect" ); |
29 | exit (1); |
30 | } |
31 | return true ; |
32 | } |
33 | //执行SQL语句 |
34 | bool CMyDB::executeSQL(string sql_str) |
35 | { |
36 | // 查询编码设定 |
37 | if (mysql_query(connection, "set names utf8" )) |
38 | { |
39 | fprintf (stderr, "%d: %s\n" ,mysql_errno(connection), mysql_error(connection)); |
40 | } |
41 | int t = mysql_query(connection, sql_str.c_str()); |
42 | if (t) |
43 | { |
44 | printf ( "Error making query: %s\n" , mysql_error(connection)); |
45 | exit (1); |
46 | } |
47 | else |
48 | { |
49 | //初始化逐行的结果集检索 |
50 | res = mysql_use_result(connection); |
51 | if (res) |
52 | { |
53 | //mysql_field_count(connection) 返回作用在连接上的最近查询的列数 |
54 | for ( int i = 0 ; i < mysql_field_count(connection) ; i++) |
55 | { |
56 | //检索一个结果集合的下一行 |
57 | row = mysql_fetch_row(res); |
58 | if (row <= 0) |
59 | { |
60 | break ; |
61 | } |
62 | //mysql_num_fields(res) 函数返回结果集中字段的数 |
63 | for ( int r = 0 ; r < mysql_num_fields(res) ; r ++) |
64 | { |
65 | printf ( "%s\t" , row[r]); |
66 | } |
67 | printf ( "\n" ); |
68 | } |
69 | } |
70 | //释放结果集使用的内存 |
71 | mysql_free_result(res); |
72 | } |
73 | return true ; |
74 | } |
75 | //表的创建 |
76 | bool CMyDB::create_table(string table_str_sql) |
77 | { |
78 | int t = mysql_query(connection , table_str_sql.c_str()); |
79 | if (t) |
80 | { |
81 | printf ( "Error making query: %s\n" , mysql_error(connection)); |
82 | exit (1); |
83 | } |
84 | return true ; |
85 | } |
[文件] main.cpp ~ 370B 下载(27)
01 | #include "mydb.h" |
02 | int main( int argc, char **argv) |
03 | { |
04 | CMyDB my; |
05 | my.initDB( "localhost" , "root" , "123456" , "studentInfo" ); |
06 | my.executeSQL( "select * from student;" ); |
07 | return 0; |
08 | } |
09 |
10 | /* |
11 | * sql语句 |
12 | *create database studentInfo |
13 | *use studentInfo |
14 | *create table student(id nchar(10) not null unique, name nchar(15) , sex nchar(4) check(sex in('男' , '女')), age nchar(3)); |
15 | */ |