Sqlite3添加自定义功能

时间:2015-08-12 04:59:10

标签: c gcc sqlite

我试图为SQLite3添加用C编写的自定义排名函数。代码编译,但我似乎无法在sqlite3中加载它。当我在SQLite3中执行.load './librank.so'时,我看到以下错误消息:

./librank.so.so: cannot open shared object file: No such file or directory

代码(来自http://www.sqlite.org/fts3.html#appendix_a

Rank.c

#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RANK)
#include <stdio.h>
#ifndef SQLITE_CORE
  #include "sqlite3ext.h"
  SQLITE_EXTENSION_INIT1
#else
  #include "sqlite3.h"
#endif


static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
  int *aMatchinfo;                /* Return value of matchinfo() */
  int nCol;                       /* Number of columns in the table */
  int nPhrase;                    /* Number of phrases in the query */
  int iPhrase;                    /* Current phrase */
  double score = 0.0;             /* Value to return */

  assert( sizeof(int)==4 );

  /* Check that the number of arguments passed to this function is correct.
  ** If not, jump to wrong_number_args. Set aMatchinfo to point to the array
  ** of unsigned integer values returned by FTS function matchinfo. Set
  ** nPhrase to contain the number of reportable phrases in the users full-text
  ** query, and nCol to the number of columns in the table.
  */
  if( nVal<1 ) goto wrong_number_args;
  aMatchinfo = (unsigned int *)sqlite3_value_blob(apVal[0]);
  nPhrase = aMatchinfo[0];
  nCol = aMatchinfo[1];
  if( nVal!=(1+nCol) ) goto wrong_number_args;

  /* Iterate through each phrase in the users query. */
  for(iPhrase=0; iPhrase<nPhrase; iPhrase++){
    int iCol;                     /* Current column */

    /* Now iterate through each column in the users query. For each column,
    ** increment the relevancy score by:
    **
    **   (<hit count> / <global hit count>) * <column weight>
    **
    ** aPhraseinfo[] points to the start of the data for phrase iPhrase. So
    ** the hit count and global hit counts for each column are found in 
    ** aPhraseinfo[iCol*3] and aPhraseinfo[iCol*3+1], respectively.
    */
    int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3];
    for(iCol=0; iCol<nCol; iCol++){
      int nHitCount = aPhraseinfo[3*iCol];
      int nGlobalHitCount = aPhraseinfo[3*iCol+1];
      double weight = sqlite3_value_double(apVal[iCol+1]);
      if( nHitCount>0 ){
        score += ((double)nHitCount / (double)nGlobalHitCount) * weight;
      }
    }
  }

  sqlite3_result_double(pCtx, score);
  return;

  /* Jump here if the wrong number of arguments are passed to this function */
wrong_number_args:
  sqlite3_result_error(pCtx, "wrong number of arguments to function rank()", -1);
}

int sqlite3rankInit(sqlite3 *db){
  sqlite3_create_function(db, "rank", -1, SQLITE_UTF8, 0, &rankfunc, 0, 0);
  return 0;
}

#if !SQLITE_CORE
int sqlite3_extension_init(
  sqlite3 *db, 
  char **pzErrMsg,
  const sqlite3_api_routines *pApi
){
  SQLITE_EXTENSION_INIT2(pApi)
  return sqlite3rankInit(db);
}
#endif

#endif


Compiled with: gcc -shared -fPIC -lm rank.c -o librank.so

有人可以帮我看看我做错了吗?

2 个答案:

答案 0 :(得分:1)

尝试包含断言头

diff --git a/rank_orig.c b/rank.c
index 5d51b4d..4940e1b 100644
--- a/rank_orig.c
+++ b/rank.c
@@ -1,5 +1,6 @@
 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RANK)
 #include <stdio.h>
+#include <assert.h>
 #ifndef SQLITE_CORE
   #include "sqlite3ext.h"

编译:

$ gcc -shared -fPIC -lm rank.c -o librank.so

加载

$ sqlite3 
SQLite version 3.8.10.1 2015-05-09 12:14:55
sqlite> .load ./librank.so 
sqlite> 

答案 1 :(得分:0)

错误消息显示SQLite已经附加&#34; .so&#34;到文件名。 因此,您必须指定裸库名称:

import os
from PIL import Image

yourpath = os.getcwd()
ext = ['.tiff','.tif']
size = (1618,920)
for root, dirs, files in os.walk(yourpath, topdown=False):
    for name in files:
        print(os.path.join(root, name))
        if os.path.splitext(os.path.join(root, name))[1].lower() in ext:
            if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".jpg"):
                print "A jpeg file already exists for %s" % name
            # If a jpeg is *NOT* present, create one from the tiff.
            else:
                outfile = os.path.splitext(os.path.join(root, name))[0] + ".jpg"
                try:
                    im = Image.open(os.path.join(root, name))#open image file
                    width = im.size[0]
                    height = im.size[1]

                    print "Generating jpeg for %s " % name
                    if width >= 1618 and height>=920:
                        #resize the image 
                        im.resize(size,Image.ANTIALIAS)
                        im.save(outfile, "JPEG", quality=100)
                    else: 
                        im.resize(im.size,Image.ANTIALIAS)
                        im.save(outfile, "JPEG", quality=100)
                except Exception, e:
                    print e