读取所有表名 - GetAllTableNames
函数简介
读取数据库中所有表的名称,返回一个包含表名的字符串列表指针。
函数原型
long GetAllTableNames(long ola, const long db);
参数定义
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。db
(长整型数): 数据库连接句柄,由 OpenDatabase 接口生成。
返回值
- 返回值:包含所有表名的字符串列表指针。如果操作失败,返回
0
。
示例
SDK
C#
using System;
using OLA.ServiceCenter.PlugFactory;
namespace OLADemo
{
internal class Program
{
static OLAPlugServer OLAServer;
static void Main(string[] args)
{
OLAServer = new OLAPlugServer();
var regResult = OLAServer.Reg(
OLAServer.UserCode,
OLAServer.SoftCode,
OLAServer.FeatureList
);
OLAServer.CreateCOLAPlugInterFace();
long db = OLAServer.OpenDatabase("OLAPlugDemo.db", "olaplug");
Console.WriteLine($"OpenDatabase 返回:{db}");
// 读取所有表名
long tableNamesPtr = OLAServer.GetAllTableNames(db);
if (tableNamesPtr != 0)
{
// 假设返回的是一个以逗号分隔的表名字符串
string tableNames = Marshal.PtrToStringAnsi(new IntPtr(tableNamesPtr));
string[] tables = tableNames.Split(',');
Console.WriteLine("数据库中的表名:");
foreach (var table in tables)
{
Console.WriteLine(table);
}
}
else
{
Console.WriteLine("读取表名失败。");
}
}
}
}
Python
from OLAPlugServer import OLAPlugServer
# 实例化
OLAServer = OLAPlugServer()
# 注册
OLAServer.Reg(OLAServer.UserCode, OLAServer.SoftCode, OLAServer.FeatureList)
# 创建OLAPlug对象
OLAServer.CreateCOLAPlugInterFace()
# 打开数据库
db = OLAServer.OpenDatabase('OLAPlug.db', 'OLAPlug')
print(f"openDatabaseResult={db}")
# 读取所有表名
tableNamesPtr = OLAServer.GetAllTableNames(db)
if tableNamesPtr != 0:
# 假设返回的是一个以逗号分隔的表名字符串
tableNames = ctypes.cast(tableNamesPtr, ctypes.c_char_p).value.decode('utf-8')
tables = tableNames.split(',')
print("数据库中的表名:")
for table in tables:
print(table)
else:
print("读取表名失败。")
原生方式
Python
import os
import sys
from ctypes import *
# 1. 加载dll
# 此处路径为插件所在路径,请根据实际情况修改。
# 32位python使用x86版本,64位python使用x64版本
if sys.maxsize > 2**32:
olaplug_dll = WinDLL(os.path.abspath(os.path.join(os.getcwd(), 'OLAPlug_x64.dll')))
else:
olaplug_dll = WinDLL(os.path.abspath(os.path.join(os.getcwd(), 'OLAPlug_x86.dll')))
# 2. 注册到后台
UserCode = "c38e200f116d4fa8bd0deb45ccb523ea"
SoftCode = "701bc92ba84642c68845e7a06c10fd99"
FeatureList = "OLA|OLAPlus"
olaplug_dll.Reg.argtypes = [c_char_p, c_char_p, c_char_p]
olaplug_dll.Reg.restype = c_int32
result = olaplug_dll.Reg(UserCode.encode('utf-8'), SoftCode.encode('utf-8'), FeatureList.encode('utf-8'))
print(f'注册结果返回: {result}')
# 3. 创建ola对象
olaplug_dll.CreateCOLAPlugInterFace.restype = c_void_p
ola_obj = olaplug_dll.CreateCOLAPlugInterFace()
# 4. 打开数据库
olaplug_dll.OpenDatabase.argtypes = [c_void_p, c_char_p, c_char_p]
olaplug_dll.OpenDatabase.restype = c_void_p
db = olaplug_dll.OpenDatabase(ola_obj, "OLAPlugDemo.db".encode('utf-8'), "olaplug".encode('utf-8'))
print(f"openDatabaseResult={db}")
# 5. 读取所有表名
olaplug_dll.GetAllTableNames.argtypes = [c_void_p, c_void_p]
olaplug_dll.GetAllTableNames.restype = c_void_p
tableNamesPtr = olaplug_dll.GetAllTableNames(ola_obj, db)
if tableNamesPtr != 0:
# 假设返回的是一个以逗号分隔的表名字符串
tableNames = ctypes.cast(tableNamesPtr, ctypes.c_char_p).value.decode('utf-8')
tables = tableNames.split(',')
print("数据库中的表名:")
for table in tables:
print(table)
else:
print("读取表名失败。")
注意事项
- 返回的表名字符串列表通常是以特定分隔符(如逗号)分隔的字符串,需要根据实际情况进行解析。
- 如果数据库中没有表或操作失败,函数将返回
0
。 - 使用完返回的字符串指针后,应妥善处理内存,避免内存泄漏。