zoukankan      html  css  js  c++  java
  • API

    API Examples

    my_ulonglong mysql_affected_rows(MYSQL *mysql)

    mysql_close(MYSQL *mysql) - almost all
    of the examples calls mysql_close()

    mysql_create_db(MYSQL *mysql, const char *db)

    mysql_change_user(MYSQL *mysql, const char *user, const char *password, const char *db)

    mysql_drop_db(MYSQL *mysql, const char *db)

    mysql_get_client_info(void)

    mysql_get_server_info(MYSQL *mysql)

    mysql_init(MYSQL *mysql)

    mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd, const char *db, unsigned int port, const char *unix_socket, unsigned int client_flag)

    mysql_real_query(MYSQL *mysql, const char *query, unsigned long length)

    mysql_select_db(MYSQL *mysql, const char *db)

    mysql_options(MYSQL *mysql, enum mysql_option option, const char *arg)

    strmov(char *dstst,char * src)

    CGI Examples

    JPEG output from JPEG
    field

    Utility Functions Examples

    strmov(char *dstst,char * src)
    Using my_ulonglong in
    printf

      

    Preface

    I see that lots of c/c++ authors are looking for MySQL API examples
    in c/c++. So I thought to make MySQL C API
    examples in this web site. And in near future I will publish this as
    book with a full web system
    as bonus.

    Since my return to my country Bangladesh, I've got so much time to
    myself and thought to contribute
    to the GNU applications that I use most ( ie, MySQL,FreeBSD,CGICC,GCC),
    while my country and government
    catches up with me. (phhhhoooooooo)

    Prior to this I have contributed the mysql_last_value(),
    released http://www.DhakaStockExchangeGame.com
    http://www.NYSEGame.com and now going to release this.

    -Jahan Saturday, September
    25, 2004 03:28:23 PM

    Aftab Jahan Subedar
    Subedar Technologies
    Subedar Baag
    Bibir Bagicha #1
    North Jatrabari
    Dhaka 1204
    Bangladesh
    http://www.DhakaStockExchangeGame.com/
    http://www.NYSEGame.com/
    http://www.CEOBangladesh.com/
    http://www.geocities.com/jahan.geo/
    - mysql_last_value() available here.
    Phone://+88027519050
    jahan@bol-online.com

    Pre-requisite

    You should know how to use C/C++, and what is MySQL.

    Basic Structure of C/C++
    Programs that uses MySQL C/C++ API

    1. All programs must include <mysql/mysql.h> as the last
      include.
    2. Define MYSQL type variable. NOTE: THERE CAN BE ONLY ONE
      MYSQL VARIABLE. (Sounds like highlander.)
    3. Initialize MYSQL type variable with mysql_init()
    4. Load any options, if required, by using mysql_options(). If you
      don't need don't call.
      You can call this fuction multiple times if you require. If you call
      this, call this before
      mysql_real_connect() and after mysql_init().
    5. Connect by calling mysql_real_connect()
    6. Call the business logic and MySQL API's
    7. Close the MYSQL type variable.

    An infra structure

    #include <mysql/mysql.h>

    return_type function_name(parameters)

    {

       MYSQL mysql;

       mysql_init(&mysql);

       mysql_options(&mysql,MYSQL_OPT_COMPRESS,0);/*call only if
    required otherwise omit*/

    mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"jahans_Dhaka_Stock_Exchange_Game");/*call
    only if required otherwise omit*/

       mysql_real_connect(....);

       /* now call other API's*/

       mysql_close(&mysql);

    }

    Compiling and
    Running

    • Compiling and Running in UNIX

       $gcc mysql_app.c -o mysql_app -I/usr/local/include
    -L/usr/local/lib/mysql -lmysqlclient

       $./mysql_app

    • Compiling and Running in Visual
      C++

       -not yet added, will be added
    later.

    Initialization Examples

    Initializing MySQL
    API

    /*------InitMySQLv1.c--------*/

    /*Variation #1*/

    /*
    Calls:
    MYSQL *mysql_init(MYSQL *mysql)
    char *mysql_get_server_info(MYSQL *mysql)
    void mysql_close(MYSQL *mysql)
    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql/mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL mysql;/* variation #1*/

      printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");

      if(mysql_init(&mysql)==NULL)
            {
                    printf("\nFailed to initate MySQL
    connection");
                    exit(1);
            }

      /*now you can call any MySQL API function you like*/

     

       mysql_close(&mysql);

      }

    /*------InitMySQLv2.c--------*/

    /*Variation #2*/

    /*
    Calls:
    MYSQL *mysql_init(MYSQL *mysql)
    char *mysql_get_server_info(MYSQL *mysql)
    void mysql_close(MYSQL *mysql)

    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql/mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL *mysql=NULL;/* variation #2*/

      printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");

      if((mysql=mysql_init(mysql))==NULL)/* variation #2*/
            {
                    printf("\nFailed to initate MySQL
    connection");
                    exit(1);
            }

      /*now you can call any MySQL API function you like*/

       mysql_close(mysql);

    }

    Checking
    Client Library Version

    /*------ClientVersion.c--------*/

    /**/

    /*
    Calls:
    MYSQL *mysql_init(MYSQL *mysql)
    char *mysql_get_client_info(void)
    void mysql_close(MYSQL *mysql)
    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql/mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

       /*Notice: it does not require MYSQL initialization*/

       printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
       printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");
       printf("MySQL Client Version is
    %s\n",mysql_get_client_info());

    }

    Checking Server
    Version

    /*------ServerVersion.c--------*/

    Calls:
    MYSQL *mysql_init(MYSQL *mysql)
    char *mysql_get_server_info(void)
    void mysql_close(MYSQL *mysql)

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql/mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL mysql;

      mysql_init(&mysql);

    mysql_real_connect(&mysql,"subedartech.sytes.net","jahan","shoja_passwd","DBDSE",0,NULL,0))

    printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");
      printf("MySQL Server Version is
    %s\n",mysql_get_server_info(&mysql));

    }

    Logging into
    MySQL Server

    /*------DBLogonv1.c--------*/

    /*Variation #1*/

    /*
    MYSQL *mysql_init(MYSQL *mysql)
    MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char
    *user, const char *passwd, const char *db,
     unsigned int port, const char *unix_socket, unsigned int client_flag)
    char *mysql_error(MYSQL *mysql)
    void mysql_close(MYSQL *mysql)

    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL mysql;

      printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");

      if(mysql_init(&mysql)==NULL)
            {
                    printf("\nFailed to initate MySQL
    connection");
                    exit(1);
            }

      /*now you can call any MySQL API function you like*/

    if
    (!mysql_real_connect(&mysql,"subedartech.sytes.net","jahan","shoja_passwd","DBDSE",0,NULL,0))

    /*variation #1*/

      {

         printf( "Failed to connect to MySQL: Error: %s\n",
    mysql_error(&mysql));

         exit(1);

       }

    printf("Logged on to database sucessfully");

    mysql_close(&mysql);

      }

    /*------DBLogonv2.c--------*/

    /*Variation #2 without database
    parameter*/

    /*
    MYSQL *mysql_init(MYSQL *mysql)
    MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char
    *user, const char *passwd, const char *db,
     unsigned int port, const char *unix_socket, unsigned int client_flag)
    char *mysql_error(MYSQL *mysql)
    int mysql_select_db(MYSQL *mysql, const char *db)

    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL mysql;

      printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");

      if(mysql_init(&mysql)==NULL)
            {
                    printf("\nFailed to initate MySQL
    connection");
                    exit(1);
            }

      /*now you can call any MySQL API function you like*/

    if
    (!mysql_real_connect(&mysql,"subedartech.sytes.net","jahan","shoja_passwd",NULL/*variation
    #2*/,0,NULL,0))
    {
        printf( "Failed to connect to MySQL: Error: %s\n",
    mysql_error(&mysql));
        exit(1);
    }

    if(mysql_select_db(&mysql,"DhakaStockExchangeGame"
    /*const char *db*/)==0)/*success*/
        printf( "Database Selected\n");
    else
        printf( "Failed to connect to Database: Error: %s\n",
    mysql_error(&mysql));

    mysql_close(&mysql);

    }

    Changing Logged
    user

    /*------ChangeUser.c--------*/

    /*
    MYSQL *mysql_init(MYSQL *mysql)
    MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char
    *user, const char *passwd, const char *db,
     unsigned int port, const char *unix_socket, unsigned int client_flag)
    char *mysql_error(MYSQL *mysql)
    void mysql_close(MYSQL *mysql)
    my_bool mysql_change_user(MYSQL *mysql, const char *user, const char
    *password, const char *db)

    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL mysql;

      printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");

      if(mysql_init(&mysql)==NULL)
            {
                    printf("\nFailed to initate MySQL
    connection");
                    exit(1);
            }

      /*now you can call any MySQL API function you like*/

    if
    (!mysql_real_connect(&mysql,"subedartech.sytes.net","jahan","shoja_passwd","DBDSE",0,NULL,0))

      {

         printf( "Failed to connect to MySQL: Error: %s\n",
    mysql_error(&mysql));

         exit(1);

       }

    printf("Logged on to database sucessfully as jahan.\n going
    to change login to web_user.");

    if( mysql_change_user(&mysql, "web_user",
    "nopassword", "DBDSE")==0)/*warning it initiates
    rollback*/
        printf("User changed\n);
    else
        printf("Error occuered:%s",mysql_error(&mysql));

    mysql_close(&mysql);

      }

    Selecting a
    Database

    /*-------SelectDB.c---------*/

    /*
    MYSQL *mysql_init(MYSQL *mysql)
    MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char
    *user, const char *passwd, const char *db,
     unsigned int port, const char *unix_socket, unsigned int client_flag)
    char *mysql_error(MYSQL *mysql)
    int mysql_select_db(MYSQL *mysql, const char *db)

    */

    #ifdef WIN32
      #include <windows.h>
      #include <winsock.h>
      #pragma warning (disable: 4514 4786)
      #pragma warning( push, 3 )
    #endif

    #include <stdio.h>
    #include "mysql.h"
    #ifndef WIN32
      #include <unistd.h>
    #endif

    int main(int argc, char **argv)
    {

      MYSQL mysql;

      printf("\n\n\tCopyright Aftab Jahan
    Subedar\n\t\thttp://www.geocities.com/jahan.geo");
      printf("\n\t\tjahan@geocities.com
    \n\t\tPhone:+88027519050\n");

      if(mysql_init(&mysql)==NULL)
            {
                    printf("\nFailed to initate MySQL
    connection");
                    exit(1);
            }

      /*now you can call any MySQL API function you like*/

    if
    (!mysql_real_connect(&mysql,"subedartech.sytes.net","jahan","shoja_passwd",NULL/*variation
    #2*/,0,NULL,0))
    {
        printf( "Failed to connect to MySQL: Error: %s\n",
    mysql_error(&mysql));
        exit(1);
    }

    if(mysql_select_db(&mysql,"DhakaStockExchangeGame"
    /*const char *db*/)==0)/*success*/
        printf( "Database Selected\n");
    else
        printf( "Failed to connect to Database: Error: %s\n",
    mysql_error(&mysql));

    mysql_close(&mysql);

    }

    Data Definition Examples

    Creating a
    Database

    /*-------CreateDB.c---------*/

    /*
    MYSQL *mysql_init(MYSQL *mysql)
    MYSQL *mysql_real_connect

    源文档 <http://www.cs.wichita.edu/~chang/lecture/cs742/program/how-mysql-c-api.html

  • 相关阅读:
    JS刷新当前页面的几种方法总结
    C#实现,C++实现,JS实现 阿拉伯数字金额转换为中文大写金额
    SQL server 生成数据字典
    jquery操作复选框(checkbox)的一些小技巧总结
    HTML中的几种空格
    C#几个经常用到的字符串截取
    关于百度地图API和jqGrid踩到的坑
    SQL常见问题积累
    JS实用小函数 数据是否合法或存在 获取当前日期时间
    vs2010 学习Silverlight学习笔记(13):数据与通信之WCF
  • 原文地址:https://www.cnblogs.com/hhdllhflower/p/2711740.html
Copyright © 2011-2022 走看看