SQLite Forum

Reading SQLite database by using C Language

Reading SQLite database by using C Language

(1) By himani_upadhyay on 2022-01-03 08:52:48 [link] [source]

I've already created SQLite database and trying to retrieve that SQLite database in C. My code compiles successfully but while I run that code for reading database file, it is showing firstly permission error ("bash: ./ex1: Permission denied"). I've already given execute permission to allow executing the file as a program and other read-write permission. But after that, the terminal is showing "bash: ./ex1.db: cannot execute binary file: Exec format error" like this.

Can anyone suggest how can I resolve this?

Here is my code for reading SQLite data.

#include <stdio.h> // printf

#include <sqlite3.h> // SQLite header

int main() { sqlite3 *db; // database connection int rc; // return code char *errmsg; // pointer to an error string

 * open SQLite database file ex1.db

rc = sqlite3_open("ex1.db", &db);
if (rc != SQLITE_OK) {
    printf("ERROR opening SQLite DB in memory: %s\n", sqlite3_errmsg(db));
    goto out;
printf("opened SQLite handle successfully.\n");

/* use the database... */

out: /* * close SQLite database */ sqlite3_close(db); printf("database closed.n"); }

Here my run log in text.

himaniupadhyay@himaniupadhyay:~$ gcc -O0 -g mysql.c -lsqlite3 -o mysql himaniupadhyay@himaniupadhyay:~$ ./ex1.db bash: ./ex1.db: Permission denied himaniupadhyay@himaniupadhyay:~$ ./ex1.db bash: ./ex1.db: cannot execute binary file: Exec format error

(2) By Simon Slavin (slavin) on 2022-01-03 09:43:24 in reply to 1 [link] [source]

It looks like you're trying to run the database file rather than the program you compiled. Your copy of Linux/Unix is unable to run that file, since it – correctly – isn't marked as executable.

What command are you typing at your bash prompt ?

(3.1) By Dan Kennedy (dan) on 2022-01-03 11:20:55 edited from 3.0 in reply to 2 [source]


(4) By Dan Kennedy (dan) on 2022-01-03 11:21:37 in reply to 1 [link] [source]

$ gcc -O0 -g mysql.c -lsqlite3 -o mysql

The "-o mysql" option tells the compiler to write your compiled executable to a file named "mysql" on disk. That's the one you want to execute. So type: