主题
打开内存数据库 - OpenMemoryDatabase
函数简介
打开内存中的数据库,返回一个数据库对象指针。
接口名称
OpenMemoryDatabaseDLL调用
c
long OpenMemoryDatabase(long ola, long address, int size, string password);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
| address | 长整数型 | 数据库所在的内存首地址。 |
| size | 整数型 | 数据库内存区域大小(字节)。 |
| password | 字符串 | 数据库密码。 |
示例
以下示例演示 从磁盘读取 SQLite 数据库文件 → 取得内存首地址 → 打开内存库 → 执行查询 → 关闭连接 的完整流程。
SDK 调用
cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
const char* dbPath = "data/app.db";
std::ifstream ifs(dbPath, std::ios::binary | std::ios::ate);
if (!ifs) return;
long dbSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> dbBuf(dbSize);
ifs.read((char*)dbBuf.data(), dbSize);
long db = ola.OpenMemoryDatabase((long)dbBuf.data(), dbSize, "");
if (db != 0) {
int count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master");
ola.CloseDatabase(db);
}
// dbBuf 由 vector 析构释放,与插件无关csharp
using System.IO;
using System.Runtime.InteropServices;
using OLAPlug;
var ola = new OLAPlugServer();
byte[] dbBytes = File.ReadAllBytes(@"data\app.db");
GCHandle pin = GCHandle.Alloc(dbBytes, GCHandleType.Pinned);
try {
long db = ola.OpenMemoryDatabase(pin.AddrOfPinnedObject().ToInt64(), dbBytes.Length, "");
if (db != 0)
{
int count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master");
ola.CloseDatabase(db);
}
} finally { pin.Free(); }python
from OLAPlugServer import OLAPlugServer
import ctypes
ola = OLAPlugServer()
with open("data/app.db", "rb") as f:
db_bytes = f.read()
buf = ctypes.create_string_buffer(db_bytes, len(db_bytes))
db = ola.OpenMemoryDatabase(ctypes.addressof(buf), len(db_bytes), "")
if db != 0:
count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master")
ola.CloseDatabase(db)java
import com.olaplug.OLAPlugServer;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.nio.file.Files;
import java.nio.file.Paths;
OLAPlugServer ola = new OLAPlugServer();
byte[] dbBytes = Files.readAllBytes(Paths.get("data/app.db"));
Memory mem = new Memory(dbBytes.length);
mem.write(0, dbBytes, 0, dbBytes.length);
long db = ola.OpenMemoryDatabase(Pointer.nativeValue(mem), dbBytes.length, "");
if (db != 0) {
int count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master");
ola.CloseDatabase(db);
}cpp
var ola = com("OlaPlug.OlaSoft")
var dbSize = ola.GetFileSize("data/app.db")
var dbPtr = ola.ReadBytesFromFile("data/app.db", 0, 0)
if(dbPtr && dbSize > 0) {
var db = ola.OpenMemoryDatabase(dbPtr, dbSize, "")
ola.FreeMemoryPtr(dbPtr) // 打开成功后插件已拷贝,可立即释放读盘缓冲
if(db) {
var count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master")
ola.CloseDatabase(db)
}
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
dbSize = ola.GetFileSize("data/app.db")
dbPtr = ola.ReadBytesFromFile("data/app.db", 0, 0)
If dbPtr <> 0 And dbSize > 0 Then
db = ola.OpenMemoryDatabase(dbPtr, dbSize, "")
ola.FreeMemoryPtr(dbPtr)
If db <> 0 Then
count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master")
ola.CloseDatabase(db)
End If
End Iftext
.局部变量 ola, OLAPlug
.局部变量 dbData, 字节集
.局部变量 db, 长整数型
.局部变量 count, 整数型
ola.创建 ()
' ① 读入文件到字节集
dbData = 读入文件 (“data/app.db”, )
.如果真 (取字节集长度 (dbData) > 0)
' ② 取变量数据地址 → 内存首地址;取字节集长度 → size
db = ola.OpenMemoryDatabase (取变量数据地址 (dbData), 取字节集长度 (dbData), “”)
.如果真 (db ≠ 0)
count = ola.ExecuteScalar (db, “SELECT COUNT(*) FROM sqlite_master”)
ola.CloseDatabase (db)
.如果真结束
.如果真结束
' dbData 为局部字节集,过程结束前须保持有效;打开成功后插件已拷贝库内容aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var dbData = io.load("data/app.db");
var db = ola.OpenMemoryDatabase(dbData, #dbData, "");
if(db){
var count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master");
ola.CloseDatabase(db);
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 dbData <类型 = 字节集类>
' ① 读入文件到字节集
dbData = 读入文件("data/app.db", )
如果真 (取字节集长度(dbData) > 0)
{
' ② 取字节集指针 → 内存首地址;取字节集长度 → size
长整数 db = ola.OpenMemoryDatabase(取字节集指针(dbData), 取字节集长度(dbData), "")
如果真 (db ≠ 0)
{
整数 count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master")
ola.CloseDatabase(db)
}
}
' dbData 在作用域内须保持有效;打开成功后插件已拷贝库内容cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
std::ifstream ifs("data/app.db", std::ios::binary | std::ios::ate);
long dbSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> dbBuf(dbSize);
ifs.read((char*)dbBuf.data(), dbSize);
long db = ola.OpenMemoryDatabase((long)dbBuf.data(), dbSize, "");
if (db != 0) {
int count = ola.ExecuteScalar(db, "SELECT COUNT(*) FROM sqlite_master");
ola.CloseDatabase(db);
}原生 DLL 调用
cpp
#include <fstream>
#include <vector>
long instance = CreateCOLAPlugInterFace();
const char* dbPath = "data/app.db";
std::ifstream ifs(dbPath, std::ios::binary | std::ios::ate);
auto dbSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> buf(dbSize);
ifs.read((char*)buf.data(), dbSize);
long db = OpenMemoryDatabase(instance, (long)buf.data(), dbSize, "");
if (db != 0) {
ExecuteScalar(instance, db, "SELECT COUNT(*) FROM sqlite_master");
CloseDatabase(instance, db);
}csharp
using System.IO;
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long OpenMemoryDatabase(long ola, long address, int size, string password);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int ExecuteScalar(long ola, long db, string sql);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int CloseDatabase(long ola, long db);
long instance = CreateCOLAPlugInterFace();
byte[] dbBytes = File.ReadAllBytes(@"data\app.db");
GCHandle pin = GCHandle.Alloc(dbBytes, GCHandleType.Pinned);
try {
long db = OpenMemoryDatabase(instance, pin.AddrOfPinnedObject().ToInt64(), dbBytes.Length, "");
if (db != 0) {
ExecuteScalar(instance, db, "SELECT COUNT(*) FROM sqlite_master");
CloseDatabase(instance, db);
}
} finally { pin.Free(); }python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
ola.OpenMemoryDatabase.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
with open("data/app.db", "rb") as f:
db_bytes = f.read()
buf = create_string_buffer(db_bytes, len(db_bytes))
db = ola.OpenMemoryDatabase(instance, c_int64(id(buf.raw)), len(db_bytes), b"")
if db:
ola.ExecuteScalar(instance, db, b"SELECT COUNT(*) FROM sqlite_master")
ola.CloseDatabase(instance, db)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 数据库对象指针。若打开失败,返回 0。 |
注意事项
| 项目 | 说明 |
|---|---|
| 内存有效性 | address 与 size 须指向包含 完整数据库文件内容 的有效内存区域;调用期间缓冲不可被释放或覆盖。 |
| 字节流 → 地址 | 易语言:取变量数据地址(字节集) + 取字节集长度(字节集);火山:取字节集指针(字节集) + 取字节集长度(字节集);C#:GCHandle.Alloc 固定数组后取 AddrOfPinnedObject();Python:create_string_buffer 后 addressof(buf) 或 id(buf.raw)。 |
| ReadBytesFromFile | 插件 ReadBytesFromFile 返回的指针可直接作为 address;打开成功后须 FreeMemoryPtr 释放读盘缓冲(库内容已拷贝)。 |
| 口令 | 若数据库有密码保护,须传入正确的 password。 |
| 释放 | 使用完成后须 CloseDatabase 释放连接。 |
| 适用场景 | 若数据库已在磁盘,可直接用 OpenDatabase;本接口适合 网络下载、资源嵌入、不落盘 等场景。 |
