You can execute the select statement to query data in a table.

Note For more information about the select statement, see Query data.

Prerequisites

Parameters

ParameterDescription
queryThe SQL statement. Configure the parameter based on the required feature.

Examples

Execute the select pk, long_value, double_value, string_value, bool_value from test_table limit 20 statement to query data in the table named test_table and set the maximum number of rows that you want to return to 20. The system returns the request type, the schema of the returned results, and the returned results of the query statement.

func queryData(client *tablestore.TableStoreClient) {
    // Create a SQL request. 
    request := &tablestore.SQLQueryRequest{Query: "select pk, long_value, double_value, string_value, bool_value from test_table limit 20"}

    // Obtain the response to the SQL request. 
    response, err := client.SQLQuery(request)
    if err != nil {
        panic(err)
    }

    // Obtain the SQL request type. 
    fmt.Printf("response type: %v\n", response.StmtType)

    // Obtain the schema of the returned results of the SQL request. 
    columns := response.ResultSet.Columns()
    fmt.Printf("response table meta: %v\n", columns)

    // Obtain the returned results of the SQL request. 
    resultSet := response.ResultSet
    fmt.Println("response resultset:")
    for resultSet.HasNext() {
        row := resultSet.Next()
        stringValue, _ := row.GetString(0)
        fmt.Printf("%v, ", stringValue)
        stringValue, _ = row.GetStringByName("pk")
        fmt.Printf("%v, ", stringValue)
        longValue, _ := row.GetFloat64(1)
        fmt.Printf("%v, ", longValue)
        longValue, _ = row.GetFloat64ByName("long_value")
        fmt.Printf("%v, ", longValue)
        floatValue, _ := row.GetFloat64(2)
        fmt.Printf("%v, ", floatValue)
        floatValue, _ = row.GetFloat64ByName("double_value")
        fmt.Printf("%v, ", floatValue)
        stringValue, _ = row.GetString(3)
        fmt.Printf("%v, ", stringValue)
        stringValue, _ = row.GetStringByName("string_value")
        fmt.Printf("%v, ", stringValue)
        boolValue, _ := row.GetBool(4)
        fmt.Printf("%v, ", boolValue)
        boolValue, _ = row.GetBoolByName("bool_value")
        fmt.Printf("%v\n", boolValue)
    }
}

Sample output:

response type: SQL_SELECT
response table meta: [pk:STRING long_value:INTEGER double_value:DOUBLE string_value:STRING bool_value:BOOLEAN]
response resultset:
binary_null, binary_null, 0, 0, 1, 1, a, a, false, false
bool_null, bool_null, 0, 0, 1, 1, a, a, false, false
double_null, double_null, 0, 0, 1, 1, a, a, false, false
long_null, long_null, 0, 0, 1, 1, a, a, false, false
string_null, string_null, 0, 0, 1, 1, , , false, false