I may be too late to answer this question, but I have encountered this problem since two days and have not found a reliable solution on the Internet. I found a solution, so I share it.
// Steps to authenticate sqlite database
download zip file with sqlite3 amalgam
unzip the file. The file should contain shell.c, sqlite3.c, sqlite3.h, sqlite3ext.h
click find link here
3a. Open userauth.c and copy all the code and paste it at the end of your sqlite3.c file.
3b. Open sqlite3userauth.h and copy all the code and run it at the end of your sqlite3.h file.
- create an output file to execute the command in the shell command:
gcc -o sqlite3Exe shell.c sqlite3.c -DSQLITE_USER_AUTHENTICATION -ldl -lpthread
4a. In the shell.c file you get the error message "sqlite3userauth.h": Solution: go to this file and comment on it. (this is because you already included the necessary code when copying sqlite3auth.h to sqlite3.h)
4b. Test your output file by running ./sqlite3Exe (this is the name given to the output file generated in the previous step). you will get sqlite console.
4s Create the database and authentication flag:
command1: .open dbname.db
command2: .auth on
command3: .exit // command 3 is optional
- Building library 5a: creating an object file // Compile sqlite3.c to create an object after adding our new code
: gcc -o sqlite3.o -c sqlite3.c -DSQLITE_USER_AUTHENTICATION
With this command, we create an object file that we can use to compile our c file.
Create a c file to authenticate your database:
//authUser.c
include "stdio.h"
include "stdlib.h"
include "sqlite3.h"
int main (int argc, char * argv []) {int a = 10; int rtn, rtn2; sqlite3 * db; char * sql, * zErMsg; rtn = sqlite3_open ("dbname.db", & db); rtn = sqlite3_user_add (db, "username", "password", 2, 1); // last, but one parameter for the number of bytes for the password, the last parameter for the weather, which the user is an administrator or not if (RTN) {fprintf (stderr, "Unable to open database:% s \ n", sqlite3_errmsg (db)); Return (0); } More {fprintf (stderr, "Secured Database Successfully \ n"); } sqlite3_close (dB); return 0; } Pre> `
Compiling the program // Compiling the program command1: gcc authUser.c sqlite3.o -lpthread -ldl command2: ./a.out // Exit: the protected database was successful
create c file to create a table if the user is authenticated
//createTable.c
include "stdio.h"
include "stdlib.h"
include "sqlite3.h"
static int callback (void * NotUsed, int argc, char ** argv, char ** azColName) {int i; for (i = 0; I am less than argc; I ++) {printf ("% s =% s \ n", azColName [i], argv [i]? argv [i]: "NULL"); } E ("\ n"); return 0; } int main (int argc, char * argv []) {int a = 10; int rtn, rtn2; sqlite3 * db; char * sql, * zErMsg; rtn = sqlite3_open ("dbname.db", & db); rtn = sqlite3_user_authenticate (db, "user", "password", 2); if (RTN) {fprintf (stderr, "Cannot open the database:% s \ n", sqlite3_errmsg (db)); Return (0); } More {fprintf (stderr, "successfully open database \ n"); } sql = "create table newtable (id int not null primary key, name varchar (100) not null)"; // sql = "insert into newtable values ββ(5, 'ishwar')"; rtn = sqlite3_exec (db, sql, callback, 0, & zErMsg); if (rtn! = SQLITE_OK) {sqlite3_free (zErMsg); } More {fprintf (stdout, "The table was created successfully \ n"); // fprintf (stdout, "successfully inserted \ n"); } sqlite3_close (dB); return 0; } Pre> `
compilation of the program // Compilation of the program
command1: gcc createTable.c sqlite3.o -lpthread -ldl
command2: ./a.out // Conclusion: the table was created successfully
- Create c file to add values ββto table
from the previous code you can see two sql variables and two fprintf inside else, now uncomment the commented line and comment on the other. and execute the same command as above output: successfully inserted
And you did, try experimenting with the code, change the values ββof the sqlite3_user_authenticate function, with which you cannot perform these operations, with the maximum possibility of opening the database (when you comment on sqlite3_user_authenticate functon.nothing else)
- Shell Testing
Run the command: ./sqlite3Exe (the output file created in step 4)
command1: .open dbname.db
command2: .tables // you should get an error, user_auth
Thank you (please write to me in case of any problems: ishwar.rimal@gmail.com)