[sqlite] Addition extension function failed!

2008-04-09 Thread dark0s dark0s
I have a new problem, I am attempting add extension function to sqlite3.
My program labsinf.c now build successfully, but I didn't add my extension. I 
don't understand what it is miss.
Help me please.

The program labsinf.c make inf, and after I typed ./inf, it makes dbforext.db.
Some output is below:

bash-3.1# gcc -lsqlite3 labsinf.c -o inf
bash-3.1# ./inf
bash-3.1# sqlite3 dbforext.db
SQLite version 3.5.7
Enter .help for instructions
sqlite select soundex(saverio);
SQL error: no such column: saverio
sqlite select soundex(savio);
SQL error: no such column: savio
sqlite select soundex();
SQL error: no such function: soundex
sqlite select soundex;
SQL error: no such column: soundex


The program is below:

#include stdio.h
#include stdlib.h
#include string.h
#include sqlite3.h

void soundex(sqlite3_context* ctx, int nargs, sqlite3_value** values) {

  int i,j;
  char c,r;
  int d;
  int count;
  char* str2;
  char* result;
  int dim;
  const char* str;
  char ret[4];
  
  str = sqlite3_value_text(values[0]);
  dim = strlen(str);
  for (i=0;idim;i++) str2[i] = str[i];
  for (i=0;i=dim;i++) str2[i] = toupper(str2[i]);
  for (i=0;i=dim;i++) 
switch (str[i]) {
  case 'A': case 'E': case 'I': case 'O':
  case 'U': case 'H': case 'W': case 'Y': str2[i] = '0';
}
  for (i=1;idim;i++) 
switch (str2[i]) {
  case 'B': case 'F':
  case 'P': case 'V': str2[i] = '1'; break;
  case 'C': case 'G':
  case 'J': case 'K':
  case 'Q': case 'S':
  case 'X': case 'Z': str2[i] = '2'; break;
  case 'D': case 'T': str2[i] = '3'; break;
  case 'L': str2[i] = '4'; break;
  case 'M': case 'N': str2[i] = '5'; break;
  case 'R': str2[i] = '6'; break;
}
  
  count=1;
  for (i=0;idim-1;i++)
if (str2[i] != str2[i+1]) count++;
  result = malloc(count);
  j=0;
  for (i=0;idim-1;i++)
if (str2[i] != str2[i+1]) {
  result[j]=str2[i];
  j++;
}
  for (i=0;i4;i++) printf(%c, result[i]);
  for (i=0;i4;i++) ret[i] = result[i];

  printf(\n\n);
  sqlite3_result_text(ctx,ret, 4, SQLITE_TRANSIENT);

}


int main(int argc, char* argv[]) {

  int rc;
  char* sql;
  sqlite3* db;
  sqlite3_stmt* stmt;
  const char* tail;



  rc = sqlite3_open(dbforext.db,db);
  if (rc) {
fprintf(stderr, E' impossibile aprire il file %s\n, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
  }

  sqlite3_create_function(db, soundex, 1, SQLITE_UTF8, NULL, soundex, NULL, 
NULL);
  sql = select soundex(saverio);;
  sqlite3_prepare(db, sql, strlen(sql), stmt, tail);
  if (rc != SQLITE_OK) {
fprintf(stderr, Errore SQL: %s\n, sqlite3_errmsg(db));
  }
  rc = sqlite3_step(stmt);

  sqlite3_close(db);

  return 0;

}

   
-
Inviato da Yahoo! Mail.
La casella di posta intelligente.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Addition extension function failed!

2008-04-09 Thread Ken
For the solution see:

http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions

1. you may need to re-compile sqlite with loadable extensions, depending upon 
the version of sqlite you may need to edit the makefile.

2. Create a .sqliterc file that loads your .so file
select load_extension(' /home/user/lib/labsinf.so');

3. Remove the main from your code and Compile using -shared flag to create a 
.so file that can be loaded at runtime.

HTH,
Ken


dark0s dark0s [EMAIL PROTECTED] wrote: I have a new problem, I am attempting 
add extension function to sqlite3.
My program labsinf.c now build successfully, but I didn't add my extension. I 
don't understand what it is miss.
Help me please.

The program labsinf.c make inf, and after I typed ./inf, it makes dbforext.db.
Some output is below:

bash-3.1# gcc -lsqlite3 labsinf.c -o inf
bash-3.1# ./inf
bash-3.1# sqlite3 dbforext.db
SQLite version 3.5.7
Enter .help for instructions
sqlite select soundex(saverio);
SQL error: no such column: saverio
sqlite select soundex(savio);
SQL error: no such column: savio
sqlite select soundex();
SQL error: no such function: soundex
sqlite select soundex;
SQL error: no such column: soundex


The program is below:

#include 
#include 
#include 
#include 

void soundex(sqlite3_context* ctx, int nargs, sqlite3_value** values) {

  int i,j;
  char c,r;
  int d;
  int count;
  char* str2;
  char* result;
  int dim;
  const char* str;
  char ret[4];
  
  str = sqlite3_value_text(values[0]);
  dim = strlen(str);
  for (i=0;i  for (i=0;i=dim;i++) str2[i] = toupper(str2[i]);
  for (i=0;i=dim;i++) 
switch (str[i]) {
  case 'A': case 'E': case 'I': case 'O':
  case 'U': case 'H': case 'W': case 'Y': str2[i] = '0';
}
  for (i=1;i
switch (str2[i]) {
  case 'B': case 'F':
  case 'P': case 'V': str2[i] = '1'; break;
  case 'C': case 'G':
  case 'J': case 'K':
  case 'Q': case 'S':
  case 'X': case 'Z': str2[i] = '2'; break;
  case 'D': case 'T': str2[i] = '3'; break;
  case 'L': str2[i] = '4'; break;
  case 'M': case 'N': str2[i] = '5'; break;
  case 'R': str2[i] = '6'; break;
}
  
  count=1;
  for (i=0;i
if (str2[i] != str2[i+1]) count++;
  result = malloc(count);
  j=0;
  for (i=0;i
if (str2[i] != str2[i+1]) {
  result[j]=str2[i];
  j++;
}
  for (i=0;i4;i++) printf(%c, result[i]);
  for (i=0;i4;i++) ret[i] = result[i];

  printf(\n\n);
  sqlite3_result_text(ctx,ret, 4, SQLITE_TRANSIENT);

}


int main(int argc, char* argv[]) {

  int rc;
  char* sql;
  sqlite3* db;
  sqlite3_stmt* stmt;
  const char* tail;



  rc = sqlite3_open(dbforext.db,db);
  if (rc) {
fprintf(stderr, E' impossibile aprire il file %s\n, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
  }

  sqlite3_create_function(db, soundex, 1, SQLITE_UTF8, NULL, soundex, NULL, 
NULL);
  sql = select soundex(saverio);;
  sqlite3_prepare(db, sql, strlen(sql), stmt, tail);
  if (rc != SQLITE_OK) {
fprintf(stderr, Errore SQL: %s\n, sqlite3_errmsg(db));
  }
  rc = sqlite3_step(stmt);

  sqlite3_close(db);

  return 0;

}

   
-
Inviato da Yahoo! Mail.
La casella di posta intelligente.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


dark0s dark0s [EMAIL PROTECTED] wrote: I have a new problem, I am attempting 
add extension function to sqlite3.
My program labsinf.c now build successfully, but I didn't add my extension. I 
don't understand what it is miss.
Help me please.

The program labsinf.c make inf, and after I typed ./inf, it makes dbforext.db.
Some output is below:

bash-3.1# gcc -lsqlite3 labsinf.c -o inf
bash-3.1# ./inf
bash-3.1# sqlite3 dbforext.db
SQLite version 3.5.7
Enter .help for instructions
sqlite select soundex(saverio);
SQL error: no such column: saverio
sqlite select soundex(savio);
SQL error: no such column: savio
sqlite select soundex();
SQL error: no such function: soundex
sqlite select soundex;
SQL error: no such column: soundex


The program is below:

#include 
#include 
#include 
#include 

void soundex(sqlite3_context* ctx, int nargs, sqlite3_value** values) {

  int i,j;
  char c,r;
  int d;
  int count;
  char* str2;
  char* result;
  int dim;
  const char* str;
  char ret[4];
  
  str = sqlite3_value_text(values[0]);
  dim = strlen(str);
  for (i=0;i  for (i=0;i=dim;i++) str2[i] = toupper(str2[i]);
  for (i=0;i=dim;i++) 
switch (str[i]) {
  case 'A': case 'E': case 'I': case 'O':
  case 'U': case 'H': case 'W': case 'Y': str2[i] = '0';
}
  for (i=1;i
switch (str2[i]) {
  case 'B': case 'F':
  case 'P': case 'V': str2[i] = '1'; break;
  case 'C': case 'G':
  case 'J': case 'K':
  case 'Q': case 'S':
  case 'X': case 'Z': str2[i] = '2'; break;
  case 'D': case 'T': str2[i] = '3'; break;
  case 'L': str2[i] = '4'; break;
  case 'M': case 'N': str2[i] = '5'; break;

Re: [sqlite] Addition extension function failed!

2008-04-09 Thread Dennis Cote
dark0s dark0s wrote:
 I have a new problem, I am attempting add extension function to sqlite3.
 My program labsinf.c now build successfully, but I didn't add my extension. I 
 don't understand what it is miss.
 Help me please.
 
 
 bash-3.1# gcc -lsqlite3 labsinf.c -o inf
 bash-3.1# ./inf
 bash-3.1# sqlite3 dbforext.db
 SQLite version 3.5.7
 Enter .help for instructions
 sqlite select soundex();
 SQL error: no such function: soundex

The sqlite3_create_function() call create a function that is available 
to the database connection used in the create call only. Your function 
is available in your inf program after it is created, but is no longer 
available after it exits. The sqlite3 command shell does not know 
anything about your custom function, so it can't be used there.

You need to create a loadable extension module and load that into the 
shell for the function to be available in the shell. See 
http://www.sqlite.org/cvstrac/wiki?p=LoadableExtensions and the shell's 
.load commnad for additional info.

HTH
Dennis Cote

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Addition extension function failed!

2008-04-09 Thread dark0s dark0s
Ok, but I did not understand step by step how work. In wiki there is:


#include sqlite3ext.h
SQLITE_EXTENSION_INIT1


static void halfFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  sqlite3_result_double(context, 0.5*sqlite3_value_double(argv[0]));
}


int sqlite3_extension_init(
  sqlite3 *db,
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi)
  sqlite3_create_function(db, half, 1, SQLITE_ANY, 0, halfFunc, 0, 0);
  return 0;
}


I understood that in my program I must insert sqlite3ext.h
instead sqlite3.h, but I have got a doubt:
Must I insert sqlite3_extension_init implementation,
like below?

#include sqlite3ext.h
SQLITE_EXTENSION_INIT1

int main(int argc, char* argv[]) {

  int rc;
  char* sql;
  sqlite3* db;
  sqlite3_stmt* stmt;
  const char* tail;

  rc = sqlite3_open(dbforext.db,db);
  if (rc) {
fprintf(stderr, E' impossibile aprire il file %s\n, sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
  }

static void soundex(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  ...
}


int sqlite3_extension_init(
  sqlite3 *db,
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi)
  sqlite3_create_function(db, soundex, 1, SQLITE_UTF8, NULL, soundex, NULL, 
NULL);
} 
  
  
  sql = select soundex(saverio);;
  sqlite3_prepare(db, sql, strlen(sql), stmt, tail);
  if (rc != SQLITE_OK) {
fprintf(stderr, Errore SQL: %s\n, sqlite3_errmsg(db));
  }
  rc = sqlite3_step(stmt);

  sqlite3_close(db);

  return 0;

}

Excuse for my ignorance with sqlite3, but I am newbye.

Savio

   
-
Inviato da Yahoo! Mail.
La casella di posta intelligente.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users