通过select语句查询表中数据。

说明 关于select语句的更多信息,请参见查询数据

前提条件

参数

参数说明
querySQL语句,请根据所需功能进行设置。

示例

使用select pk, long_value, double_value, string_value, bool_value from test_table limit 20语句查询test_table表中数据且最多返回20行数据。系统会返回查询语句的请求类型、返回值Schema、返回结果等信息。

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

    // 获取SQL的响应结果。
    response, err := client.SQLQuery(request)
    if err != nil {
        panic(err)
    }

    // 获取SQL的请求类型。
    fmt.Printf("response type: %v\n", response.StmtType)

    // 获取SQL返回值的Schema。
    columns := response.ResultSet.Columns()
    fmt.Printf("response table meta: %v\n", columns)

    // 获取SQL的返回结果。
    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)
    }
}

返回结果示例如下:

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