⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysqlclienttest.c

📁 linux平台下
💻 C
字号:
/*
  Name:         MySQLClientTest
  Author1:       Kip Warner (kip@zero47.com) 
  Author2:      Modified By Tang Xu bravetang@tom.com
  Date:         24/11/03 13:15, 20/05/2005
  Description:  Example to show usage of MySQL databases from client end.
                I did not have much time. Sorry...
*/

// Includes...
#include <windows.h>
#include <MySQL/mysql.h>
#include <stdlib.h>
#include <stdio.h>

// Database name...
char        g_szHost[]          = "localhost";
UINT        g_unPort            = MYSQL_PORT;
char        g_szUserName[]      = "any";
char        g_szPassword[]      = "";
char        g_szDatabase[]      = "test";
char        g_szSQLStatement[]  = "SELECT * from table1";

// Entry point...
int main(int nArguments, char *pszArguments[])
{
    // Variables...
    MYSQL          *myDatabase      = NULL;
    MYSQL_RES	   *myResult        = NULL;
    MYSQL_FIELD	   *myField         = NULL;
    MYSQL_ROW	    myRow           = NULL;
    UINT            unRecords       = 0;
    UINT            unFields        = 0;
    UINT            unIndex         = 0;
    UINT            unFieldIndex    = 0;

    // Initialize MySQL...
    myDatabase = mysql_init(NULL);
    
        // Failed...
        if(!myDatabase)
        {
            // Alert user...
            printf("] Error: Unable to initialize MySQL API...\n");

            // Cleanup, abort, terminate...
            mysql_close(myDatabase);
            getch();
            return 0;
        }
    

    // Connect to server and check for error...
    if(mysql_real_connect(myDatabase, g_szHost, g_szUserName, g_szPassword, 
                          NULL, g_unPort, NULL, 0) < 0)
    {
        // Alert user...
        printf("] Error: Unable to connect to server...\n");
        printf("%s\n",mysql_error(myDatabase));

        // Cleanup, abort, terminate...
        mysql_close(myDatabase);
        getch();
        return 0;
    }

    // Select database in server and check for error...
    if(mysql_select_db(myDatabase, g_szDatabase) < 0)
    {
        // Alert user...
        printf("] Error: Unable to select database...\n");
        
        // Cleanup, abort, terminate...
        mysql_close(myDatabase);
        getch();
        return 0;
    }

    // Query database and check for error...
    if(mysql_query(myDatabase, g_szSQLStatement) != 0)
    {
        // Alert user...
        printf("] Error: Unable to execute query...\n");
        
        // Cleanup, abort, terminate...
        mysql_close(myDatabase);
        getch();
        return 0;
    }

    // Retrieve query result from server...
    myResult = mysql_store_result(myDatabase);
    
        // Failed...
        if(!myResult)
        {
            // Alert user...
            printf("] Error: Unable to retrieve result...\n");
            
            // Cleanup, abort, terminate...
            mysql_close(myDatabase);
            getch();
            return 0;        
        }
  
    // How many records were returned in the result set?
    
        // Calculate...
        unRecords = mysql_num_rows(myResult);
    
        // Alert user...
        printf("] Query: %d records found...\n", unRecords);
    
    // How many fields are present in a record?
        
        // Calculate...
        unFields = mysql_num_fields(myResult);
        
        // Alert user...
        printf("] Query: There are %d fields in each record...", unFields);
    
    // Output records...
    for(unIndex = 0; unIndex < unRecords; unIndex++)
    {
        // Fetch row from results...
        myRow = mysql_fetch_row(myResult);

        // Fetch fields from row...
        myField = mysql_fetch_fields(myResult);

        // Show record...
        printf("] Record: %d / %d\n", unIndex, unRecords);

        // Output all fields in this row...
        for(unFieldIndex = 0; unFieldIndex < unFields; unFieldIndex++)
        {
            // Output...
            printf("\n");
            if(unIndex ==0)            
                printf("\t%s", myField[unFieldIndex].name);
            printf("\t%s",myRow[unFieldIndex]?(char *)myRow[unFieldIndex]:"");            
        }        
    }
    
    // Free result...
    mysql_free_result(myResult);
    
    // Close server connection...
    mysql_close(myDatabase);
    myDatabase = NULL;
    
    // Alert user, exit...
    printf("\n Done, press any key to exit...\n");
    getch();
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -