c++连接数据库 在vc6.0
配置相关环境
我的mysql安装路径为E:\mysql-5.5.28-win32所以要在VC中设置include路径和lib的路径。
添加MySql的include目录到VC工作台中Project->Settings->C/C++->Category->Preprocessor->Additional include directories中添加 E:\mysql-5.5.28-win32\include。
添加lib的路径:Tools->Options->Directories中选择Library files,然后添加lib的目录:E:\mysql-5.5.28-win32\LIB然后在Project->Settings->Link中,
添加 libmysql.lib 到Object/libray modules中
mysql相关知识点
#include <windows.h>
#include <iostream>
#include
<mysql.h>
#include <stdio.h>
#include
<string.h>
using namespace std;
int main( int argc, char * argv[]
)
{
//connection params
char *host = "localhost";
char
*user = "root";
char *pass = "";
char *db = "test";
MYSQL
*mysql;
MYSQL_RES *res; //结果集
MYSQL_FIELD * fd ;
MYSQL_ROW
row;
char szSqlText[500]="";
int i;
int
tcreate=0;//表存在为0,表不存在为1
if ( (mysql = mysql_init((MYSQL*) 0))
//初始化数据结构
&& mysql_real_connect( mysql, host, user, pass, db, 0,
NULL, 0 ) )
{
mysql_set_character_set(mysql,"utf8");
//connection character set
//wait for posibility to check
system/mysql mysqlets
if(tcreate) ///////
{
sprintf(szSqlText, //构造SQL语句
"create table mytable"
//新建一张表
"(time datetime, s1 char(6), "
"s2 char(11), s3
int, s4 int)");
if (mysql_query( mysql, szSqlText))
//执行SQL语句
{//执行SQL语句出错
cout<<"Can‘t create
table:"<<mysql_error(mysql) << endl;
mysql_close( mysql ) ;
return FALSE ;
}
cout<<"建表成功!"<<endl;
}
sprintf(szSqlText,"insert into mytable
values(now(),‘Test‘,‘MySQLTest‘,2000,15)");
if (mysql_query( mysql,
szSqlText))
{//执行SQL语句出错
cout<<"Can‘t insert data to
table执行sql出错"<<mysql_error(mysql) << endl;
mysql_close( mysql) ;
return FALSE ;
}
sprintf(szSqlText, "select * from mytable");
if (mysql_query( mysql,
szSqlText))
//进行数据检索
{
//执行SQL语句出错
mysql_close( mysql ) ;
return FALSE ;
}
else
{
res = mysql_store_result( mysql)
;
//取得查询结果
i = (int) mysql_num_rows( res ) ;
//取得有效记录数
cout<<"Query:
"<<szSqlText<<"\n"<<i<<" records
found:"<<endl;
for ( i = 0 ; fd = mysql_fetch_field( res )
; i++ )
cout<<fd->name<<"\t";
cout<<endl;
//输出各字段名
while(row = mysql_fetch_row( res
))
//依次读取各条记录
cout<<row[0]<<"\t"<<row[1]<<"\t"<<row[2]<<"\t"<<row[3]<<"\t"<<row[4]<<endl;
mysql_free_result( res ) ;
}
}
else
{//连接数据库出错
cout<<"Can‘t connect to the mysql server不能连接
"<<mysql_error(mysql) << endl;
mysql_close( mysql )
;
return FALSE ;
}
mysql_close( mysql ) ;
return TRUE ;
}
摘自:http://bbs.csdn.net/topics/230075782
mysql相关知识点
/查看MySQL版本
mysql> select version();
//显示所有数据库
mysql> show databases;
//使用数据库
mysql> use database_name;
//显示所有数据表
mysql> show tables;
//显示数据表结构
mysql> describe table_name;
//创建数据库
mysql> create database database_name;
//删除数据库
mysql> drop database database_name;
//创建数据表
mysql> use database_name;
mysql> create table table_name (字段名 VARCHAR(20), 字段名 CHAR(1));
//删除数据表
mysql> drop table table_name;
//查询记录
mysql> select * from table_name;
//导入.sql文件
mysql> use database_name;
mysql> source c:/mysql.sql
//修改root密码
mysql> UPDATE mysql.user SET password=PASSWORD(‘新密码‘) WHERE User=‘root‘;
//退出
mysql> quit