如何用TURBO C串連MYSQL

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.