Click here to Skip to main content
Rate this: bad
good
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
Comments
Andrew Brock at 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 at 7-Nov-11 5:31am
   
great answer for me. Thank you
nv3 at 8-Nov-12 2:19am
   
SQL stands for "Structured Query Language" ... you forgot?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

Solution 2

  Permalink  
Rate this: bad
good
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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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

#include
#include
#include

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).
  Permalink  
Comments
Joneeky at 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
0 Sergey Alexandrovich Kryukov 223
1 Abhinav S 170
2 Richard Deeming 155
3 CHill60 150
4 Maciej Los 145
0 Sergey Alexandrovich Kryukov 7,176
1 OriginalGriff 6,410
2 Maciej Los 2,877
3 Abhinav S 2,732
4 Peter Leow 2,704


Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2015
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