在cassandra表中插入一条记录

gcuhipw9  于 2021-06-13  发布在  Cassandra
关注(0)|答案(1)|浏览(373)

我创建了一个表,university\u by \u person,如下所示:

CREATE TABLE university_by_person (
    person_name frozen<name>,
    university_id uuid,
    university_name text,
    university_phone text,
    university_address frozen<address>,
    PRIMARY KEY ((person_name), university_id)
) WITH CLUSTERING ORDER BY (university_id ASC)
    AND comment = 'Q1. Find universities(including university info) near a person';

我还自定义了类型名称和地址,如下所示:

CREATE TYPE name (
    first_name text,
    last_name text
)

CREATE TYPE address (
    street text,
    city text,
    state_or_province text,
    postal_code text,
    country text
)

但是,现在当我尝试将一条记录插入这个表并按enter键时,cmd提示符只返回“…”。看来指挥官认为我的命令还没完成?但我有一个“;”最后。我怎么能把它修好?
我的插入命令:

INSERT INTO  university_by_person
  (person_name, university_id, university_name, university_phone, university_address)
  VALUES (
    {first_name: 'Meryl', last_name: 'Streep'},
    e7ae5cf3-d358-4d99-b900-85902fda9bb0,
    'University of Toronto',
    '416-963-0086',
    {street: '27 King's College Cir', city: 'Toronto', state_or_province: 'Ontario', postal_code: 'M5S 1A1', country: 'Canada'}
  );
ohfgkhjo

ohfgkhjo1#

问题是 street 地址中有一个包含单引号的字符串( ' )人物:

street: '27 King's College Cir'

这个 ... cqlsh中的一行表示它需要更多的输入,因为字符串过早地被“终止”。
要在字符串中使用撇号,需要用另一个单引号转义,使其看起来像:

street: '27 King''s College Cir'

以下是完整更正的cql声明:

INSERT INTO university_by_person
  (person_name, university_id, university_name, university_phone, university_address)
  VALUES (
    {first_name: 'Meryl', last_name: 'Streep'},
    e7ae5cf3-d358-4d99-b900-85902fda9bb0,
    'University of Toronto',
    '416-963-0086',
    {street: '27 King''s College Cir', city: 'Toronto', state_or_province: 'Ontario', postal_code: 'M5S 1A1', country: 'Canada'}
  );

应该按预期执行。该列的值还应包括撇号:

cqlsh:stackoverflow> SELECT university_address FROM university_by_person
  WHERE person_name = {first_name: 'Meryl', last_name: 'Streep'};

 university_address
------------------------------------------------------------------------------------------------------------------------------
 {street: '27 King''s College Cir', city: 'Toronto', state_or_province: 'Ontario', postal_code: 'M5S 1A1', country: 'Canada'}

有关详细信息和注解,请参见转义字符。干杯!

相关问题