用Dev-CPP来测试MySQL的C API程序,执行到第三步,不行,请大虾指教!谢谢!

明月登楼 2005-08-31 10:23:14
/*
Name: MySQLClientTest
Author: Kip Warner (kip@zero47.com)
Date: 24/11/03 13:15
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>
#include <conio.h>

// Database name...
char g_szHost[] = "localhost";
UINT g_unPort = MYSQL_PORT;
char g_szUserName[] = "root";
char g_szPassword[] = "caoxin";
char g_szDatabase[] = "mydata";
char g_szSQLStatement[] = "SELECT * FROM `yuangong` WHERE 1";

// 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);
int temp=0;
// 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");

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

// Select database in server and check for error...
if((temp=mysql_select_db(myDatabase, g_szDatabase)) != 0) //执行到此步就行了,就提出如下的错误,请大虾赐教!
{
// Alert user...
printf("] Error: Unable to select *%d* database...\n",temp);

// 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("\t%s", myField[unFieldIndex].name);
}
}

// Free result...
mysql_free_result(myResult);

// Close server connection...
mysql_close(myDatabase);
myDatabase = NULL;

// Alert user, exit...
printf("] Done, press any key to exit...\n");
getch();
return 0;
}
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhujiujun 2005-11-27
  • 打赏
  • 举报
回复
楼主,能不能把正确的结果贴出来啊。我也准备做这方面的东西~~~
谢谢!
Jagen在路上 2005-09-25
  • 打赏
  • 举报
回复
大小写问题吧,哈哈,我来接分的。
foochow 2005-09-24
  • 打赏
  • 举报
回复
JFJFJF,多给偶点啊,要升级了,嘿嘿
fiftymetre 2005-09-24
  • 打赏
  • 举报
回复
恭喜楼主,偶是来蹭分的~~~
明月登楼 2005-09-24
  • 打赏
  • 举报
回复
哈哈,昨天我已解决此难题!欢喜中。。。。。
明月登楼 2005-09-01
  • 打赏
  • 举报
回复
怎么没有人回答呀?
明月登楼 2005-08-31
  • 打赏
  • 举报
回复
既然,前面两个条件都过了,到最后选择数据库却出错了,我是确实不明白问题出哪里了?郁闷中...

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧