如何在C ++中使用SQL? [英] How do I use SQL in C++?
本文介绍了如何在C ++中使用SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我第一次考虑在C ++代码中使用SQL.我有几个问题:
1-这是一种可移植的方式吗?我的意思是我必须告诉用户在其计算机上安装SQL吗?
2-我应该从哪里开始?在哪里可以获取要在C ++中使用的SQL的正确版本?如果有几种可能,那是最好的?
4-是否有指向好的教程的链接(不是SQL本身,而是在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++)?
推荐答案
^ ]
连接mySQL和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是外部文件.您必须将其添加到您的项目中
sqlext.h is external file. u have to add it in to your project
这篇关于如何在C ++中使用SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文