Example of cgo calling mysql database in Golang

Source: Internet
Author: User

Golang has few documents on cgo, so I definitely want to write a simple case. Here I use Mysql for the case. One is to call the functions in C, one is to use the mysql library function using golang. My environment is MAC. If you test it, please write LDFLAGS in your own environment.

Note:

Golang's cgo reference must be a separate line, that is, it must be written as follows:

Import "C"
Cgo does not have the C. NULL type, so you need to use nil

Package main

/*

# Include <stdio. h>
# Include <stdlib. h>
# Include <stdarg. h>
# Include "/usr/local/mysql/include/mysql. h"

MYSQL * mysql, * res;
MYSQL_RES * results;
MYSQL_FIELD * field;

Struct Person {
Char * name;
Int age;
Int height;
Int weight;
};
# Cgo darwin LDFLAGS:-I/usr/local/mysql/include-L/usr/local/mysql/lib-lmysqlclient

Void get_mysql_query ()
{
MYSQL_ROW row;
Mysql = mysql_init (NULL );
Mysql_options (mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client ");

Res = mysql_real_connect (mysql, "127.0.0.1", "root", "dgj99349", "myblog", 0, NULL, 0 );

If (mysql_query (res, "SELECT * FROM wp_users ")){
Fprintf (stderr, "Query failed (% s)/n", mysql_error (res ));
Exit (1 );
   }

If (! (Results = mysql_store_result (res ))){
Fprintf (stderr, "Couldn't get result from % s/n", mysql_error (res ));
Exit (1 );
    }
  
Int number = mysql_num_fields (results );
While (row = mysql_fetch_row (results ))){
For (int I = 0; I <number; I ++)
      {
If (field = mysql_fetch_field (results ))! = NULL ){
Printf ("field_name is: % s, field_value is: % s \ n", field-> name, row [I]);
         }
      }
   }
  
Mysql_free_result (results );
Mysql_close (mysql );
Mysql_server_end ();

Return;
}
*/
Import "C"
Import "fmt"
Import "unsafe"

Const (
MaxSize = 1 <20
)

Func main (){
 
  
C. puts (C. CString ("c mysql function query ...... "))
C. get_mysql_query ()

C. puts (C. CString ("c mysql queries using library functions ...... "))
// Use the C function library to initialize MYSQL *
Mysql: = C. mysql_init (nil );
// Use the database to connect to MYSQL *
C. mysql_real_connect (mysql, C. CString ("127.0.0.1"), C. CString ("root"), C. CString ("dgj99349"), C. CString ("myblog"), 0, nil, 0 );
// Query the int function
C. mysql_query (mysql, C. CString ("SELECT * FROM wp_users "))
// Query result MYSQL_RES *
Results: = C. mysql_store_result (mysql)
// The number of queried fields: unsigned int
Number: = C. mysql_num_fields (results );
// Query result char ** MYSQL_ROW
Row: = C. mysql_fetch_row (results)
// Query the field result MYSQL_FIELD *
Field: = C. mysql_fetch_field (results)
// The content length of each field is unsigned long
SQL _lengths: = C. mysql_fetch_lengths (results)

Lengths: = (* [maxSize] uint64) (unsafe. Pointer (SQL _lengths ))
   
Cfields: = (* [maxSize] C. MYSQL_FIELD) (unsafe. Pointer (field ))
   
RowPtr: = (* [maxSize] * [maxSize] byte) (unsafe. Pointer (row ))
   
For I: = 0; I <int (number); I ++ {
Length: = cfields [I]. name_length
Fname: = (* [maxSize] byte) (unsafe. Pointer (cfields [I]. name) [: length]
Fmt. Print ("field_name is:", string (fname ))
Fmt. Println (", field_value is:", string (rowPtr [I] [: lengths [I])
    }
// Release result
C. mysql_free_result (results );
// Close mysql
C. mysql_close (mysql );
C. mysql_server_end ();
  
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.