参考:
odbc使用SQLDriverConnect连接数据库
sqliteodbc文档
Linux下通过unixODBC访问SQLite数据库
SQLDriverConnect
microsoft官方文档
¶ 安装软件
sudo apt install -y sqlite3 sqlitebrowser unixodbc unixodbc-dev libsqliteodbc
其中sqlitebrowser可以打开sqlite的数据库文件。
# 连接
const char connStr[] = "Driver=SQLite3;Database=test.db";
= SQLDriverConnect(hdbc, NULL, (SQLCHAR*)connStr, sizeof(connStr), outConnStr, sizeof(outConnStr), &outConnStrLen, SQL_DRIVER_COMPLETE); retcode
¶ 坑
- 注意如果第二个参数为NULL的话,最后一个参数不能为SQL_DRIVER_PROMPT,否则会连接失败。
- 连接串的Driver不能写成
SQLite3 ODBC Driver
。猜测是因为/etc/odbcinst.ini
里只有SQLite3
。
¶ 完整代码
#include <iostream>
#include <cstring>
#if defined(_WIN16) || defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#include <sqlext.h>
using namespace std;
= SQL_NULL_HENV;
SQLHENV henv = SQL_NULL_HDBC;
SQLHDBC hdbc = SQL_NULL_HSTMT;
SQLHSTMT hstmt
void Cleanup() {
//释放语句句柄
if (hstmt != SQL_NULL_HSTMT)
(SQL_HANDLE_STMT, hstmt);
SQLFreeHandle
if (hdbc != SQL_NULL_HDBC) {
//断开数据库连接
(hdbc);
SQLDisconnect//释放连接句柄
(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle}
//释放环境句柄句柄
if (henv != SQL_NULL_HENV)
(SQL_HANDLE_ENV, henv);
SQLFreeHandle}
int main() {
;
SQLRETURN retcode;
SQLLEN length
// Allocate the ODBC environment and save handle.
= SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
retcode if ((retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
("SQLAllocHandle(Env) Failed\n\n");
printf();
Cleanupreturn(9);
}
// Notify ODBC that this is an ODBC 3.0 app.
= SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
retcode if ((retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
("SQLSetEnvAttr(ODBC version) Failed\n\n");
printf();
Cleanupreturn(9);
}
// Allocate ODBC connection handle and connect.
= SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
retcode if ((retcode != SQL_SUCCESS_WITH_INFO) && (retcode != SQL_SUCCESS)) {
("SQLAllocHandle(hdbc1) Failed\n\n");
printf();
Cleanupreturn(9);
}
//数据库连接
//第二个参数是之前配置的数据源,后面是数据库用户名和密码,如果数据源中已经指定了就直接写NULL即可。
//HWND desktopHandle = GetDesktopWindow(); // desktop's window handle
//const char connStr[] = "Driver=SQLite3 ODBC Driver;Database=/home/searchstar/test.db;Timeout=100";
//const char connStr[] = "Driver=SQLite3 ODBC Driver;Database=test.db";
const char connStr[] = "Driver=SQLite3;Database=test.db";
[255];
SQLCHAR outConnStr;
SQLSMALLINT outConnStrLen= SQLDriverConnect(hdbc, NULL, (SQLCHAR*)connStr, sizeof(connStr), outConnStr, sizeof(outConnStr), &outConnStrLen, SQL_DRIVER_COMPLETE);
retcode << outConnStrLen;
cout if (outConnStrLen) {
<< ',' << outConnStr;
cout }
<< endl;
cout if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO)) {
("SQLConnect() Failed\n\n");
printf();
Cleanupreturn(9);
}
//分配执行语句句柄
= SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
retcode if ((retcode != SQL_SUCCESS) && (retcode != SQL_SUCCESS_WITH_INFO)) {
("SQLAllocHandle(hstmt1) Failed\n\n");
printf();
Cleanupreturn(9);
}
();
Cleanup
return 0;
}
输出:
164,DSN=;Database=test.db;StepAPI=;Timeout=;SyncPragma=;NoTXN=;ShortNames=;LongNames=;NoCreat=;NoWCHAR=;FKSupport=;Tracefile=;JournalMode=;LoadExt=;BigInt=;JDConv=;PWD=