admin管理员组

文章数量:1387360

I want to programmatically get, with the C-API, the values, row by row, of the third column of the information_schema table :

./lib/duckdb '/home/raphy/GraspAppData/DuckDB/unsd-citypopulation-year-both.duckdb'
v1.2.1 8e52ec4395
Enter ".help" for usage hints.
D SELECT column_name, ordinal_position, data_type FROM information_schema.columns;
┌─────────────────┬──────────────────┬───────────┐
│   column_name   │ ordinal_position │ data_type │
│     varchar     │      int32       │  varchar  │
├─────────────────┼──────────────────┼───────────┤
│ Country or Area │                1 │ VARCHAR   │
│ Year            │                2 │ BIGINT    │
│ Area            │                3 │ VARCHAR   │
│ Sex             │                4 │ VARCHAR   │
│ City            │                5 │ VARCHAR   │
│ City type       │                6 │ VARCHAR   │
│ Record Type     │                7 │ VARCHAR   │
│ Reliability     │                8 │ VARCHAR   │
│ Source Year     │                9 │ BIGINT    │
│ Value           │               10 │ DOUBLE    │
│ Value Footnotes │               11 │ VARCHAR   │
├─────────────────┴──────────────────┴───────────┤
│ 11 rows                              3 columns │
└────────────────────────────────────────────────┘

With this code:

        duckdb_state state;
        duckdb_result res;

        std::string tableName_s = "unsdcitypopulationyearboth";
        const char* tableName = "unsdcitypopulationyearboth";
        state = duckdb_query(con, "CREATE OR REPLACE TABLE unsdcitypopulationyearboth AS SELECT * FROM read_csv('/home/raphy/Downloads/unsd-citypopulation-year-both.csv', strict_mode = false, ignore_errors = true);", NULL);
        state = duckdb_query(con, "SELECT column_name, ordinal_position, data_type FROM information_schema.columns;", &res);
        
        while (true)
        {
            duckdb_data_chunk result = duckdb_fetch_chunk(res);
            if (!result)
            {
                break;
            }
            idx_t row_count = duckdb_data_chunk_get_size(result);
            duckdb_vector col3 = duckdb_data_chunk_get_vector(result, 2);
            char* col3_data = (char *) duckdb_vector_get_data(col3);
            uint64_t *col3_validity = duckdb_vector_get_validity(col3);
            for (idx_t row = 0; row < row_count; row++)
            {
               if (duckdb_validity_row_is_valid(col3_validity, row))
                {
                    std::cout << col3_data[row] << std::endl;
                }
                else
                {
                    printf("NULL");
                }
                printf("\n");
            }
        }
        duckdb_destroy_result(&res);
        duckdb_disconnect(&con);
        duckdb_close(&ddb);

I get this output :

V

A

R

C

H

A

R

It seems that each output's row is a letter of the first row of the third column of the information_schema table. What am I doing wrong and/or missing? How to make it work?

本文标签: