VC++6.0 访问Mysql一完整例子
2010-03-02 21:33
417 查看
VC++6.0
要进行设置
工具
/
选项
/
添加目录(
mysql.h
)所在目录
#if defined(_WIN32) || defined(_WIN64) //
为了支持
windows
平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h" //
我的机器上该文件在
D:/mysql/include
下
//
定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "select sex from
employee where employee_id = %d"
#pragma
comment(lib,"D://mysql//lib//opt//libmysql.lib")
int main(int argc, char **argv) //char
**argv
相当于
char
*argv[]
{
MYSQL mysql,*sock; //
定义数据库连接的句柄,它被用于几乎所有的
MySQL
函数
MYSQL_RES
*res; //
查询结果集,结构类型
MYSQL_FIELD *fd ; //
包含字段信息的结构
MYSQL_ROW row ; //
存放一行查询结果的字符串数组
char qbuf[160]; //
存放查询
sql
语句字符串
if (argc != 2) { //
检查输入参数
fprintf(stderr,"usage : mysql_select <userid>/n/n");
exit(1);
}
mysql_init(&mysql);
if (!(sock =
mysql_real_connect(&mysql,"localhost","root","wuqx168","mydb",0,NULL,0)))
{
fprintf(stderr,"Couldn't connect to
engine!/n%s/n/n",mysql_error(&mysql));
perror("");
exit(1);
}
sprintf(qbuf,SELECT_QUERY,atoi(argv[1]));
if(mysql_query(sock,qbuf)) {
fprintf(stderr,"Query failed (%s)/n",mysql_error(sock));
exit(1);
}
if (!(res=mysql_store_result(sock))) {
fprintf(stderr,"Couldn't get result from %s/n",
mysql_error(sock));
exit(1);
}
printf("number of fields returned:
%d/n",mysql_num_fields(res));
while (row = mysql_fetch_row(res)) {
printf("Ther employee_id #%d 's sex is: %s/n",
atoi(argv[1]),(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" :
row[0])) ;
puts( "query ok !/n" ) ;
}
mysql_free_result(res);
mysql_close(sock);
exit(0);
return 0;
}
命令行运行:
C:/Documents and Settings/l/
桌面
/
新建文件夹
(2)/Debug>mysql 200301
number of fields returned: 1
Ther userid #200301 's sex is: m
query ok !
查询数据库信息
C:/Documents and Settings/l>mysql -u
root -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 1
Server version: 5.1.34-community
MySQL Community Server (GPL)
Type 'help;' or '/h' for help. Type '/c' to
clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| base1 |
| mydb |
| mysql |
| proj |
| test |
+--------------------+
6 rows in set (0.08 sec)
mysql> use mydb;
Database changed
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| employee |
| products |
| test1 |
+----------------+
3 rows in set (0.03 sec)
mysql> describe employee;
+-------------+---------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| employee_id | char(6) | NO | PRI | NULL |
|
| name | char(8) | YES | MUL | NULL |
|
| sex | char(2) | YES | |
NULL | |
| birthday | date
| YES | | NULL
| |
+-------------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> select *from employee;
+-------------+----------+------+------------+
| employee_id | name | sex
| birthday |
+-------------+----------+------+------------+
| 200301 | zhangsan | m | 1978-05-08 |
| 200302 | lisi
| f | 1973-03-20
|
| 200303 | wangwu
| f | 1970-10-09
|
| 200304 | zhaoliu
| m | 1975-01-18 |
+-------------+----------+------+------------+
4 rows in set (0.00 sec)
mysql>
要进行设置
工具
/
选项
/
添加目录(
mysql.h
)所在目录
#if defined(_WIN32) || defined(_WIN64) //
为了支持
windows
平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h" //
我的机器上该文件在
D:/mysql/include
下
//
定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "select sex from
employee where employee_id = %d"
#pragma
comment(lib,"D://mysql//lib//opt//libmysql.lib")
int main(int argc, char **argv) //char
**argv
相当于
char
*argv[]
{
MYSQL mysql,*sock; //
定义数据库连接的句柄,它被用于几乎所有的
MySQL
函数
MYSQL_RES
*res; //
查询结果集,结构类型
MYSQL_FIELD *fd ; //
包含字段信息的结构
MYSQL_ROW row ; //
存放一行查询结果的字符串数组
char qbuf[160]; //
存放查询
sql
语句字符串
if (argc != 2) { //
检查输入参数
fprintf(stderr,"usage : mysql_select <userid>/n/n");
exit(1);
}
mysql_init(&mysql);
if (!(sock =
mysql_real_connect(&mysql,"localhost","root","wuqx168","mydb",0,NULL,0)))
{
fprintf(stderr,"Couldn't connect to
engine!/n%s/n/n",mysql_error(&mysql));
perror("");
exit(1);
}
sprintf(qbuf,SELECT_QUERY,atoi(argv[1]));
if(mysql_query(sock,qbuf)) {
fprintf(stderr,"Query failed (%s)/n",mysql_error(sock));
exit(1);
}
if (!(res=mysql_store_result(sock))) {
fprintf(stderr,"Couldn't get result from %s/n",
mysql_error(sock));
exit(1);
}
printf("number of fields returned:
%d/n",mysql_num_fields(res));
while (row = mysql_fetch_row(res)) {
printf("Ther employee_id #%d 's sex is: %s/n",
atoi(argv[1]),(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" :
row[0])) ;
puts( "query ok !/n" ) ;
}
mysql_free_result(res);
mysql_close(sock);
exit(0);
return 0;
}
命令行运行:
C:/Documents and Settings/l/
桌面
/
新建文件夹
(2)/Debug>mysql 200301
number of fields returned: 1
Ther userid #200301 's sex is: m
query ok !
查询数据库信息
C:/Documents and Settings/l>mysql -u
root -p
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 1
Server version: 5.1.34-community
MySQL Community Server (GPL)
Type 'help;' or '/h' for help. Type '/c' to
clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| base1 |
| mydb |
| mysql |
| proj |
| test |
+--------------------+
6 rows in set (0.08 sec)
mysql> use mydb;
Database changed
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| employee |
| products |
| test1 |
+----------------+
3 rows in set (0.03 sec)
mysql> describe employee;
+-------------+---------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| employee_id | char(6) | NO | PRI | NULL |
|
| name | char(8) | YES | MUL | NULL |
|
| sex | char(2) | YES | |
NULL | |
| birthday | date
| YES | | NULL
| |
+-------------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> select *from employee;
+-------------+----------+------+------------+
| employee_id | name | sex
| birthday |
+-------------+----------+------+------------+
| 200301 | zhangsan | m | 1978-05-08 |
| 200302 | lisi
| f | 1973-03-20
|
| 200303 | wangwu
| f | 1970-10-09
|
| 200304 | zhaoliu
| m | 1975-01-18 |
+-------------+----------+------+------------+
4 rows in set (0.00 sec)
mysql>
相关文章推荐
- c语言访问mysql 完整例子
- c访问mysql的完整例子
- c访问mysql的完整例子
- c访问mysql的完整例子
- c访问mysql的完整例子
- 基础 ADO.NET 访问MYSQL 与 MSSQL 数据库例子
- JavaWeb中MySQL和Oracle数据库通用分页功能实现完整例子
- Java访问Sql Server 2008 数据库样例(完整例子)
- 使用连接池访问memcached(libmemcached)的完整例子
- Java访问Sql Server 2008 数据库样例(完整例子)
- Hibernate4使用Annotation连接访问MySQL的小例子
- Hibernate4使用映射文件hbm.cml连接访问MySQL的小例子
- MySQL 5 C API 访问数据库例子程序
- C语言访问MYSQL数据库的完整的代码例子
- Java 访问MySQL的小例子
- python 访问mysql 代码例子
- Hibernate4使用Annotation连接访问MySQL的小例子
- php访问mysql例子
- 远程访问linux下的mysql,完整解决方案
- [Spark][Python]Spark 访问 mysql , 生成 dataframe 的例子: