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