linux c++连接mysql示例
编译和连接程序
MySQL中有一个特殊的脚本,叫做mysql_config. 它会为你编译MySQL客户端,并连接到MySQL服务器提供有用的信息.你需要使用下面两个选项.
1. --libs 选项 - 连接MySQL客户端函数库所需要的库和选项.
$ mysql_config --libs
2. --cflags 选项 - 使用必要的include文件的选项等等.
$ mysql_config --cflags
你需要将上面两个选项加入到对源文件的编译命令中. 所以,要编译上面的程序,要使用下面的命令:
$ g++ -o output-file $(mysql_config --cflags) test.c $(mysql_config --libs)
执行编译后的程序:
$ ./output.file
#include <mysql.h>
#include <stdlib.h>
#include <stdio.h>
static char *server_args[] =
{
"this_program", /* this string is not used */
"--datadir=.",
"--key_buffer_size=32M"
};
static char *server_groups[] =
{
"embedded",
"server",
"this_program_SERVER",
(char *)NULL
};
int main(void)
{
if (mysql_server_init(sizeof(server_args) / sizeof(char *),
server_args, server_groups))
exit(1);
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "admin";
char *password = "metrics"; /* 此处改成你的密码 */
char *database = "test";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
//utput table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
相关文档:
C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1. setfillpatterne函数
setfillpatterne函数的功能是选择用户定义的填充模式,其用法为:void far setfillpattern(char far *upattern, int ......
Linux下扩展硬盘(一)
(2009-09-26 19:15:12)
标签:it
四、fdisk 对硬盘及分区的操作,进入fdisk 对硬盘操作阶段
我们可以对硬盘进行分区操作,前提是您把fdisk -l 弄明白了;通过fdisk -l
,我们能找出机器中所有硬盘个数及设备名称;比如上面的例子,我们会看到两个设备一个是/dev/hda ,另一 ......
设置dsn后虽然虚拟机可以上网了,但是用户名变成了bogon,而不是之前的localhost,去掉DNS后重启虚拟机就又变成了localhost
然后就可以mount本机了
还有一种方法:
hostname 显示当前主机名
hostname localhost 设置当前主机名为localhost
估计也可以解决上述不能mount的问题。 ......
linux的文件安装路径
2006年07月15日 星期六 上午 11:09
如果是别人发布的二进制包,可执行文件通常都安装到 /usr/bin 下面;如果是自己从源代码安装的,可执行文件通常都在 /usr/local/bin 下面,除非配置时指定了安装位置。
例如:
rpm -ivh xxx1.rpm
可执行文件通常都安装到 /usr/bin 下面
./configure (或者 ......