分类: Mysql/postgreSQL
2013-09-27 11:25:44
/*说明:
执行下面的命令:
mysql_config --libs
mysql_config --cflags
然后编译该程序,使用命令:
gcc -o 生成的目标文件 $(mysql_config --cflags) 源文件.c $(mysql_config --libs)
运行结果: ./目标文件
*/
#include
#include
#include
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "123"; /* password */
char *database = "asterisk";
conn = mysql_init(NULL);
char my_sql[80]="select realnum from a where shortnum=";
char *num="8";
strcat(my_sql,num);
/* Connect to database */
if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, my_sql))
{
fprintf(stderr, "%sn", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
//printf("MySQL Tables in mysql database: \n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s\n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}