vc怎么连接mysql 铸铁管和pvc怎么连接

如何用VC连接 Mysql数据库#include mysql.h
main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = ""; /* 此处改成你的密码 */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:n");
while ((row = mysql_fetch_row(res)) != NULL)
{
printf("%s n", row[0]);
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
VC6.0下怎么连接Mysql数据库前提需要先安装mysqlvc怎么连接mysql,安装目录下有mysql提供的库文件:
vc6设置:
打开vc6--选择Tools(工具)--Options(选项)--Directories(文件夹)
Include包含mysql的Include目录;
Library包含mysql的Lib文件目录
或者vc怎么连接mysql你把这个libmysql.lib直接拷到你VC系统库目录下也可以
c语言怎么连接mysql数据库 代码//vc工具中添加E:\WAMP\BIN\MYSQL\MYSQL5.5.8\LIB 路径
//在工程设置-》链接》库模块中添加 libmysql.lib
#include stdio.h
#include time.h
#include string.h
#include winsock.h
#include "E:\wamp\bin\mysql\mysql5.5.8\include\mysql.h"
void main(){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server ="localhost";
char *user ="root";
char *password="";
char *database="test";
char sql[1024]="select * from chinaren";
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
if(mysql_query(conn,sql)){
fprintf(stderr,"%s\n",mysql_error(conn));
exit(1);
}
res=mysql_use_result(conn);
while((row = mysql_fetch_row(res))!=NULL){
printf("%s\n",row[2]);
}
mysql_free_result(res);
mysql_close(conn);
}
===============================
#if defined(_WIN32) || defined(_WIN64)//为了支持windows平台上vc怎么连接mysql的编译
#include windows.h
#endif
#include stdio.h
#include stdlib.h
#include "mysql.h"
//定义数据库操作的宏vc怎么连接mysql,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "show tables;"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*handle;//定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *result;//查询结果集,结构类型
MYSQL_FIELD *field ;//包含字段信息的结构
MYSQL_ROW row ;//存放一行查询结果的字符串数组
charquerysql[160];//存放查询sql语句字符串
//初始化
mysql_init(mysql);
//连接数据库
if (!(handle = mysql_real_connect(mysql,"localhost","user","pwd","dbname",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(mysql));
}
sprintf(querysql,SELECT_QUERY,atoi(argv[1]));
//查询数据库
if(mysql_query(handle,querysql)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(handle));
}
//存储结果集
if (!(result=mysql_store_result(handle))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(handle));
}
printf("number of fields returned: %d\n",mysql_num_fields(result));
//读取结果集的内容
while (row = mysql_fetch_row(result)) {
printf("table: %s\n",(((row[0]==NULL)(!strlen(row[0]))) ? "NULL" : row[0]) ) ;
}
//释放结果集
mysql_free_result(result);
//关闭数据库连接
mysql_close(handle);
system("PAUSE");
//为了兼容大部分的编译器加入此行
return 0;
}
C怎么连接MySQL数据库?//
S:~在官方给的例子基础上进行修改后的例子
#include
#include
#include
#include
#include
"mysql_connection.h"
#include
#include
#include
#include
#include
using
namespace
std;
int
main(void)
try
{
sql::Driver
*driver;
sql::Connection
*con;
sql::Statement
*stmt;
sql::ResultSet
*res;
driver
=
0;
/*
Create
a
connection
*/
driver
=
get_driver_instance();
//
获取连接驱动
con
=
driver-connect("tcp://localhost:3306",
"root",
"********");
//
ip,用户,密码
/*
Connect
to
the
MySQL
test
database
*/
con-setSchema("test");
//
选择数据库
stmt
=
con-createStatement();
//
创建查询语句
res
=
stmt-executeQuery("select
*
from
a");
//
执行查询
while
(res-next())
{
cout
"\t...
MySQL
replies:
";
/*
Access
column
data
by
alias
or
column
name
*/
cout
res-getString(1)
'\t'
res-getString(2)
endl;
//
输出1,2列
}
delete
res;
delete
stmt;
delete
con;
return
EXIT_SUCCESS;
}
catch
(sql::SQLException
e)
{
cout
"#
ERR:
SQLException
in
"
__FILE__;
cout
"("
__FUNCTION__
")
on
line
"
__LINE__
endl;
cout
"#
ERR:
"
e.what();
cout
"
(MySQL
error
code:
"
e.getErrorCode();
cout
",
SQLState:
"
e.getSQLState()
"
)"
endl;
}
运行截图:
mysql
connector/c
文档(包括例子):
mysql
connector/c
api下载:
下载以后lib和include文件夹直接copy到你vc的lib和include里
然后工程库引用里添加对mysqlcppconn.lib的引用
可能要将mysqlcppconn.dll放到c:\windows文件夹下
具体可以参考这里:
另外注意目前有个已知的因debug和release模式下string长度不同而引起的bug:
所以代码要在release下编译运行,driver-connect才能连接成功
【vc怎么连接mysql 铸铁管和pvc怎么连接】vc怎么连接mysql的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于铸铁管和pvc怎么连接、vc怎么连接mysql的信息别忘了在本站进行查找喔 。

    推荐阅读