LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3947|回复: 4

c语言实现Mysql数据库访问

[复制链接]
发表于 2005-5-20 11:19:16 | 显示全部楼层 |阅读模式
  1. /*
  2.   Name:         MySQLClientTest
  3.   Author1:       Kip Warner (kip@zero47.com)
  4.   Author2:      Modified By Tang Xu [email]tangxu.sh@gmail.com[/email]
  5.   Date:         24/11/03 13:15, 20/05/2005
  6.   Description:  Example to show usage of MySQL databases from client end.
  7.                 I did not have much time. Sorry...
  8. */
  9. // Includes...
  10. #include <MySQL/mysql.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. // Database name...
  14. char        g_szHost[]          = "localhost";
  15. UINT        g_unPort            = MYSQL_PORT;
  16. char        g_szUserName[]      = "any";
  17. char        g_szPassword[]      = "";
  18. char        g_szDatabase[]      = "test";
  19. char        g_szSQLStatement[]  = "SELECT * from table1";
  20. // Entry point...
  21. int main(int nArguments, char *pszArguments[])
  22. {
  23.     // Variables...
  24.     MYSQL          *myDatabase      = NULL;
  25.     MYSQL_RES           *myResult        = NULL;
  26.     MYSQL_FIELD           *myField         = NULL;
  27.     MYSQL_ROW            myRow           = NULL;
  28.     UINT            unRecords       = 0;
  29.     UINT            unFields        = 0;
  30.     UINT            unIndex         = 0;
  31.     UINT            unFieldIndex    = 0;
  32.     // Initialize MySQL...
  33.     myDatabase = mysql_init(NULL);
  34.    
  35.         // Failed...
  36.         if(!myDatabase)
  37.         {
  38.             // Alert user...
  39.             printf("] Error: Unable to initialize MySQL API...\n");
  40.             // Cleanup, abort, terminate...
  41.             mysql_close(myDatabase);
  42.             getch();
  43.             return 0;
  44.         }
  45.    
  46.     // Connect to server and check for error...
  47.     if(mysql_real_connect(myDatabase, g_szHost, g_szUserName, g_szPassword,
  48.                           NULL, g_unPort, NULL, 0) < 0)
  49.     {
  50.         // Alert user...
  51.         printf("] Error: Unable to connect to server...\n");
  52.         printf("%s\n",mysql_error(myDatabase));
  53.         // Cleanup, abort, terminate...
  54.         mysql_close(myDatabase);
  55.         getch();
  56.         return 0;
  57.     }
  58.     // Select database in server and check for error...
  59.     if(mysql_select_db(myDatabase, g_szDatabase) < 0)
  60.     {
  61.         // Alert user...
  62.         printf("] Error: Unable to select database...\n");
  63.         
  64.         // Cleanup, abort, terminate...
  65.         mysql_close(myDatabase);
  66.         getch();
  67.         return 0;
  68.     }
  69.     // Query database and check for error...
  70.     if(mysql_query(myDatabase, g_szSQLStatement) != 0)
  71.     {
  72.         // Alert user...
  73.         printf("] Error: Unable to execute query...\n");
  74.         
  75.         // Cleanup, abort, terminate...
  76.         mysql_close(myDatabase);
  77.         getch();
  78.         return 0;
  79.     }
  80.     // Retrieve query result from server...
  81.     myResult = mysql_store_result(myDatabase);
  82.    
  83.         // Failed...
  84.         if(!myResult)
  85.         {
  86.             // Alert user...
  87.             printf("] Error: Unable to retrieve result...\n");
  88.             
  89.             // Cleanup, abort, terminate...
  90.             mysql_close(myDatabase);
  91.             getch();
  92.             return 0;        
  93.         }
  94.   
  95.     // How many records were returned in the result set?
  96.    
  97.         // Calculate...
  98.         unRecords = mysql_num_rows(myResult);
  99.    
  100.         // Alert user...
  101.         printf("] Query: %d records found...\n", unRecords);
  102.    
  103.     // How many fields are present in a record?
  104.         
  105.         // Calculate...
  106.         unFields = mysql_num_fields(myResult);
  107.         
  108.         // Alert user...
  109.         printf("] Query: There are %d fields in each record...", unFields);
  110.    
  111.     // Output records...
  112.     for(unIndex = 0; unIndex < unRecords; unIndex++)
  113.     {
  114.         // Fetch row from results...
  115.         myRow = mysql_fetch_row(myResult);
  116.         // Fetch fields from row...
  117.         myField = mysql_fetch_fields(myResult);
  118.         // Show record...
  119.         printf("] Record: %d / %d\n", unIndex, unRecords);
  120.         // Output all fields in this row...
  121.         for(unFieldIndex = 0; unFieldIndex < unFields; unFieldIndex++)
  122.         {
  123.             // Output...
  124.             printf("\n");
  125.             if(unIndex ==0)            
  126.                 printf("\t%s", myField[unFieldIndex].name);
  127.             printf("\t%s",myRow[unFieldIndex]?(char *)myRow[unFieldIndex]:"");            
  128.         }        
  129.     }
  130.    
  131.     // Free result...
  132.     mysql_free_result(myResult);
  133.    
  134.     // Close server connection...
  135.     mysql_close(myDatabase);
  136.     myDatabase = NULL;
  137.    
  138.     // Alert user, exit...
  139.     printf("\n Done, press any key to exit...\n");
  140.     getch();
  141.     return 0;
  142. }
复制代码
 楼主| 发表于 2005-5-20 11:20:47 | 显示全部楼层
原先的作者不知什么居心,竟然没提供显示每个字段内容的代码。在这里我加上了。
从这个例子可以看出,mysql返回的所有字段值均转换为了字符串。
回复 支持 反对

使用道具 举报

发表于 2005-5-21 12:35:08 | 显示全部楼层
謝謝, 對我在c及mysql的學習上加了一頁呢. thanks.
回复 支持 反对

使用道具 举报

发表于 2007-7-23 13:45:44 | 显示全部楼层
楼主的代码里面,是在windows环境吗?
为何还include "windws.h"
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-7-24 09:31:32 | 显示全部楼层
Post by freesu
楼主的代码里面,是在windows环境吗?
为何还include "windws.h"

是我的疏漏,现在已经删掉了windows.h
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表