SQLite Nineth Lesson Sqlite3_set_authorizer case

Source: Internet
Author: User

A my_authorizer function, such as the following format, can be registered in the interpretation execution statement of the SQLite statement and executed first, like a hook, which controls some access to the SQL statement, similar to the netfilter of a network packet.

#include <iostream>

#include "Sqlite/sqlite3.h"

using namespace std;

/*

The registered callback function prohibits the user from deleting any table, in fact, the third parameter

The name of the table that will pass in the current operation, which can be matched with the value, whether

The table needs to be manipulated, of course, since pszstring is an unsigned integer, if

There are multiple parameters you want to pass in, you can set the third Sqlite3_set_authorizer

parameter is a struct, and then, by casting the pszstring type, returns

SQLITE_OK, means to continue execution, return Sqlite_deny to deny execution

*/

int My_authorizer (void* pszstring,

int ncode,/* The operation code that the current SQL parsing module is executing * /

Const char* psz1,/* a database table that is passed in the current operation by the SQL parsing module and is determined by the opcode to be empty * /

Const char* psz2,

Const char* psz3,

Const char* psz4)

{

int nnotpermitcode = * (int*) pszstring;

if (Nnotpermitcode = = one)

{

printf ("Can not execute drop\n");

return Sqlite_deny;

}

return Sqlite_ok;

}

int Main ()

{

int rc = 0;

sqlite3* db = NULL;

char* pdbname = "test0.db";

char* pszerrmsg = NULL;

rc = SQLITE3_OPEN_V2 (pdbname,&db,sqlite_open_readwrite| Sqlite_open_create,null);

   

char* PSZCREATETB1 = "Create virtual table Geo_test1 using RTREE_I32 (ID, Minx, Maxx)";

rc = sqlite3_exec (db,pszcreatetb1, 0, 0, &pszerrmsg);

char* PSZINSERTSQL1 = "INSERT into geo_test1 values (1, 400, 400)";

rc = sqlite3_exec (DB,PSZINSERTSQL1, 0, 0, &pszerrmsg);

char* pszdroptable = "drop table Geo_test1";

/*

Authorization Action Code (authorizer action Codes)

excerpt from: https://www.sqlite.org/c3ref/c_alter_table.html

each authorization opcode passes different parameters to the authorization registration function, with detailed reference

URL. 11 is to delete the authorization opcode for the database table, when invoking the authorization registration function

The third string gets the name of the table that is currently being manipulated.

*/

int nnotpermitcode = one;

/*

The third parameter is a parameter value passed to the authorization registration function

*/

Sqlite3_set_authorizer (db, My_authorizer, &nnotpermitcode);

sqlite3_stmt* statement;

rc = SQLITE3_PREPARE_V2 (db, Pszdroptable,-1, &statement, NULL);

/*

Single-step debugging to PERR error message: No authored, indicates no permission to operate

*/

Const char* pszerr = sqlite3_errmsg (db);

Sqlite3_close (DB);

   

return 0;

}


SQLite Nineth Lesson Sqlite3_set_authorizer case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.