Howto: Connect MySQL server using C program API under Linux or UNIX


Bagaimana cara menulis program C untuk koneksi ke server database MySQL? How do I write a C program to connect MySQL database server?
database MySQL tidak mendukung program C API seperti PHP atau perl . MySQL database does support C program API just like PHP or perl .
C kode API didistribusikan dengan MySQL. The C API code is distributed with MySQL. Hal ini termasuk dalam perpustakaan mysqlclient dan memungkinkan C program untuk mengakses database. It is included in the mysqlclient library and allows C programs to access a database.
Banyak dari klien dalam distribusi source MySQL ditulis dalam C. Jika Anda mencari contoh-contoh yang menunjukkan bagaimana menggunakan API C, lihatlah di klien ini. Many of the clients in the MySQL source distribution are written in C. If you are looking for examples that demonstrate how to use the C API, take a look at these clients. Anda dapat menemukan ini pada direktori klien dalam distribusi source MySQL. You can find these in the clients directory in the MySQL source distribution.

Persyaratan Requirements

Pastikan Anda telah menginstal lingkungan pengembangan seperti gcc, pengembangan paket mysql dll Berikut ini adalah daftar merangkum daftar paket untuk mengkompilasi program: Make sure you have development environment installed such as gcc, mysql development package etc. Following is the list summarize the list of packages to compile program:
  • mysql: program klien MySQL dan shared library mysql : MySQL client programs and shared library
  • mysqlclient: Backlevel MySQL shared libraries (libs lama) mysqlclient : Backlevel MySQL shared libraries (old libs)
  • mysql-devel: File untuk pengembangan aplikasi MySQL (harus ada) mysql-devel : Files for development of MySQL applications (a must have)
  • mysql-server: MySQL server sendiri mysql-server : Mysql server itself
  • gcc, membuat dan lain pembangunan libs: GNU C compiler gcc, make and other development libs : GNU C compiler

Contoh Program C Sample C Program

Mengikuti instruksi harus bekerja pada setiap distro Linux atau komputer UNIX. Following instructions should work on any Linux distro or UNIX computer. Berikut adalah program kecil yang menghubungkan ke mysql dan daftar tabel dari database mysql server (. link download ): Here is the small program that connects to mysql server and list tables from mysql database.( download link ):
/ * Sederhana program C yang terhubung ke server Database MySQL * / /* Simple C program that connects to MySQL Database server*/ 
 # Include  #include  
 # Include  #include  
 main () { main () { 
    MYSQL * conn; MYSQL * conn ; 
    * MYSQL_RES res; MYSQL_RES * res ; 
    MYSQL_ROW baris; MYSQL_ROW row ; 

    Char * server = "localhost"; char * server = "localhost" ; 
    Char * user = "root"; char * user = "root" ; 
    char * password = "PASSWORD"; / * set me * pertama / char * password = "PASSWORD" ; /* set me first */ 
    Char * database = "mysql"; char * database = "mysql" ; 

    conn = mysql_init (NULL); conn = mysql_init ( NULL ); 

    / * Hubungkan ke database * / /* Connect to database */ 
    if (server! mysql_real_connect (conn,, if (! mysql_real_connect ( conn , server , 
          user, password, database, 0, NULL, 0)) { user , password , database , 0 , NULL , 0 )) { 
       fprintf (stderr, "% s \ n", mysql_error (conn)); fprintf ( stderr , "%s \n " , mysql_error ( conn )); 
       exit (1); exit ( 1 ); 
    } } 

    / * Kirim * query SQL / /* send SQL query */ 
    if (mysql_query (conn, "tabel menunjukkan")) { if ( mysql_query ( conn , "show tables" )) { 
       fprintf (stderr, "% s \ n", mysql_error (conn)); fprintf ( stderr , "%s \n " , mysql_error ( conn )); 
       exit (1); exit ( 1 ); 
    } } 

    res = mysql_use_result (conn); res = mysql_use_result ( conn ); 

    / * Nama tabel output * / /* output table name */ 
    printf ("Tabel pada database MySQL mysql: \ n"); printf ( "MySQL Tables in mysql database: \n " ); 
    while ((baris = mysql_fetch_row (res))! = NULL) while (( row = mysql_fetch_row ( res )) != NULL ) 
       printf ("% s \ n", baris [0]); printf ( "%s \n " , row [ 0 ]); 

    / * Tutup koneksi * / /* close connection */ 
    mysql_free_result (res); mysql_free_result ( res ); 
    mysql_close (conn); mysql_close ( conn ); 
 } } 
 

Bagaimana cara mengkompilasi dan link program terhadap libs MySQL? How do I compile and link program against MySQL libs?

MySQL dilengkapi dengan script khusus yang disebut mysql_config. MySQL comes with a special script called mysql_config. Ini memberikan Anda informasi yang berguna untuk mengkompilasi Anda klien MySQL dan menghubungkannya ke server database MySQL. It provides you with useful information for compiling your MySQL client and connecting it to MySQL database server. Anda perlu menggunakan berikut dua pilihan. You need to use following two options.
Pass - libs pilihan - Perpustakaan dan opsi yang diperlukan untuk menghubungkan dengan perpustakaan klien MySQL. Pass --libs option - Libraries and options required to link with the MySQL client library.
$ mysql_config --libs
Output: Output:
-L/usr/lib64/mysql-Lmysqlclient-Lz-lcrypt-lnsl-lm -L/usr/lib64-lssl-lcrypto -L/usr/lib64/mysql -lmysqlclient -lz -lcrypt -lnsl -lm -L/usr/lib64 -lssl -lcrypto 
Pass - CFLAGS opsi - Kompilator flag untuk menemukan menyertakan file dan flag-flag compiler kritis dan mendefinisikan digunakan ketika kompilasi perpustakaan libmysqlclient. Pass --cflags option - Compiler flags to find include files and critical compiler flags and defines used when compiling the libmysqlclient library.
$ mysql_config --cflags
Output: Output:
-I/usr/include/mysql-G-pipe-m64-D_GNU_SOURCE-D_FILE_OFFSET_BITS = 64-D_LARGEFILE_SOURCE-fno-ketat-aliasing -I/usr/include/mysql -g -pipe -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing 
Anda harus lulus di atas pilihan untuk gcc compiler yaitu GNU C. You need to pass above option to GNU C compiler ie gcc. Jadi untuk mengkompilasi program di atas, masukkan: So to compile above program, enter:
$ gcc -o output-file $(mysql_config --cflags) mysql-c-api.c $(mysql_config --libs)
Sekarang melaksanakan program: Now execute program:
$ ./output-file
Output: Output:
Tabel MySQL di database mysql: MySQL Tables in mysql database:
columns_priv columns_priv
db db
func func
help_category help_category
help_keyword help_keyword
help_relation help_relation
help_topic help_topic
tuan rumah host
tables_priv tables_priv
time_zone time_zone
time_zone_leap_second time_zone_leap_second
time_zone_name time_zone_name
time_zone_transition time_zone_transition
time_zone_transition_type time_zone_transition_type
pengguna user 



http://www.cyberciti.biz/tips/linux-unix-connect-mysql-c-api-program.html

Share this

Related Posts

Previous
Next Post »