Ajay_Kumar
Ajay_Kumar

Reputation: 1387

How to connect to SQL through C?

I am making a project in C and I need database access. How would you connect to and access a database in the C language? I am using MySQL currently.

Upvotes: 3

Views: 16716

Answers (3)

D W
D W

Reputation: 49

If you're using MariaDB/mySQL, you can try LGPL-ed connector (Oracle's is GPL'd, see below on licensing):

https://mariadb.com/kb/en/mariadb-connector-c-3-3-5-release-notes/

For PostgreSQL (Postgres-licensed):

https://www.postgresql.org/docs/14/libpq-connect.htm

For SQLite (public domain):

https://www.sqlite.org/cintro.html

So the above libraries would be okay in terms of licensing, where you don't have to open up your code, i.e. you can use them in commercial development (which would not be the case with GPL license).

If you don't want to write your own code that handles connections, allocated/deallocates resources etc. you can use Vely C framework - I use it for this purpose, as the interface is pretty easy and the same for all supported databases:

https://vely.dev/

Vely is EPL-2 licensed, which is a business friendly license, so here you also don't have to open up your source code and can write commercial applications.

Upvotes: 0

Tony
Tony

Reputation: 10327

Depending on why you actually need to use a database (you didn't say) you could start using SQLite to develop your application.

Once you have it working and you have decided on which server you want to use (MySQL, MS SQL Server, Oracle, etc.) you can then read the documentation on creating a connection specific to that database server.

Upvotes: 1

Luke
Luke

Reputation: 3872

There are many solutions, depending on which database you are using and which OS you are using. One solution that will provide broad access to an array of combinations would be ODBC. You would connect to the database with a function like SQLConnect and use the other API functions to do your queries.

The links are windows specific, but you should be able to find analogs for whatever OS you will be using on your client side.

Upvotes: 1

Related Questions