void test1()
{
MYSQL *pConn;
pConn = mysql_init(NULL);
//第2、3、4、5参数的意思分别是:服务器地址、用户名、密码、数据库名,第6个为mysql端口号(0为默认值3306)
if(!mysql_real_connect(pConn,"localhost","root","root","test",0,NULL,0))
{
printf("无法连接数据库:%s",mysql_error(pConn));
return;
}
mysql_query(pConn,"set names gbk");//防止乱码。设置和数据库的编码一致就不会乱码
//SET NAMES x 相当于 SET character_set_client = x;SET character_set_results = x;SET character_set_connection = x;
//写set character set gbk;查询不会乱码,但是参数化插入会报错。而set names gbk则都不会乱码
//mysql_real_query比mysql_query多了个参数: 字符串query的长度, 所以适合有二进制数据的query, 而mysql_query的字符串query不能包含二进制,因为它以\0为结尾
//mysql_query() 不能传二进制BLOB字段,因为二进制信息中的\0会被误判为语句结束。 mysql_real_query() 则可以。
if(mysql_query(pConn,"select * from persons"))
{
printf("查询失败:%s",mysql_error(pConn));
return;
}
//mysql_store_result是把查询结果一次性取到客户端的离线数据集,当结果比较大时耗内存。
//mysql_use_result则是查询结果放在服务器上,客户端通过指针逐行读取,节省客户端内存。但是一个MYSQL*连接同时只能有一个未关闭的mysql_use_result查询
MYSQL_RES *result = mysql_store_result(pConn);
MYSQL_ROW row;
while(row = mysql_fetch_row(result))
{
printf("%s %s\n",row[1],row[2]);
}
mysql_free_result(result);
mysql_close(pConn);
}
//获得更新行数
void test2()
{
MYSQL *pConn;
pConn = mysql_init(NULL);
if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))
{
printf("无法连接数据库:%s",mysql_error(pConn));
return;
}
if(mysql_query(pConn,"update persons set Age=Age+1"))
{
printf("执行失败:%s",mysql_error(pConn));
return;
}
printf("更新成功,共更新完成%d条",mysql_affected_rows(pConn));
mysql_close(pConn);
}
//获得自增id
void test3()
{
MYSQL *pConn;
pConn = mysql_init(NULL);
if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))
{
printf("无法连接数据库:%s",mysql_error(pConn));
return;
}
mysql_query(pConn,"set names gbk");
if(mysql_query(pConn,"insert into persons(Name,Age) values('如鹏网',100)"))
{
printf("执行insert失败%s",mysql_error(pConn));
return;
}
printf("执行insert成功,新id=%d",mysql_insert_id(pConn));
mysql_close(pConn);
}
//参数化查询
void test4()
{
MYSQL* pConn;
pConn = mysql_init(NULL);
if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0))
{
printf("数据库连接失败:%s",mysql_error(pConn));
return;
}
mysql_query(pConn,"set names gbk");
MYSQL_STMT *stmt;
MYSQL_BIND bind[2];
memset(bind,0,sizeof(bind));//把is_null、length等字段默认值设置为NULL等默认值,否则执行会报错
stmt = mysql_stmt_init(pConn);
char* insertSQL="insert into persons(Name,Age) values(?,?)";
if (mysql_stmt_prepare(stmt, insertSQL, strlen(insertSQL)))
{
fprintf(stderr, " mysql_stmt_prepare(), INSERT failed,%s\n",mysql_error(pConn));
return;
}
bind[0].buffer_type= MYSQL_TYPE_STRING;
bind[0].buffer= "黑马";
bind[0].buffer_length= strlen("黑马"); //如果设定了buffer_length,则可以不试用length
int age=3;
bind[1].buffer_type= MYSQL_TYPE_LONG;
bind[1].buffer= &age;
bind[1].buffer_length = sizeof(age);
if (mysql_stmt_bind_param(stmt, bind))
{
fprintf(stderr, " mysql_stmt_bind_param() failed %s\n", mysql_stmt_error(stmt));
return;
}
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, " mysql_stmt_execute(), failed %s\n", mysql_stmt_error(stmt));
return;
}
mysql_stmt_close(stmt);
mysql_close(pConn);
printf("参数化执行SQL结束");
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
mysql包含文件及实例.rar (50个子文件)
mysql包含文件
include
thr_cond.h 5KB
my_thread.h 5KB
thr_mutex.h 6KB
plugin_group_replication.h 6KB
keycache.h 7KB
my_getopt.h 6KB
my_dbug.h 9KB
sql_state.h 15KB
mysql_embed.h 1KB
mysql_version.h 1KB
decimal.h 5KB
plugin_keyring.h 4KB
mysql.h 28KB
mysqld_ername.h 118KB
my_config.h 12KB
mysql_com_server.h 1KB
m_ctype.h 33KB
my_compiler.h 5KB
thr_rwlock.h 6KB
typelib.h 2KB
little_endian.h 3KB
errmsg.h 4KB
my_dir.h 3KB
big_endian.h 4KB
my_byteorder.h 5KB
byte_order_generic_x86.h 2KB
plugin_validate_password.h 2KB
mysqld_error.h 44KB
my_thread_local.h 3KB
my_list.h 1KB
m_string.h 10KB
byte_order_generic.h 2KB
sslopt-longopts.h 3KB
my_sys.h 39KB
plugin.h 26KB
plugin_ftparser.h 7KB
mysql_com.h 24KB
my_command.h 2KB
binary_log_types.h 2KB
sslopt-vars.h 3KB
my_global.h 22KB
my_xml.h 3KB
mysql_time.h 2KB
sql_common.h 8KB
plugin_audit.h 17KB
my_alloc.h 2KB
sslopt-case.h 2KB
lib
libmysql.dll 4.7MB
libmysql.lib 26KB
demo.txt 4KB
共 50 条
- 1
资源评论
- 烬殇2018-06-05可以用 但是32位程序好像有些问题
- 落落西风2018-04-23可以编译通过,调用的时候,需要根据项目修改下
勤奋de码农
- 粉丝: 91
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功