我在 Windows 11 WSL 上运行 GridDB 版本 5.5.0-40191 CE,并安装了 Ubuntu。我正在尝试使用 CLI (gs_sh) 在表中插入行。表格布局如下所示:
CREATE TABLE DimEmployee (
EmployeeKey INTEGER NOT NULL PRIMARY KEY ,
ParentEmployeeKey INTEGER NULL,
EmployeeNationalIDAlternateKey STRING NULL,
ParentEmployeeNationalIDAlternateKey STRING NULL,
SalesTerritoryKey INTEGER NULL,
FirstName STRING NOT NULL,
LastName STRING NOT NULL,
MiddleName STRING NULL,
NameStyle BOOL NOT NULL,
Title STRING NULL,
HireDate TIMESTAMP NULL,
BirthDate TIMESTAMP NULL,
LoginID STRING NULL,
EmailAddress STRING NULL,
Phone STRING NULL,
MaritalStatus STRING NULL,
EmergencyContactName STRING NULL,
EmergencyContactPhone STRING NULL,
SalariedFlag BOOL NULL,
Gender STRING NULL,
PayFrequency BYTE NULL,
BaseRate float NULL,
VacationHours SHORT NULL,
SickLeaveHours SHORT NULL,
CurrentFlag BOOL NOT NULL,
SalesPersonFlag BOOL NOT NULL,
DepartmentName STRING NULL,
StartDate TIMESTAMP NULL,
EndDate TIMESTAMP NULL,
Status STRING NULL,
EmployeePhoto BLOB NULL );
表中的最后一列 (EmployeePhoto) 是一个 BLOB。当我尝试插入该列不为 NULL 的行时,出现以下错误:
D20332:执行 SQL 时发生意外错误。 : msg=[[240001:SQL_COMPILE_SYNTAX_ERROR] 解析 SQL 失败,原因 = 语法错误:0xFFD8FFE000104A464946)
失败的INSERT语句如下所示:
INSERT INTO DimEmployee VALUES (1,18,'14417807',NULL,11,'Guy','Gilbert','R',false,'Production Technician - WC60',TIMESTAMP('2006-01-28T00:00:00.000Z'),TIMESTAMP('1981-11-12T00:00:00.000Z'),'motor-works\guy1','[email protected]','320-555-0195','M','Guy Gilbert','320-555-0195',false,'M',1,12.45,21,30,true,false,'Production',TIMESTAMP('2006-01-28T00:00:00.000Z'),NULL,'Current',0xFFD8FFE000104A464946 );
CLI 似乎不接受代表 BLOB 的文字 0xFFD8FFE000104A464946。如果我用 NULL 替换文字 0xFFD8FFE000104A464946,则 INSERT 工作正常。
有什么想法吗?
在做了一些研究并尝试了不同的方法来表示 BLOB 值之后,我找不到通过 CLI 插入它的方法。然而,使用 GridDB 的 Python 客户端可以轻松地执行插入。与失败的 SQL INSERT 语句等效的 Python 代码如下所示:
import griddb_python as griddb
# Insert one row into a table
def insert_data_into_table (container, row_data):
# Retrieve the container information
try:
container.put(row_data)
except Exception as e:
print(f"Error inserting row: {e}")
return
################ Main code ######################
# GridDB connection
factory = griddb.StoreFactory.get_instance()
gridstore = factory.get_store(
host='10.0.0.1', # Your connection info goes here
port=10001,
cluster_name="myCluster",
username="admin",
password="admin" ))
# Get container info
container_name = "DimEmployee"
container = gridstore.get_container(container_name)
if container is None:
raise RuntimeError(f"Container '{container_name}' does not exist.")
# Retrieve container information
container_info = gridstore.get_container_info(container_name)
if container_info is None:
raise RuntimeError(f"Container '{container_name}' does not exist.")
insert_data_into_table (container, [1, 18, '14417807', 'None', 11, 'Guy', 'Gilbert', 'R', False,
'Production Technician - WC60', '2006-01-28T00:00:00Z', '1981-11-12T00:00:00Z',
'motor-works\\guy1', 'guy3motor-works.com', '320-555-0195', 'M', 'Guy Gilbert',
'320-555-0195', False, 'M', 1, 12.45, 21, 30, True, False, 'Production',
'2006-01-28T00:00:00Z', None, 'Current', bytearray(b'\xff\xd8\xff\xe0\x00\x10\x4A\x46\x49\x46')])
请注意,BLOB 数据不能作为 bytes 传递到 container.put 方法。它需要转换为bytearray,否则您将收到错误:“插入行时出错:第 30 列的值无效,类型应为:10”。 运行代码后,我可以使用 CLI 验证插入是否按预期工作:
gs[public]> select EmployeeKey, HEX(EmployeePhoto) from DimEmployee where EmployeeKey = 1;
> 1 results. (4 ms)
gs[public]> get
+-------------+----------------------+
| EmployeeKey | |
+-------------+----------------------+
| 1 | FFD8FFE000104A464946 |
+-------------+----------------------+
The 1 results had been acquired.