Hi,
Could any one please let me know if we can connect to database using C, and if yes let me know the functions in C used to send the SQL statements in the buffer to SQL server.

Dani AI

Generated

asked whether C can connect to a database and which functions send an SQL buffer to the server; pointed toward an embedded-SQL route. Short answer: yes. Common, practical options are (1) ODBC (portable), (2) a vendor native C API (MySQL, PostgreSQL, Oracle, etc.), or (3) an embedded‑SQL precompiler. For executing a SQL string held in a C buffer the typical ODBC calls are SQLExecDirect (direct execution) or SQLPrepare + SQLExecute (prepared statements). For large parameter data use SQLParamData / SQLPutData.

Typical ODBC workflow (high level): allocate environment and connection handles, connect, allocate a statement handle, execute the SQL (direct or prepared), fetch results, free handles. Minimal example of executing a statement in a buffer:

SQLHSTMT hstmt;
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

SQLCHAR *sql = (SQLCHAR *)"INSERT INTO users(name) VALUES('Bob')";
ret = SQLExecDirect(hstmt, sql, SQL_NTS);
/* check ret and call SQLGetDiagRec on error */
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);

Vendor-specific C APIs expose different entry points: MySQL uses mysql_query/mysql_real_connect; PostgreSQL uses PQexec/PQconnectdb; Oracle’s native interface uses OCI calls such as OCIStmtPrepare / OCIStmtExecute. For Microsoft SQL Server, ODBC is common (or OLE DB/Native Client historically); DB‑Library is deprecated.

Practical tips: always check SQLRETURN and use SQLGetDiagRec to get driver/server error text; ensure SQL strings are null‑terminated and use parameter binding to avoid SQL injection and improve performance; manage transactions explicitly when doing multiple statements; free handles to avoid leaks; confirm the appropriate DB driver is installed and matches the ODBC driver manager (unixODBC/Windows ODBC). This covers the functions typically used to send a SQL buffer from C to a server and gives starting points for different database engines.

Recommended Answers

All 2 Replies

This might help Pro*C

thnx,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.