c_cpp sqlite3的运行时可加载扩展添加extended_errorcode()函数(但失败了。不要使用它!)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp sqlite3的运行时可加载扩展添加extended_errorcode()函数(但失败了。不要使用它!)相关的知识,希望对你有一定的参考价值。

package require sqlite3

set dbfile ./example.db
set extfile ./exterrcode.dll

file delete $dbfile

sqlite3 db $dbfile
db enable_load_extension 1
db eval [format {SELECT load_extension('%s');} $extfile]

db eval {
	CREATE TABLE "users" (
		"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
		"name" TEXT DEFAULT NULL
	);
	
	INSERT INTO "users" VALUES(1, 'Taro');
}

if {[catch {
	db eval {INSERT INTO "users" VALUES(1, 'Hanako');}; # UNIQUE constraint failed: users.id
} err]} {
	puts $err
#	db eval {SELECT errcode() as ec} e {parray e}
	db eval {SELECT extended_errcode() as ee} e {puts "extended_errcode()=$e(ee)"}; # expected to be 1555 but is 0
#	puts "db errorcode=[db errorcode]"
}
/*
** 2018-12-18
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
** Compiling this file with MSYS2 (assuming that sqlite3 devel installed.)
**    gcc -g -shared exterrcode.c -o exterrcode.dll
**
** Using this library
**    $ sqlite3 example.db
**    > .load exterrcode.dll
**    > (e.g. violate primary key constraint)
**    > SELECT errcode();
**    > -- 19 (SQLITE_CONSTRAINT)
**    > -- https://www.sqlite.org/rescode.html#primary_result_code_list
**    >
**    > (e.g. violate primary key constraint)
**    > SELECT extended_errcode();
**    > -- 1555  (SQLITE_CONSTRAINT_PRIMARYKEY)
**    > -- https://www.sqlite.org/rescode.html#extrc
**
******************************************************************************
**
** This SQLite extension implements a extended_errcode() function.
**
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <assert.h>
#include <string.h>

/*
** Implementation of the errcode() function.
**
** Call sqlite3_errcode(db) function to get the recent errcode.
**
*/
static void errcode_func(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  assert( argc==0 );
  sqlite3* db = sqlite3_context_db_handle(context);
  int errCode = sqlite3_errcode(db);
  sqlite3_result_int(context, errCode);
}

/*
** Implementation of the extended_errcode() function.
**
** Call sqlite3_extended_errcode(db) function to get the recent extended_errcode.
**
*/
static void extended_errcode_func(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  assert( argc==0 );
  sqlite3* db = sqlite3_context_db_handle(context);
  int errCode = sqlite3_extended_errcode(db);
  sqlite3_result_int(context, errCode);
}

#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_exterrcode_init(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  int rc = SQLITE_OK;
  SQLITE_EXTENSION_INIT2(pApi);
  (void)pzErrMsg;  /* Unused parameter */
  rc = sqlite3_create_function(db, "errcode", 0, SQLITE_UTF8, 0, errcode_func, 0, 0);
  rc = sqlite3_create_function(db, "extended_errcode", 0, SQLITE_UTF8, 0, extended_errcode_func, 0, 0);
  return rc;
}

以上是关于c_cpp sqlite3的运行时可加载扩展添加extended_errorcode()函数(但失败了。不要使用它!)的主要内容,如果未能解决你的问题,请参考以下文章

应用程序进入后台时可扩展的 FCM 通知模型

c_cpp sqlite3的简易封装

从 API 获取 JSON,将其添加到 sqlite3 数据库并自动获取下一页

Django无法在Ubuntu上加载SpatiaLite库扩展mod_spatialite

运行filezilla时出现queue.sqlite3加载传输队列时发生错误怎么办

基于 Django + Celery 的应用程序的动态(即运行时可配置)日志配置