读取列类型 - GetColumnType
函数简介
读取查询结果集中指定列的数据类型,返回列的类型代码。
函数原型
int GetColumnType(long ola, long stmt, int columnIndex);
参数定义
ola
(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。stmt
(长整型数): 数据库语句对象指针,由 ExecuteReader 接口返回。columnIndex
(整型数): 列索引,从0开始。
返回值
- 返回值:列的类型代码,具体如下:
SQLITE_INTEGER
:整数类型,返回1
。SQLITE_FLOAT
:浮点数类型,返回2
。SQLITE_TEXT
:文本类型,返回3
。SQLITE_BLOB
:二进制大对象类型,返回4
。SQLITE_NULL
:空值类型,返回5
。
- 如果操作失败,返回
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}");
// 执行查询并返回STMT指针
string sql = "SELECT * FROM MyTable";
long stmtPtr = OLAServer.ExecuteReader(db, sql);
if (stmtPtr != 0)
{
Console.WriteLine("查询成功,STMT指针已返回。");
// 读取列数量
int columnCount = OLAServer.GetColumnCount(stmtPtr);
Console.WriteLine($"查询结果的列数: {columnCount}");
// 遍历列并读取列类型
for (int i = 0; i < columnCount; i++)
{
int columnType = OLAServer.GetColumnType(stmtPtr, i);
string typeName = columnType switch
{
1 => "SQLITE_INTEGER",
2 => "SQLITE_FLOAT",
3 => "SQLITE_TEXT",
4 => "SQLITE_BLOB",
5 => "SQLITE_NULL",
_ => "UNKNOWN"
};
Console.WriteLine($"列 {i} 的类型: {typeName}");
}
}
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}")
# 执行查询并返回STMT指针
sql = "SELECT * FROM MyTable"
stmtPtr = OLAServer.ExecuteReader(db, sql)
if stmtPtr != 0:
print("查询成功,STMT指针已返回。")
# 读取列数量
columnCount = OLAServer.GetColumnCount(stmtPtr)
print(f"查询结果的列数: {columnCount}")
# 遍历列并读取列类型
for i in range(columnCount):
columnType = OLAServer.GetColumnType(stmtPtr, i)
typeName = {
1: "SQLITE_INTEGER",
2: "SQLITE_FLOAT",
3: "SQLITE_TEXT",
4: "SQLITE_BLOB",
5: "SQLITE_NULL"
}.get(columnType, "UNKNOWN")
print(f"列 {i} 的类型: {typeName}")
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. 执行查询并返回STMT指针
sql = "SELECT * FROM MyTable"
olaplug_dll.ExecuteReader.argtypes = [c_void_p, c_void_p, c_char_p]
olaplug_dll.ExecuteReader.restype = c_void_p
stmtPtr = olaplug_dll.ExecuteReader(ola_obj, db, sql.encode('utf-8'))
if stmtPtr != 0:
print("查询成功,STMT指针已返回。")
# 读取列数量
olaplug_dll.GetColumnCount.argtypes = [c_void_p, c_void_p]
olaplug_dll.GetColumnCount.restype = c_int32
columnCount = olaplug_dll.GetColumnCount(ola_obj, stmtPtr)
print(f"查询结果的列数: {columnCount}")
# 遍历列并读取列类型
for i in range(columnCount):
olaplug_dll.GetColumnType.argtypes = [c_void_p, c_void_p, c_int]
olaplug_dll.GetColumnType.restype = c_int32
columnType = olaplug_dll.GetColumnType(ola_obj, stmtPtr, i)
typeName = {
1: "SQLITE_INTEGER",
2: "SQLITE_FLOAT",
3: "SQLITE_TEXT",
4: "SQLITE_BLOB",
5: "SQLITE_NULL"
}.get(columnType, "UNKNOWN")
print(f"列 {i} 的类型: {typeName}")
else:
print("查询失败。")
注意事项
- 该函数用于获取查询结果集中指定列的数据类型,通常与 ExecuteReader 和 GetColumnCount 配合使用。
- 如果列索引无效或操作失败,函数将返回
0
。可以通过 GetDatabaseError 函数获取详细的错误信息。 - 列类型代码与SQLite数据库的类型定义一致,具体如下:
SQLITE_INTEGER
:整数类型,返回1
。SQLITE_FLOAT
:浮点数类型,返回2
。SQLITE_TEXT
:文本类型,返回3
。SQLITE_BLOB
:二进制大对象类型,返回4
。SQLITE_NULL
:空值类型,返回5
。