一切盡在代碼中,代碼中也太多了if/else,可以對它進行更好的函數及至類的封裝,規范的處理好異常。
-
#include <windows.h>
-
//需要在VC的Options設置一個include路徑指向'%mysql_home%/inlude'目錄
-
#include <mysql.h>
-
//設置一個lib路徑指向'%mysql_home%/lib/opt'目錄 (mysql5.0是個目錄)
-
#pragma comment(lib,"libmysql.lib")
-
#define host_name "localhost" //數據庫服務器
-
#define db_name "test" //數據庫名
-
#define user_name "root" //用戶名
-
#define password "" //密碼
-
int
main(
int
argc,
char
* argv[]) {
-
-
char
szSqlText[500] ;
-
-
MYSQL *conn;
-
MYSQL_RES *rs;
-
MYSQL_ROW row;
//注意它的聲明 typedef char **MYSQL_ROW,字符串數組
-
BOOL
bCreate = FALSE;
-
-
conn = mysql_init(NULL);
-
if
(conn == NULL)
-
{
-
fprintf(stderr,
"mysql_init() failed (probably out of memory)\n"
);
-
exit(1);
-
}
-
-
if
(mysql_real_connect(conn,host_name,user_name,password,
-
db_name,MYSQL_PORT,NULL,0) == NULL)
-
{
-
//在MYSQL初始化之后的操作如果有錯誤,可以用mysql_errno(MYSQL*)和
-
//mysql_errer(MYSQL*) 分別獲得出錯代號和描述
-
fprintf(stderr,
"mysql_real_connect() failed:\nError %u (%s)\n"
,
-
mysql_errno(conn),mysql_error(conn));
-
exit(1);
-
}
-
-
printf(
"connect to db successful.\n"
);
-
if
(bCreate) {
-
//第一次運行創建一個表mytable
-
sprintf(szSqlText,
"create table mytable(time datetime,s1 char(6),s2 char(11),s3 int,s4 int)"
);
-
if
(mysql_query(conn,szSqlText)) {
-
printf(
"Can't create table.\n"
);
-
mysql_close(conn);
-
return
0;
-
}
-
}
-
sprintf(szSqlText,
"insert into mytable values('2000-3-10 21:01:30','Test','MySQLTest',2000,3)"
);
-
if
(mysql_query(conn,szSqlText)) {
-
printf(
"Insert values error:\nError %u (%s)\n"
,
-
mysql_errno(conn),mysql_error(conn));
-
mysql_close(conn);
-
return
0;
-
}
-
else
{
-
//insert/delete/update 語句可用mysql-affected_rows()得到受作用的行
-
printf(
"INSERT statement succeeded: %lu rows affected\n"
,
-
(unsigned
long
)mysql_affected_rows(conn));
-
}
-
-
//查詢數據
-
sprintf(szSqlText,
"select * from mytable"
);
-
-
//執行成功則返回零
-
if
(mysql_query(conn,szSqlText) != 0) {
-
mysql_close(conn);
-
return
0;
-
}
-
else
{
-
//立即從服務器返回所有行,存儲到本地,產生結果集,失敗則返回NULL
-
rs = mysql_store_result(conn);
-
-
//結果集是保留在服務器上,fetch_row時才逐行從服務器上取
-
//rs = mysql_use_result(conn);
-
//Get query result.
-
int
count = (
int
)mysql_num_rows(rs);
-
printf(
"Query: %s.\n%ld records found.\n"
,szSqlText,count);
-
//MYSQL_ROW是一個指向數值數組的指針,row[0],row[1]...row[列數-1]
-
//所有的數據類型都以字符串返回,即使是數字型,要進行串轉換
-
//NULL指針代表數據庫字段的NULL,應經常檢查列是否為NULL
-
while
((row = mysql_fetch_row(rs)) != NULL){
//返回NULL,則說明不再有行
-
for
(unsigned
int
i=0; i<mysql_num_fields(rs);i++){
-
if
(i>0)
-
fputc('\t',stdout);
-
printf(
"%s"
,row[i]!=NULL ? row[i]:
"NULL"
);
-
}
-
fputc('\n',stdout);
-
}
-
//使用完后,釋放結果集占用內存
-
mysql_free_result(rs);
-
}
-
mysql_close(conn);
-
return
0;
-
}
-
//最后,注意查詢語句中字符串的轉義 select a from t where a=''1',是要出錯的
-
//空字符轉換為'\0',這里的0 是可打印的ASCII 碼0,而不是空。
-
//反斜線、單引號和雙引號分別轉換為‘\\’、‘\'’ 和‘\"’
-
//你也可以用mysql_escape_string(to_str,from_str,from_len)轉換sql語句
輸出結果是:
connect to db successful.
INSERT statement succeeded: 1 rows affected
Query: select * from mytable.
8 records found.
2000-03-10 21:01:30 Test MySQLTest 2000 3
2000-03-10 21:01:30 Test MySQLTest 2000 3
2000-03-10 21:01:30 Test MySQLTest 2000 3
2000-03-10 21:01:30 Test MySQLTest 2000 3
2000-03-10 21:01:30 Test MySQLTest 2000 3
Press any key to continue