ScyllaDB University Live | Free Virtual Training Event
Learn more
ScyllaDB Documentation Logo Documentation
  • Server
  • Cloud
  • Tools
    • ScyllaDB Manager
    • ScyllaDB Monitoring Stack
    • ScyllaDB Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
  • Resources
    • ScyllaDB University
    • Community Forum
    • Tutorials
Download
ScyllaDB Docs Scylla C/C++ Driver Features Basics Keyspaces

Keyspaces¶

Setting the Keyspace at Connection Time¶

A session can be initially connected using a supplied keyspace.

Performance Tip: An application should create a single session object per keyspace as a session object is designed to be created once, reused, and shared by multiple threads within the application.

CassSession* session = cass_session_new();
CassCluster* cluster = cass_cluster_new();

/* Configure cluster */

CassFuture* connect_future
  = cass_session_connect_keyspace(session, cluster, "keyspace1");

/* Handle connect future */

cass_future_free(connect_future);

cass_session_free(session);
cass_cluster_free(cluster);

Changing Keyspaces¶

You can specify a keyspace to change to by executing a USE statement on a connection session object.

void use_keyspace(CassSession* session) {
  CassStatement* use_statement
    = cass_statement_new("USE keyspace1", 0);

  CassFuture* use_future
    = cass_session_execute(session, use_statement);

  /* Check future result... */

  cass_statement_free(use_statement);
  cass_future_free(use_future);
}

Be very careful though: if the session is shared by multiple threads, switching the keyspace at runtime could easily cause unexpected query failures.

Single Session and Multiple Keyspaces¶

It is possible to interact with multiple keyspaces using a single session object by fully qualifying the table names in your queries e.g. keyspace_name.table_name.

Examples¶

SELECT * FROM keyspace_name.table_name WHERE ...;
INSERT INTO keyspace_name.table_name (...) VALUES (...);

Creating Keyspaces and Tables¶

It is also possible to create keyspaces and tables by executing CQL using a session object.

Examples¶

CREATE KEYSPACE cpp_driver
  WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
CREATE TABLE cpp_driver.contributers (
  lastname text,
  firstname test,
  company text,
  website text,
  since timestamp,
  last_activity timestamp
  PRIMARY KEY(lastname));

Was this page helpful?

PREVIOUS
Handling Results
NEXT
Prepared Statements
  • Create an issue
  • Edit this page

On this page

  • Keyspaces
    • Setting the Keyspace at Connection Time
    • Changing Keyspaces
    • Single Session and Multiple Keyspaces
      • Examples
    • Creating Keyspaces and Tables
      • Examples
Scylla C/C++ Driver
  • master
    • master
  • C/C++ Driver for ScyllaDB
  • API Documentation
    • CassAggregateMeta
    • CassAuthenticator
    • CassAuthenticatorCallbacks
    • CassBatch
    • CassCluster
    • CassCollection
    • CassColumnMeta
    • CassCustomPayload
    • CassDataType
    • CassErrorResult
    • CassExecProfile
    • CassFunctionMeta
    • CassFuture
    • CassIndexMeta
    • CassInet
    • CassIterator
    • CassKeyspaceMeta
    • CassLogMessage
    • CassMaterializedViewMeta
    • CassMetrics
    • CassNode
    • CassPrepared
    • CassResult
    • CassRetryPolicy
    • CassRow
    • CassSchemaMeta
    • CassSession
    • CassSpeculativeExecutionMetrics
    • CassSsl
    • CassStatement
    • CassTableMeta
    • CassTimestampGen
    • CassTuple
    • CassUserType
    • CassUuid
    • CassUuidGen
    • CassValue
    • CassVersion
    • DseDateRange
    • DseDateRangeBound
    • DseLineString
    • DseLineStringIterator
    • DsePolygon
    • DsePolygonIterator
  • Features
    • Basics
      • Batches
      • Binding Parameters
      • Client-side timestamps
      • Consistency
      • Data Types
      • The date and time Types
      • Futures
      • Handling Results
      • Keyspaces
      • Prepared Statements
      • Schema Metadata
      • Tuples
      • User-Defined Types (UDTs)
      • UUIDs
    • Building
    • Client Configuration
    • Cloud
    • Configuration
      • Retry policies
    • Execution Profiles
    • FAQ
    • Installation
    • Logging
    • Metrics
    • Scylla Specific Features
    • Security
      • SSL
    • Testing
      • Cassandra Cluster Manager (CCM)
    • Tracing
  • DSE Features
    • Authentication
    • Geospatial types
Docs Tutorials University Contact Us About Us
© 2025, ScyllaDB. All rights reserved. | Terms of Service | Privacy Policy | ScyllaDB, and ScyllaDB Cloud, are registered trademarks of ScyllaDB, Inc.
Last updated on 28 April 2025.
Powered by Sphinx 7.4.7 & ScyllaDB Theme 1.8.6