Write a test program when using C in Linux to operate MySQL database
Int main (void) <br/>{< br/> MySQL m_mysql; <br/> mysql_init (& m_mysql ); </P> <p> const char plocalhost [] = "localhost"; <br/> const char puser [] = "root "; <br/> const char ppass [] = "123456"; <br/> const char PDB [] = "httpcontrol "; </P> <p> If (mysql_real_connect (& m_mysql, plocalhost, puser, ppass, PDB, 0, null, 0) = NULL) <br/> cout <"Connect failed" <Endl; <br/> else <br/> cout <"Connect success" <Endl; </P> <p> mysql_close (& m_mysql); <br/> return 0; <br/>}< br/>
Use valgrind to detect memory leakage after compilation
[Root @ localhost string] # valgrind -- tool = memcheck -- leak-check = full./MySQL
==10352 = memcheck, a memory error detector
= 10352 = copyright (c) 2002-2009, and gnu gpl 'd, by Julian Seward et al.
= 10352 = Using Valgrind-3.5.0 and libvex; rerun with-H for copyright info
==10352 = command:./MySQL
= 10352 =
Connect success
= 10352 =
==10352 === heap summary:
= 10352 = in use at Exit: 61,336 bytes in 16 blocks
= 10352 = total heap usage: 118 allocs, 102 frees, 115,929 bytes allocated
= 10352 =
= 10352 = 24,528 bytes in 6 blocks are possibly lost in loss record 4 of 5
= 10352 = at 0x4005903: malloc (vg_replace_malloc.c: 195)
= 10352 = by 0x30dd75: my_once_alloc (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x30e59c :??? (In/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x30ef96 :??? (In/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x30f111: get_charset_by_csname (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x32c3d2: mysql_init_character_set (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x32dabb: mysql_real_connect (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x8048a76: Main (Str. cpp: 32)
= 10352 =
= 10352 = 28,616 bytes in 7 blocks are possibly lost in loss record 5 of 5
= 10352 = at 0x4005903: malloc (vg_replace_malloc.c: 195)
= 10352 = by 0x30dd75: my_once_alloc (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x30e57a :??? (In/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x30ef96 :??? (In/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x30f111: get_charset_by_csname (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x32c3d2: mysql_init_character_set (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x32dabb: mysql_real_connect (in/usr/lib/MySQL/libmysqlclient. so.15.0.0)
= 10352 = by 0x8048a76: Main (Str. cpp: 32)
= 10352 =
= 10352 = leak summary:
= 10352 = definitely lost: 0 bytes in 0 blocks
==10352 = indirectly lost: 0 bytes in 0 blocks
==10352 = possibly lost: 53,144 bytes in 13 blocks
= 10352 = still reachable: 8,192 bytes in 3 blocks
= 10352 = suppressed: 0 bytes in 0 blocks
= 10352 = reachable blocks (those to which a pointer was found) are not shown.
= 10352 = to see them, Rerun with: -- leak-check = full -- show-reachable = Yes
= 10352 =
= 10352 = for counts of detected and suppressed errors, Rerun with:-V
= 10352 = Error Summary: 2 errors from 2 contexts (suppressed: 3
I found it online.
Http://dev.mysql.com/doc/refman/5.0/en/mysql-library-end.html
Simply put, it is used after the database is disconnected.
Mysql_library_end ()
Close the database used by MySQL
That is, in mysql_close (...)
Add later
Mysql_library_end ();
OK.
To add another statement, it is best to use the MySQL API
Call mysql_library_init (....)