1. [代码][C/C++]代码
#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>
int main()
{
MYSQL mysql;//连接数据库的变量
MYSQL_RES *res;//存放查询结果的变量
MYSQL_ROW row;
char *query;
int t,r;
char *server= "localhost";
char *user = "root";
char *password="test";
char *database = "test";
mysql_init(&mysql); //连接数据库前,必须调用此函数初始化变量
/**
* MYSQL *mysql_init(MYSQL *mysql)
* 传入MYSQL类型的指针
* 返回一个被初始化的MYSQL*句柄
* 在内存不够的情况下,返回NULL
*/
if(!mysql_real_connect(&mysql,server,user,password,database,0/*TCP IP端口*/,NULL/*Unix socket连接类型*/,0/*运行成ODBC数据库的标记*/))
{
printf("Error connection to database:%s/n",mysql_error(&mysql));
}else
printf("Connected.../n");
query = "select * from t1";
printf("query execute:%s/n",query);
t = mysql_real_query(&mysql,query,(unsigned int) strlen(query));
/**
*
* int STDCALL mysql_real_query(MYSQL *mysql,const char *q, unsigned int length);
* 执行任何SQL查询语句进行查询。
* mysql是我们前面用过的变量,q是SQL查询语句,length是这个查询语句的长度。
*/
if(t)
{
printf("Error making query:%s/n",mysql_error(&mysql));
}else
printf("[%s] made.../n",query);
res = mysql_store_result(&mysql);
/**
* mysql_store_result(MYSQL *mysql)
* 立刻检索全部结果
*
* mysql_use_result(MYSQL *mysql)
* 初始化一个一行一行地结果集合的检索
*/
while(row = mysql_fetch_row(res))
{
/**
* MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);
* 检索行
*/
for(t=0;t<=mysql_num_fields(res);t++)
{
printf("%s",row[t]);
}
printf("/n");
}
printf("mysql_free_result.../n");
mysql_free_result(res);
/**
* mysql_free_result()
* 释放由它使用的内存。
*/
sleep(1);
//execute the insert query
query = "insert into t1(id,name) values(3,'kunp')";
t = mysql_real_query(&mysql,query,(unsigned int) strlen(query));
if(t)http://www.huiyi8.com/hunsha/hanshi/
{韩式婚纱照片
printf("Error making query:%s/n",query);
mysql_error(&mysql);
}
else printf("[%s] made.../n",query);
mysql_close(&mysql);
/**
* void mysql_close(MYSQL *mysql)
* 传入MYSQL类型的指针
* 功能,关闭一个服务器连接,并释放与连接相关的内存i
*/
return 1;
}
//编译:gcc testsql.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -o testsql
2. [代码][C/C++]代码
#include <stdlib.h>
#include <stdio.h>
#include <mysql/mysql.h>
int main(void){
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
MYSQL *conn_ptr;
int result;//query result
int iTableRow,iTableCol,i,j;
conn_ptr = mysql_init(NULL);
if (!conn_ptr)
{
fprintf(stderr,"mysql_init failed!
");
return (-1);
}
conn_ptr = mysql_real_connect(conn_ptr,"127.0.0.1","root","123456","mysql",3306,0,NULL);
if (conn_ptr)
{
printf ("connection succeed!
");
}
else{
printf ("connection failed!
");
return (-2);
}
result = mysql_query(conn_ptr,"select * from test.tbl_user");
if(result){
printf(stderr,"select error %d: %s !
",mysql_errno(conn_ptr),mysql_error(conn_ptr));//
return EXIT_FAILURE;
}
res_ptr = mysql_store_result(conn_ptr);//集合
if( res_ptr )
{
iTableRow = mysql_num_rows(res_ptr);//行
iTableCol = mysql_num_fields(res_ptr);//列
for(i=0; i<iTableRow; i++)
{
sqlrow = mysql_fetch_row(res_ptr);
for(j=0; j<iTableCol; j++)
{
printf("%-8s ",sqlrow[j]);//字符串向左靠,右补空格
}
printf("
");
}
mysql_free_result(res_ptr);//完成对数据的所有操作后,调用此函数来让MySQL库清理它分配的对象
mysql_close(conn_ptr);
printf ("connection close!
");
return 0;
}
编译:gcc -o mydbcon -I /usr/include/mysql/ -L /usr/lib/mysql -lmysqlclient mysql.c
运行:./mydbcon