安裝完MySQL以後,將安裝目錄中的include目錄下的libmysql.lib文件拷到VS2008安裝目錄中的VC\lib\下,而後在 項目-選項-c/c++-常規 中的附加包含目錄以及 連接器-常規 中的附加庫目錄中加入「c:\MySQL\include\」,而且在 連接器-輸入 中的附加依賴項內添加「libmysql.lib」,這樣便可使編譯器找到mysql.h頭文件,並可在程序中使用c語言的mysql API來操做數據庫。(若是MySQL安裝目錄中無include目錄,可到MySQL官網下載並安裝MySQL connector for C,並修改include目錄路徑)mysql
01 |
#include <Windows.h> |
02 |
#include <stdio.h> |
03 |
#include <stdlib.h> |
04 |
#include <string.h> |
05 |
#include <mysql.h> |
06 |
#include <iostream> |
07 |
using namespace std; |
08 |
09 |
int main() |
10 |
{ |
11 |
const char user[] = "root" ; //username |
12 |
const char pswd[] = "root" ; //password |
13 |
const char host[] = "localhost" ; //or"127.0.0.1" |
14 |
const char table[] = "test" ; //database |
15 |
unsigned int port = 3306; //server port |
16 |
MYSQL myCont; |
17 |
MYSQL_RES *result; |
18 |
MYSQL_ROW sql_row; |
19 |
MYSQL_FIELD *fd; |
20 |
char column[32][32]; |
21 |
int res; |
22 |
mysql_init(&myCont); |
23 |
if (mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0)) |
24 |
{ |
25 |
cout<< "connect succeed!" <<endl; |
26 |
mysql_query(&myCont, "SET NAMES GBK" ); //設置編碼格式,不然在cmd下沒法顯示中文 |
27 |
res=mysql_query(&myCont, "select * from samples" ); //查詢 |
28 |
if (!res) |
29 |
{ |
30 |
result=mysql_store_result(&myCont); //保存查詢到的數據到result |
31 |
if (result) |
32 |
{ |
33 |
int i,j; |
34 |
cout<< "number of result: " <<(unsigned long )mysql_num_rows(result)<<endl; |
35 |
for (i=0;fd=mysql_fetch_field(result);i++) //獲取列名 |
36 |
{ |
37 |
strcpy (column[i],fd->name); |
38 |
} |
39 |
j=mysql_num_fields(result); |
40 |
for (i=0;i<j;i++) |
41 |
{ |
42 |
printf ( "%s\t" ,column[i]); |
43 |
} |
44 |
printf ( "\n" ); |
45 |
while (sql_row=mysql_fetch_row(result)) //獲取具體的數據 |
46 |
{ |
47 |
for (i=0;i<j;i++) |
48 |
{ |
49 |
printf ( "%s\n" ,sql_row[i]); |
50 |
} |
51 |
printf ( "\n" ); |
52 |
} |
53 |
} |
54 |
} |
55 |
else |
56 |
{ |
57 |
cout<< "query sql failed!" <<endl; |
58 |
} |
59 |
} |
60 |
else |
61 |
{ |
62 |
cout<< "connect failed!" <<endl; |
63 |
} |
64 |
if (result!=NULL) mysql_free_result(result); //釋放結果資源 |
65 |
mysql_close(&myCont); //斷開鏈接 |
66 |
return 0; |
67 |
} |
01 |
測試環境:MySQL 5.1.35 |
02 |
安裝MySQL以後,打開MySQL Command Line Client,輸入root密碼,便可操做數據庫 |
03 |
04 |
//查看MySQL版本 |
05 |
mysql> select version(); |
06 |
07 |
//顯示全部數據庫 |
08 |
mysql> show databases; |
09 |
10 |
//使用數據庫 |
11 |
mysql> use database_name; |
12 |
13 |
//顯示全部數據表 |
14 |
mysql> show tables; |
15 |
16 |
//顯示數據表結構 |
17 |
mysql> describe table_name; |
18 |
19 |
//建立數據庫 |
20 |
mysql> create database database_name; |
21 |
22 |
//刪除數據庫 |
23 |
mysql> drop database database_name; |
24 |
25 |
//建立數據表 |
26 |
mysql> use database_name; |
27 |
mysql> create table table_name (字段名 VARCHAR(20), 字段名 CHAR (1)); |
28 |
29 |
//刪除數據表 |
30 |
mysql> drop table table_name; |
31 |
32 |
//查詢記錄 |
33 |
mysql> select * from table_name; |
34 |
35 |
//導入.sql文件 |
36 |
mysql> use database_name; |
37 |
mysql> source c:/mysql.sql |
38 |
39 |
//修改root密碼 |
40 |
mysql> UPDATE mysql.user SET password=PASSWORD( '新密碼' ) WHERE User= 'root' ; |
41 |
42 |
//退出 |
43 |
mysql> quit |