C API Data Structures
MYSQL
This structure represents the handler for one database connection. It is used for almost all MySQL functions. Do not try to make a copy of a MYSQL structure. There is no guarantee that such a copy will be usable.
MYSQL_RES
This structure represents the result of a query that returns rows (SELECT, SHOW, DESCRIBE, EXPLAIN). The information returned from a query is called the result set in the remainder of this section.
MYSQL_ROW
This is a type-safe representation of one row of data. It is currently implemented as an array of counted byte strings. (You cannot treat these as null-terminated strings if field values may contain binary data, because such values may contain null bytes internally.) Rows are obtained by calling mysql_fetch_row().
C API Function Overview
mysql_init(): Gets or initializes a MYSQL structure.
mysql_real_connect(): Connects to a MySQL server.
mysql_query(): Executes an SQL query specified as a null-terminated string.
mysql_use_result(): Initiates a row-by-row result set retrieval.
mysql_fetch_row(): Fetches the next row from the result set
mysql_num_fields(): Returns the number of columns in a result set.
mysql_free_result(): Frees memory used by a result set.
mysql_store_result(): Retrieves a complete result set to the client.
mysql_num_rows(): Returns the number of rows in a result set.
mysql_close(): Closes a server connection
mysql_error(): Returns the error message for the most recently invoked MySQL function.
mysql_affected_rows(): Returns the number of rows changed/deleted/inserted by the last UPDATE, DELETE, or INSERT query.
query.c
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char* argv[])
{
if(argc!=2)
{
printf("error args\n");
return -1;
}
MYSQL *conn;
MYSQL_RES *res; //保存查詢結果的指針
MYSQL_ROW row;
char* server="localhost";
char* user="root";
char* password="123";
char* database="test";//要訪問的數據庫名稱
char query[300]="select * from person where name='";
sprintf(query,"%s%s%s",query, argv[1],"'"); //SQL語句
/* strcpy(query,"select * from Person"); */
puts(query); //檢查SQL語句本身有沒有語法問題
unsigned int t,r;
conn=mysql_init(NULL); //獲得或初始化一個MYSQL結構
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)) //連接一個MySQL服務器
{
printf("Error connecting to database:%s\n",mysql_error(conn));
return -1;
}
else
{
printf("Connected...\n");
}
//將SQL語句通過conn傳遞給數據庫
t=mysql_query(conn,query); //執行指定爲一個空結尾的字符串的SQL查詢
if(t) //t>0表示不成功
{
printf("Error making query:%s\n",mysql_error(conn));
}
else
{
//用mysql_num_rows可以得到查詢的結果集有幾行
//要配合mysql_store_result使用
//結果保存在res中
//res = mysql_store_result(conn); //檢索一個完整的結果集合給客戶
//printf("mysql_num_rows=%ld\n",mysql_num_rows(res)); //返回一個結果集合中的行的數量
//不保存,每次使用直接去數據庫那邊去取
res=mysql_use_result(conn); //初始化一個一行一行的結果集合的檢索
row = mysql_fetch_row(res); //從結果集合中取得下一行
if(row == NULL)
{
printf("Didn't find data\n");
}
else
{
do{
//printf("num=%d\n",mysql_num_fields(res));//列數
for(t=0;t<mysql_num_fields(res);t++) //返回一個結果集合中的列的數量
{
printf("%8s ",row[t]);
}
printf("\n");
}while((row=mysql_fetch_row(res))!=NULL); //從結果集合中取得下一行
}
mysql_free_result(res); //釋放一個結果集合使用的內存
}
mysql_close(conn); //關閉一個服務器連接
return 0;
}
insert.c
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char* argv[])
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char* server="localhost";
char* user="root";
char* password="123";
char* database="test";
char query[200]="insert into person(name,english,science) values('bluk',60,60)";
int t,r;
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
{
printf("Error connecting to database:%s\n",mysql_error(conn));
}
else
{
printf("Connected...\n");
}
//將SQL語句通過conn傳遞給數據庫
//執行指定爲一個空結尾的字符串的SQL查詢
t=mysql_query(conn,query);
if(t) //t>0表示不成功
{
printf("Error making query:%s\n",mysql_error(conn));
}else{
printf("insert success\n");
}
mysql_close(conn); //關閉一個服務器連接
return 0;
}
update.c
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char* argv[])
{
if(argc!=2)
{
printf("error args\n");
return -1;
}
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char* server="localhost";
char* user="root";
char* password="123";
char* database="test";
char query[200]="update Person set LastName='";
sprintf(query,"%s%s%s",query,argv[1],"' where personID=5");
puts(query);
int t,r;
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
{
printf("Error connecting to database:%s\n",mysql_error(conn));
}
else
{
printf("Connected...\n");
}
t=mysql_query(conn,query);
if(t)
{
printf("Error making query:%s\n",mysql_error(conn));
}
else
{
printf("update success\n");
}
mysql_close(conn);
return 0;
}
delete.c
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int main(int argc,char* argv[])
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char* server="localhost";
char* user="root";
char* password="123";
char* database="test";
char query[200]="delete from Person where FirstName='xiong'";
int t,r;
conn=mysql_init(NULL);
if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0))
{
printf("Error connecting to database:%s\n",mysql_error(conn));
}
else
{
printf("Connected...\n");
}
t=mysql_query(conn,query);
if(t)
{
printf("Error making query:%s\n",mysql_error(conn));
}
else
{
printf("delete success,delete row=%ld\n",(long)mysql_affected_rows(conn));
}
mysql_close(conn);
return 0;
}