views:

98

answers:

4

I am fairly new to C. (I have good knowledge of C# [Visual Studio] and Java [Eclipse]) I want to make a program that stores information. My first instinct was to use a database like SQL Server. But I don't think that it is compatible with C. So now I have two options:

  • Create a struct (also typedef) containing the data types.
  • Find a way to integrate SQLite through a C header file

Which option do you think is best? Or do you have another option? I am kind of leaning toward making a struct with a typedef, but could be pursuaded to change my mind.

+6  A: 

ODBC is a C library and permits access to databases in a mode very reminiscent of JDBC. Microsoft has an ODBC driver for SQLServer that is included in Windows or, for non-Windows, you can leverage something like FreeTDS.

Also SQLite provides a mechanism for more direct C access to the database, which permits embedding, etc.

And, depending on your storage needs, BDB might be worth investigating.

ig0774
There is also a ODBC driver for SQLite (http://www.ch-werner.de/sqliteodbc/).
Matthew Flaschen
I'd tend to think the ODBC driver for SQLite is a bit of overkill when implementing a new app in C. Useful if you have pre-existing ODBC code, but why not just use the C API directly?
ig0774
You can definitely use the SQLite API directly, but using ODBC could make it easier to support other databases later. ODBC might also prove useful knowledge to Mohit in the future.
Matthew Flaschen
A: 

You can use MySQL with C example here http://www.ucl.ac.uk/is/mysql/c/

anijhaw
A: 

MySQL is accessible from C using the Connector/C library.

It appears that MS SQL Server is only compatible with ODBC and doesn't provide its own C library. MS has been subtly discouraging C development for a long time, so this isn't surprising.

AFAIK, Oracle, PostGreSQL both have their own C libraries. All of these can also be accessed via ODBC (this includes MySQL)

SQLite is primarily an embedded database written as a C library. This only supports a non-standard version of SQL, so it may not be what you want, but is excellent for storage of small amounts of data. The C API docs can be found here.

Chinmay Kanchi
+1  A: 

The real question is what you need. If you define your own data structure, you have to manage all aspects of what you're doing -- if you want a variable number of records, you'll need to manage that space with malloc/free/realloc. If you want to store data on disk, you'll need to write code to read and write structures. If you want to index the data, you'll need to write code to index it, etc.

In exchange for that, you get really fast access, especially if your needs are really simple (e.g., small, fixed number of identical records so you can easily use an array of structs).

Using a database more or less reverses those -- you gain a lot more flexibility to deal with variable types of data, more data than you want in memory, ACID transactions, building indexes on the fly, etc. In exchange for that, you probably end up with more code to do things you may not need, and slower execution -- especially if you're storing little enough data that it can all fit in memory.

Jerry Coffin