Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Comments
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?

 
Share this answer
 
 
Share this answer
 
C++
#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
 
Share this answer
 
v2
MYSQL or MSSQL?
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";
if(!mysql_init(&mysql))
{
printf("初始化失败"); //Init failed
exit(1);
}
if(mysql_real_connect(&mysql, host, username, password, database, 0, NULL, 0))
{
printf("连接数据库成功\n"); //connect database succeed
}
else
{
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
{
printf("插入成功\n");
}
else
{
printf("插入失败\n");
}
mysql_close(&mysql);
getchar();
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).
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900