connecting sqlite to C program - c

[SOLVED]
All I had to do was open the sqlite3.c file inside my "project tree" like this and include sqlite3.h in the header.
I'm trying to connect my C program (using Code Blocks) to a database. I've already downloaded all the key files from the sqlite website like amalgamation zip file that comes with shell.c, sqlite3.c, sqlite3.h, and sqlite3ext.h.
I found this this tutorial on how to use libraries on code blocks. It's pretty simple, but then I realized that none of the files that came with amalgamation zip were .lib or .a. I know sqlite uses libraries, but all the files I listed above are source code files that access those libraries by themselves.
How to compile sqlite -> On their web site they have some good info: Everything is contained within a single code file, so it is easy to drop into the source tree of a larger C or C++ program. They meant the sqlite3.c file. But what did they mean by "dropping into the source tree"?
On the same page, there is a section Compiling The Command-Line Interface. I kind of got it the main points... I also found a sqlite shell tutorial here, so I downloaded the shell and now I can create/modify DBs. Although I feel like I'm pretty close to getting it to work on my C program, I still can't implement any DB from the source files or shell into my code.
Any thoughts?
#include <stdio.h> // printf
#include <sqlite3.h> // SQLite header (from /usr/include)
int main()
{
sqlite3 *db; // database connection
int rc; // return code
char *errmsg; // pointer to an error string
/*
* open SQLite database file test.db
* use ":memory:" to use an in-memory database
*/
rc = sqlite3_open(":memory:", &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");
}
Also, on the Command Line Shell For SQLite you can find a detailed explanation on how to transfer existing data (created from outside the C program since I still can't connect the DB to my C program) to a .txt file. This is good to know and all, but not exactly what I want. I could make it work using a lot of fread, fprintfetc...but that's not quite as good as having a actual DB implemented into your program. For instance, if later I wanted to make my program.c into a program.exe I could do it with the DB embedded inside the program.exe.

The amalgation expects that you put all files of the amalgation in you project folder, that you add #include "sqlite3.h" in your sources that want to access sqlite and that you make sqlite3.c a member of your project.
Assuming a gcc command line compiler, you should do:
gcc main.c sqlite3.c -lpthread -o foo
No need for a library here, and never try to include sqlite3.c.

Related

Module for python3.6.2 (Spyder environment) from c source code

I'm quite new to python that I always use by writing script in spyder and running them in its Ipython console with python3.6.2 .
I'm trying to write a simple module from a "swig_example.c" file following a couple of swig tutorial (http://www.swig.org/tutorial.html, http://www.swig.org/Doc1.3/Python.html#Python_nn6).
My aim is to be able to run a script "main_python.py" which should look like:
import swig_test
print(swig_test.fact(4))
where fact is a function defined in the original c source.
The source file is "swig_example.c":
/* File: swig_example.c */
#include "swig_example.h"
int fact(int n) {
if (n == 0) {
return 1;
}
else {
return n * fact(n-1);
}
}
The header file is as simple as:
/* File: swig_example.h */
int fact(int n);
and the interface one:
/* File: swig_example.i */
%module swig_test
%{
#include "swig_example.h"
%}
%include "swig_example.h"
When in the terminal I run:
swig -python swig_example.i
a "swig_example_wrap.c" and a "swig_test.py" files are created.
How should I proceed to have my "main_python.py" working?
(It now returns a "No module named '_swig_test' error).
I would like to have some script (maybe using distutils?) so that each time I modify the .c source I can easily update the module without changing the "main_python.py" file.
If you have any solution which uses Xcode instead of spyder it would be well accepted.
I think that the question could be useful to many that are new to python (and Mac actually...) and try to use it while not throwing away their previous works...
EDIT:
I -partially- solved the problem. Now the main point remain Spyder.
I create the files ".c", ".h" and ".i" the way I described. Then, following this post (Python.h not found using swig and Anaconda Python) I create, in the same folder, my "setup.py" file:
from distutils.core import setup, Extension
example_module = Extension('_example', sources=['example.c','example.i'])
setup(name='example', ext_modules=[example_module], py_modules= .["example"])
Then, In anaconda navigator I open the terminal of the environment I'm working in, move to the right folder and run:
python setup.py build_ext --inplace
If now I open spyder everything works the desired way. But If I now want to modify my C source, say add a new function, problems arises. I modify the ".c", ".h" and ".i" files annd thenn re-run in the terminal the previous line. The "example.py" file result to e correctly modified (it innncludes the attribute of the new function), but when try to import the module in spyder (import example) changes are nnot registered and an error message "_example has no attribute "new function" is given in the Ipython console unless I restart Spyder itself.
Is there faster way to fix it? (maybe this is the interaction mentioned in the comments... )
Thank you all :-)

I cannot get Eclipse MinGW and Sqlite to Work Together for a C program

So I have spent the whole day trying to figure this out, and alas I have failed. I need help! So I installed Eclipse and MinGW so that I can write a C program. I am new to all of this. I need to be able to access an sqlite database. So I downloaded the sqlite amalgamation and unzipped it to C:/sqlite3 and it contains two .c files and two .h files. In the examples, I have seen online they include the sqlite.h header file as follows:
#include <sqlite.h>
So I think that I need to add an includes folder holding my sqlite info. So I click on my project and select Properties>C/C++ General>Paths and Symbols. With the "Includes" tab selected and the Language GNU C highlighted, I add the Include directory C:\sqlite3. So far no problems, I can build a project that prints my name in the console.
Now I add some sample code from a tutorial site:
#include <stdio.h>
#include <sqlite3.h>
int main(int argc, char* argv[])
{
sqlite3 *db;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
I get 3 errors claiming undefined reference to the sqlite3 functions. So I think I need to add a link to sqlite3 in the compiler. So now I go to Properties>C/C++ Build/Settings and add -lsqlite3 to the MinGW C Linker Command line. Now my errors disappear when I build the project, but I get a compile error that says this:
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/6.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lsqlite3
It looks like it is looking for the sqlite3 files in the MinGW directories. I am stuck. I have tried pasting the file in the MinGW directory and even that doesn't work. I know that I am missing something obvious to the world, but I only have about 5 pieces of hair left on my head and could really use some insight. Please help me to get Eclipse set up using MinGW and accessing Sqlite. Thanks!
The amalgamation is supposed to be compiled together with your other source files. Just add the .c file to your project, as if it were one of your own source files.

Axis2 fails to load DLL

I came across the below line in the Apache-Axis2 log file.
[Sat Nov 14 12:16:08 2015] [error] ..\..\util\src\class_loader.c(167) Loading shared library ..//lib/axis2_http_sender.dll Failed. DLERROR IS DLL Load Error 126: The specified module could not be found.
On analyzing the class_loader.c file from line#156 to line#167 as given below:
dll_name = axutil_dll_desc_get_name(dll_desc, env);
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Shared library to be loaded is %s",dll_name);
dl_handler = AXIS2_PLATFORM_LOADLIB(dll_name);
if (!dl_handler)
{
#ifndef WIN32
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Loading shared library %s Failed. DLERROR IS %s",
dll_name, AXIS2_PLATFORM_LOADLIB_ERROR);
#else
axis2_char_t buff[AXUTIL_WIN32_ERROR_BUFSIZE];
axutil_win32_get_last_error(buff, AXUTIL_WIN32_ERROR_BUFSIZE);
AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Loading shared library %s Failed. DLERROR IS %s",dll_name, buff);
I guess the problem is in the very first line - dll_name = axutil_dll_desc_get_name(dll_desc, env);. The value stored in dll_name is ..//lib/axis2_http_sender.dll. Though the axis2_http_sender.dll is present
in the lib directory which is relative to the executable, the linker fails to connect to it.
I have never seen file name syntax like below:
..//lib/axis2_http_sender.dll
I tested it in Windows Command line and it worked like :
../lib/axis2_http_sender.dll
What are the implication of using consecutive /s in a C
function like fopen()?
I did try few code samples.
Below is a piece of C code:
FILE *fp;
fopen_s(&fp,"C://tempfile.txt", "w");
fputs("Text content", fp);
fclose(fp);
The above code worked fine for me.
Cracked this one finally.
This CSDN blog post suggested that Axis2C Windows distribution depends on OpenSSL DLLs.
I listed the dll dependencies of axis2_apache_server.exe using the following command.
listdlls axis2_apache_server.exe
and the list showed that the two ssl dlls libeay32 and ssleay32 are required to run it. However, these two dlls were missing from the Axis2 Binary Distribution.
(I don't know why & I think it should have been included. Moreover there is no mention of this in Axis2 documentation.)
The above dlls are available in either Apache2 or OpenSSL installs the I added the path to these dlls to my PATH variable.
I ran the axis2_apache_server.exe and voila !!
Conclusion:Consecutive /s in the file path doesn't affect the linking at all.
Moral: One should check the dll dependencies of an exe file first and make sure that all the dlls are present when he ran into a dll load error.
Hard learned moral though!!

How to open files from a NaCl Dev Environment application?

I'm trying to get a simple command line application to run in the NaCl Development Environment. But I don't understand why it doesn't want to open files:
#include <stdio.h>
#include <ppapi_simple/ps_main.h>
int my_main (int argc, char ** argv) {
FILE * f = fopen ("out.txt","w");
if (f) {
fputs ("output to the file", f);
fclose(f);
} else {
puts("could not open file");
}
}
PPAPI_SIMPLE_REGISTER_MAIN(my_main)
Running:
bash.nmf-4.3$ gcc -I"$NACL_SDK_ROOT/include" test.c -lppapi_simple -lnacl_io -lppapi
bash.nmf-4.3$ ./a.out
could not open file
bash.nmf-4.3$
It's clearly possible for an application to open files in arbitrary locations within the dev environment - I'm using nano to edit the test code! But the naclports version of nano doesn't look like it's been changed in ways that are immediately connected to file manipulation..?
Lua is another app that appears to have only been modified very slightly. It falls somewhere in between, in that it can run test files but only if they're placed in /mnt/html5, and won't load them from the home folder. My test program shows no difference in behaviour if I change it to look in /mnt/html5 though.
NB. my goal here is to build a terminal application I can use within the dev environment alongside Lua and nano and so on, not a browser-based app - I assume that makes some difference to the file handling rules.
Programs run in the NaCl Dev Environment currently need to linked with -lcli_main (which in turn depends on -lnacl_spawn) for an entry point which understands how to communicate with the javascript "kernel" in naclprocess.js. They need this to know what current working directory they were run from, as well as to heard about mounted file systems.
Programs linked against just ppapi_simple can be run, but will not setup all the mount points the dev environment may expect.
There is a linker script in the dev env that simplifies linking a command line program -lmingn. For example the test program from the question can be compiled with:
gcc test.c -o test -lmingn
NOTE: This linker script had a recently resolved issue, a new version with the fix was published to the store on 5/5/2015.
In the near future, we have plans to simplify things further, by allowing main to be the entry point.
Thanks for pointing out the lua port lacks the new entry point!
I've filed an issue and will look into fixing it soon:
https://code.google.com/p/naclports/issues/detail?id=215
I found a solution to this, although I don't fully understand what it's doing. It turns out that the small changes made to nano are important, because they cause some other functions elsewhere in the NaCl libraries to get pulled in that correctly set up the environment for file handling.
If the above file is changed to:
#include <stdio.h>
int nacl_main (int argc, char ** argv) {
FILE * f = fopen ("out.txt","w");
if (f) {
fputs ("output to the file", f);
fclose(f);
} else {
puts("could not open file");
}
}
...and compiled with two more libraries:
gcc -I"$NACL_SDK_ROOT/include" test.c -lppapi_simple -lnacl_io -lppapi -lcli_main -lnacl_spawn
...then it will work as expected and write the file.
Instead of registering our own not-main function with PPAPI_SIMPLE_REGISTER_MAIN, pulling in cli_main causes it to do so with an internal function that sets some things up, presumably including what is needed for file writing to work, and expects to then be able to call nacl_main, which is left to the program to define with external visibility (several layers of fake-main stacking going on). This is why the changes to nano look so minimal.
nacl_spawn needs to be linked because cli_main uses it for ...something.

Reference a file in C static library

I created a static library in C using Visual Studio. This library contains a function which accesses a text file stored in that current directory. The library was built properly. But the problem is that when I call the function from outside other project it is not loading that text file( I linked the .lib file properly everything else is working except for loading of that file).
Any ideas how to load a text file from .lib file just by relative path??
Thanks in advance..
The following is the library test function definition
int test()
{
FILE *fp = fopen("hello.txt", "r");
if(!fp) printf("File Error");
return 0;
}
The test.lib file is built and created for this.
Just accessing the current folder hello.txt file but when this function is called from other Project. it is saying File Error.
Modify your code to look at the errno:
#include <errno.h>
#include <string.h>
...
if(!fp) printf("File error: %s\n", strerror(errno));
And then look up the meaning of the errno on your operating system to see what's going on.
I'm pretty sure the fact that you're calling this function from a library is a red herring.
What's most likely happening is that your hello.txt file is not in the working directory of the executing process. Go ahead and #include <windows.h> in your project, and use the GetCurrentDirectory function to see what the working directory is when you run your program. Most likely, it's not the same path as your text file.
To remedy this, you can do one of two things: you can change the startup settings of the program (whether that's from Visual Studio or a Windows shortcut) to specify the working directory (called "Start in:" for a Windows shortcut) to be the path to the text file you want to open, or you can figure out what working directory your program has been using and move your text file there instead.
Edit: Also, if you want the application to use its own directory (where the executable file actually resides) you can use the GetModuleFileName function to get the full path of the executable. Of course, you'll have to trim the filename of the program off the end of the string it produces, but that should be a piece of cake.
Check your file path and print an errno, I think you have a static file path

Resources