http://topic.csdn.net/t/20061219/02/5239903.html
如题~
并且如何在C中建立对于表的索引
或者建立对于文本词库的索引
求一例
-----------------------------------------------------------------------
这个代码是书上的
不知道你是否能用的着
/* line.h */
#ifndef LINE_H
#define LINE_H
/* 线的属性结构 */
typedef struct { int id;
int x1, y1, x2, y2; } Line;
/* 初始化连接 */
int init_api(char *host, char *user, char *passwd, char *db);
/* 关闭连接 */
void close_api(void);
/* 取得错误信息 */
char *get_error(void);
/* 根据id查询线的信息 */
Line *get_line(int id);
/* 将一条线保存到数据库中 */
int assign_line(Line *line);
#endif
/*
说明:空间数据库实习
功能:将线保存在数据库中,并且实现查询功能
语句:
CREATE TABLE Line {
id int not null primary key,
x1 int not null,
y1 int not null,
x2 int not null,
y2 int not null
};
*/
#include <stdio.h>
#include <stdlib.h>
#include "C:/mysql/include/mysql.h "
#include "line.h "
static MYSQL *connection, mysql;
static char *error = NULL;
int init_api(char *host, char *user, char *passwd, char *db)
{
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, host, user,
passwd, db, 0, 0, 0);
if(!connection) return -1;
else return 0;
}
void close_api(void)
{
if(connection != NULL) {
mysql_close(connection);
connection = NULL;
}
}
char *get_error(void)
{
return error;
}
Line *get_line(int id)
{
char *query = "select x1, y1, x2, y2, /
from Line where id = %d ";
char sql[128];
int state;
error = (char *)NULL;
sprintf(sql, query, id);
state = mysql_query(connection, sql);
if(state != 0) {
error = mysql_error(connection);
return (Line *)NULL;
}else {
MYSQL_RES *result;
Line *line;
MYSQL_ROW row;
result = mysql_store_result(connection);
if(result == NULL) {
error = mysql_error(connection);
return (Line *)NULL;
}
line = malloc(sizeof(*line));
row = mysql_fetch_row(result);
if(!row) {
error = "查询失败 ";
return (Line *)NULL;
}
line-> x1 = atoi(row[0]);
line-> y1 = atoi(row[1]);
line-> x2 = atoi(row[2]);
line-> y2 = atoi(row[3]);
return line;
}
}
int assign_line(Line *line)
{
const char *query = "insert into Line ( id, /
x1, y1, x2, y2 ) /
values(%d, %d, %d, %d, %d) ";
char sql[128];
int state;
error = (char *)NULL;
sprintf(sql, query, line-> id,
line-> x1, line-> y1, line-> x2, line-> 2);
state = mysql_query(connection, sql);
if(state != 0) {
error = mysql_error(connection);
return -1;
}else return 0;
}
--------------------------------------------------------------
这是测试代码:
#include <stdio.h>
#include <stdlib.h>
#include "line.h "
void test_db(void)
{
static int id = 0;
Line line, *p;
while(!feof(stdin)) {
int line_id = rand()%id;
line.id = id++;
scanf( "%d%d%d%d ", &line.x1, &line.y1, &line.x2, &line.y2);
if(!assign_line(&line)) {
printf(get_error());
exit(1);
}
p = get_line(line_id);
if(p == NULL) {
printf( "%d: 查找失败/n ", line_id);
}else {
printf( "%d: 查找成功/n ", line_id);
printf( "(%d, %d)-> (%d, %d)/n ",
p-> x1, p-> y1, p-> x2, p-> y2);
free(p);
}
}
}
void main(void)
{
init_api( "mysql ", "chai ", "passwd ", "test ");
test_db();
close_api();
}
----------------------------------------------------------------------
MYSQL C API 访问程序
http://blog.csdn.net/hzhxxx/archive/2006/12/14/1443031.aspx