• 全部
  • 系统维护与使用
  • 应用程序开发
  • 内核源代码
  • 驱动程序开发
  • CPU和硬件区
  • UNIX文化
  • Solaris
  • Power Linux
  • 问答

初学者,想用C来连接MySql,请指教!

yb611 2004-01-12 02:12:57
我的操作系统是RedHat 7.3(未完全安装,事后装的mysql-3.22.25.tar.gz),请问怎样这在个环境下用C语言来连接到MySql数据库,我只知道MySql自带了和C语言的接口,但是具体怎样使用,怎样进行编译却不清楚,请给个代码,帮帮我吧,多谢!!!
...全文
25 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kodo 2004-01-14
看example就知道了阿
回复
lovered 2004-01-14
这几天正好在做这个,可以看下mysql手册,有C的API调用
回复
mechgoukiteng 2004-01-14
看他的文档 pdf 有c接口的api函数
回复
young_wind 2004-01-14
MYSQL *
do_connect (char *host_name, char *user_name, char *password, char *db_name,unsigned int port_num, char *socket_name, unsigned int flags)
{
MYSQL *conn; /* pointer to connection handler */
conn = mysql_init (NULL);
/* allocate, initialize connection handler */
if (conn == NULL)
{
print_error (NULL, "mysql_init() failed (probably out of memory)");
return (NULL);
}
#if defined(MYSQL_VERSION_ID) && MYSQL_VERSION_ID >= 32200
/* 3.22 and up */
if (mysql_real_connect (conn, host_name, user_name, password,
db_name, port_num, socket_name, flags) == NULL)
{
print_error (conn, "mysql_real_connect() failed");
return (NULL);
}
#else /* pre-3.22 */
if (mysql_real_connect (conn, host_name, user_name, password,
port_num, socket_name, flags) == NULL)
{
print_error (conn, "mysql_real_connect() failed");
return (NULL);
}
if (db_name != NULL) /* simulate effect of db_name parameter */
{
if (mysql_select_db (conn, db_name) != 0)
{
print_error (conn, "mysql_select_db() failed");
mysql_close (conn);
return (NULL);
}
}
#endif
return (conn); /* connection is established */
}

void
do_disconnect (MYSQL *conn)
{
mysql_close (conn);
}

void
print_error (MYSQL *conn, char *message)
{
fprintf (stderr, "%s\n", message);
if (conn != NULL)
{
fprintf (stderr, "Error %u (%s)\n",
mysql_errno (conn), mysql_error (conn));
}
}
bool
processquery_noresult(MYSQL *conn, char *query)
{
MYSQL_RES *res_set;
if (mysql_query (conn, query) != 0) /* the query failed */
{
print_error (conn, "process_query() failed");
return false;
}
/* the query succeeded; determine whether or not it returns data */
res_set = mysql_store_result (conn);
if (res_set == NULL) /* no result set was returned */
{
/*
* does the lack of a result set mean that an error
* occurred or that no result set was returned?
*/
if (mysql_field_count (conn) > 0)
{
/*
* a result set was expected, but mysql_store_result()
* did not return one; this means an error occurred
*/
print_error (conn, "Problem processing result set");
return false;
}
else
{
/*
* no result set was returned; query returned no data
* (it was not a SELECT, SHOW, DESCRIBE, or EXPLAIN)
* so just report number of rows affected by query
*/
return true;
}
}
}
void
process_query(MYSQL *conn, char *query,MYSQL_RES *res_set)
{
unsigned int field_count;
if (mysql_query (conn, query) != 0) /* the query failed */
{
print_error (conn, "process_query() failed");
return;
}
/* the query succeeded; determine whether or not it returns data */
res_set = mysql_store_result (conn);
if (res_set == NULL) /* no result set was returned */
{
/*
* does the lack of a result set mean that an error
* occurred or that no result set was returned?
*/
if (mysql_field_count (conn) > 0)
{
/*
* a result set was expected, but
* mysql_store_result() did not return
* one; this means an error occurred
*/
print_error(conn, "Problem processing result set");
}
else
{
/*
* no result set was returned; query returned
* no data(it was not a SELECT, SHOW, DESCRIBE,
* or EXPLAIN),so just report number of rows
* affected by query
*/
fprintf (stderr,"%lu rows affected\n",
(unsigned long) mysql_affected_rows (conn));
}
}
else /* a result set was returned */
{
/* process rows, then free the result set */
fprintf(stderr,"%s\n","process query successful");
fprintf(stderr,"%d rows returned\n",(unsigned long)mysql_num_rows(res_set));
}
}
回复
skykong77 2004-01-12
以后会用MySQL,所以也来看看,获益不少!
回复
earthwind 2004-01-12
路过近来学习。
回复
paopaoer 2004-01-12
mysql-3.22.25.tar.gz
里面的document里有例子
回复
fierygnu 2004-01-12
看mysql的文档。编译时用-Imysql/include/path,连接时-Lmysql/lib/path,执行时保证mysql的动态库路径在/etc/ld.so.conf里或者LD_LIBRARY_PATH里。
回复
相关推荐
发帖
Linux/Unix社区
创建于2007-08-27

2.0w+

社区成员

Linux/Unix社区 应用程序开发区
申请成为版主
帖子事件
创建了帖子
2004-01-12 02:12
社区公告
暂无公告