windows平台下怎样用Turbo c2.0连接mysql数据库
2010-10-03 12:54
393 查看
http://topic.csdn.net/t/20031217/13/2573130.html
/*
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[] = "charlieface ";
char g_szPassword[] = "pudgy ";
char g_szDatabase[] = "Candy ";
char g_szSQLStatement[] = "SELECT * chocolates ";
// 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 ");
// 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( "/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;
}
/*
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[] = "charlieface ";
char g_szPassword[] = "pudgy ";
char g_szDatabase[] = "Candy ";
char g_szSQLStatement[] = "SELECT * chocolates ";
// 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 ");
// 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( "/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;
}
相关文章推荐
- 【Python】Windows平台下Python、Pydev连接Mysql数据库
- 【Python】Windows平台下Python、Pydev连接Mysql数据库
- Windows平台下Codeblocks连接Mysql数据库
- VC++在Windows平台连接MySql数据库
- Windows平台使用Navicat for MySQL通过SSH密钥认证连接远程MySQL数据库
- 64位win7平台下,使用C语言通过odbc无法连接MySQL数据库的问题。
- Ubuntu jsp平台使用JDBC来连接MySQL数据库
- qtp 怎样连接mysql数据库操作
- windows平台下如何修改mysql数据库密码
- Windows下使用dev-cpp连接mysql数据库
- C++在WINDOWS平台使用ADO连接数据库
- c#怎样连接MySQL数据库
- Windows平台eclipse连接Mysql解决方案
- windows系统下解决Qt连接MySQL数据库时出现的问题:QMYSQL driver not loaded
- 怎样在Eclipse开发环境中连接MySQL数据库
- 添加打印机的时候怎样说windows没法连接到打印机毛病为0x00000002
- Windows平台Python连接sqlite3数据库的方法分析
- MySQL在Windows上的安装及VC通过ODBC连接MySQL数据库
- 在windows下Qt5.7/C++怎么连接MySQL数据库
- 【NodeJs】Ctrl+C在Linux平台和Windows平台下的TCP连接中的不同表现