在程序设计中,句柄是一种特殊的智能指针 。当一个应用程序要引用其他系统(如数据库、操作系统)所管理的内存块或对象时,就要使用句柄。
句柄与普通指针的区别在于,指针包含的是引用对象的内存地址,而句柄则是由系统所管理的引用标识,该标识可以被系统重新定位到一个内存地址上。这种间接访问对象的模式增强了系统对引用对象的控制。
enum选项 | 实际参数类型 | 说明 |
MySQL_OPT_CONNECT_TIMEOUT | const unsigned int * | 连接超时之前的等待秒数 |
MySQL_OPT_COMPRESS | 使用NULL | 网络连接中使用压缩机制 |
MySQL_INIT_COMMAND | const char * | 每次连接建立后发送的命令 |
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
|
#include
<stdlib.h> #include
<stdio.h> #include
"mysql.h" int main( int argc, char **argv) { MYSQL
* conn_ptr; conn_ptr
= mysql_init(NULL); if (!conn_ptr) { perror ( "mysql_init
failed\n" ); exit (1); } conn_ptr
= mysql_real_connect(conn_ptr, "localhost" , "root" , "yao" , "test" ,0,NULL); if (conn_ptr) { printf ( "connection
success\n" ); } else { printf ( "connection
failed\n" ); } mysql_close(conn_ptr); return 0; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include
<stdlib.h> #include
<stdio.h> #include
"mysql.h" int main( int argc, char **argv) { MYSQL
mysql_conn; mysql_init(&mysql_conn); if (mysql_real_connect(&mysql_conn, "localhost" , "root" , "dfdsfjfd" , "test" ,0,NULL)) { printf ( "connection
success\n" ); mysql_close(&mysql_conn); } else { perror ( "connection
failed\n" ); if (mysql_errno(&mysql_conn)) { fprintf (stderr, "connection
error %d:%s\n" ,mysql_errno(&mysql_conn),mysql_error(&mysql_conn)); } } return 0; } |
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
|
#include
<stdlib.h> #include
<stdio.h> #include
"mysql.h" int main( int argc, char **argv) { MYSQL
mysql_conn; int res; mysql_init(&mysql_conn); if (mysql_real_connect(&mysql_conn, "localhost" , "root" , "yao" , "test" ,0,NULL)) { printf ( "connection
success\n" ); res
= mysql_query(&mysql_conn, "insert
into child(childid,age,name) values (1,12,'bing')" ); if (!res) { printf ( "inserted
%lu rows\n" ,(unsigned long )mysql_affected_rows(&mysql_conn)); } else { fprintf (stderr, "insert
error %d:%s" ,mysql_errno(&mysql_conn),mysql_error(&mysql_conn)); } mysql_close(&mysql_conn); } else { fprintf (stderr, "connection
failed\n" ); if (mysql_errno(&mysql_conn)) { fprintf (stderr, "Connection
error %d:%s\n" ,mysql_errno(&mysql_conn),mysql_error(&mysql_conn)); } } return 0; } |