Click here to Skip to main content
12,756,745 members (37,530 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
This is the first time I'm thinking of using SQL in my C++ code. I have a few question:

1- Is this a portable way? I mean will I have to tell users to install SQL on their machine?

2- Where should I start? Where can I get the right version of SQL to be used in C++? if there are several possible ones, which is the best?

4- Any link to a good tutorial (not on SQL itself, but using it in C++)?
Posted 6-Nov-11 20:25pm
Andrew Brock 7-Nov-11 1:34am
What type of SQL.

SQL stands for Secure Query Language. It defines a language for interacting with and controlling a database. It is not a database in itself.

There are lots of different data bases which are driven by SQL. MySQL is the most popular free one, some others are Oracle and Microsoft SQL. You can also interact with Excel spreadsheets with a basic form of SQL.

Depending on the database and approach, users may or may not need additional software.
Joseph Marzbani 7-Nov-11 5:31am
great answer for me. Thank you
nv3 8-Nov-12 2:19am
SQL stands for "Structured Query Language" ... you forgot?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Rate this: bad
Please Sign up or sign in to vote.

Solution 4

#include "windows.h"

#include "sqlext.h"

#include "stdio.h"
#include "string.h"

int main(void)


                HENV                      hEnv = NULL; // Env Handle from SQLAllocEnv()

                HDBC                      hDBC = NULL; // Connection handle

                HSTMT                     hStmt = NULL;// Statement handle

                UCHAR                     szDSN[SQL_MAX_DSN_LENGTH] = "SQL Anywhere 10 CustDB";// Data Source Name buffer

				UCHAR                     szUID[10] = "ml_server";// User ID buffer 

                UCHAR                     szPasswd[10] = "sql";// Password buffer

                UCHAR                     szModel[128];// Model buffer

                SDWORD                    cbModel;// Model buffer bytes recieved

				char					  buff[9] = "Testing";

				UCHAR                   szSqlStr[128]= "INSERT into Quali Values ('Testing')" ;
                RETCODE					  retcode;    
				//sprintf((char*)szSqlStr,"INSERT into <tablename> (Colname) Values ('%s')",buff);

                // Allocate memory for ODBC Environment handle

                SQLAllocEnv (&hEnv);

                // Allocate memory for the connection handle

                SQLAllocConnect (hEnv, &hDBC);


                // Connect to the data source "test" using userid and password.

                retcode = SQLConnect (hDBC, szDSN, SQL_NTS, szUID, SQL_NTS, szPasswd, SQL_NTS);


                if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)


                                // Allocate memory for the statement handle

                                retcode = SQLAllocStmt (hDBC, &hStmt);


                                // Prepare the SQL statement by assigning it to the statement handle

                                retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));


                                // Execute the SQL statement handle

                                retcode = SQLExecute (hStmt);


                                // Project only column 1 which is the models

                                SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);


                                // Get row of data from the result set defined above in the statement

                                retcode = SQLFetch (hStmt);

                                // Free the allocated statement handle

                                SQLFreeStmt (hStmt, SQL_DROP);

                                // Disconnect from datasource

                                SQLDisconnect (hDBC);



                // Free the allocated connection handle

                SQLFreeConnect (hDBC);


                // Free the allocated ODBC environment handle

                SQLFreeEnv (hEnv);

                return 0;


sqlext.h is external file. u have to add it in to your project
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

If MYSQL, then I for a eg.(Is C but not C++):

#include <stdio.h>
#include <winsock.h>
#include <mysql.h>

int main()
MYSQL mysql;
char sql[128];
char *host = "localhost";
char *username = "root";
char *password = "pwd7155807";
char *database = "myphp";
printf("初始化失败"); //Init failed
if(mysql_real_connect(&mysql, host, username, password, database, 0, NULL, 0))
printf("连接数据库成功\n"); //connect database succeed
printf("连接数据库失败\n"); //otherwise failed
//using "insert" sentence to insert info. to the table
strcpy(sql, "INSERT INTO stu(id, name, class) VALUES (3, 'xiaoming', 5)");
if(mysql_real_query(&mysql, sql, strlen(sql)) == 0)// inserting
return 0;

But you should install MYSQL server at first, then select specific directory in your IDE form what dir. your MYSQL server installed.(LIKE: D:\Program Files\mysql5.5.8\include).
Joneeky 8-Nov-12 5:42am
I am sorry. Maybe some Chinese characters are not displayed properly. Do not take notice of them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170217.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100