2009/7/3 chandan <chandan.b...@globaledgesoft.com>:
> Hi all,
>    Consider the following scenario,
>    1. A table contains a column of type "text".
>    2. The value of this column for the first row is say "linux".
>
> If we execute the SQL statement: "select name from some_tbl where id = ?"
> using sqlite3_step() API, then what is the value returned by
> sqlite3_column_bytes(). Does the count include the '\0' byte (count ==
> 6). If it does not include the '\0' byte the count should be 5.
>
> I executed the following program:
>
> /******************************************************************************/
> #include <stdio.h>
> #include <stdint.h>
> #include <stdlib.h>
> #include <string.h>
> #include "sqlite3.h"
>
> const char *update_db = "update some_tbl set name = ? where id = ?";
> const char *read_db = "select name from some_tbl where id = ?";
>
> int32_t main(int argc, char *argv[])
> {
>    sqlite3_stmt *stmt;
>    sqlite3 *db;
>    int32_t num_bytes;
>    char buf[100];
>    int32_t ret;
>
>    if (argc != 2) {
>        fprintf(stderr, "Usage: %s <database name>\n", argv[0]);
>        goto out1;
>    }
>
>    ret = sqlite3_initialize();
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "Unable to initialize db.\n");
>        goto out1;
>    }
>
>    ret = sqlite3_open(argv[1], &db);
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "Unable to open database.\n");
>        goto out2;
>    }
>
>    stmt = NULL;
>    ret = sqlite3_prepare_v2(db, update_db, strlen(update_db) + 1,
>                 &stmt, NULL);
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "sqlite3_prepare_v2: %s.\n",
>            sqlite3_errmsg(db));
>        goto out3;
>    }
>
>    ret = sqlite3_bind_text(stmt, 1, "linux", strlen("linux") + 1,
>                SQLITE_TRANSIENT);

This will insert 6 bytes into the db - includes the trailing '\0'

>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "sqlite3_bind_text: %s.\n",
>            sqlite3_errmsg(db));
>        goto out4;
>    }
>
>    ret = sqlite3_bind_int64(stmt, 2, 1);
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "sqlite3_bind_int64: %s.\n",
>            sqlite3_errmsg(db));
>        goto out4;
>    }
>
>    ret = sqlite3_step(stmt);
>    if (ret != SQLITE_DONE) {
>        fprintf(stderr, "sqlite3_step: %s.\n",
>            sqlite3_errmsg(db));
>        goto out4;
>    }
>
>    ret = sqlite3_finalize(stmt);
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "sqlite3_finalize: %s.\n",
>            sqlite3_errmsg(db));
>    }
>
>    stmt = NULL;
>    ret = sqlite3_prepare_v2(db, read_db, strlen(read_db) + 1, &stmt,
>                 NULL);
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "sqlite3_prepare_v2: %s.\n",
>            sqlite3_errmsg(db));
>        goto out3;
>    }
>
>    ret = sqlite3_bind_int64(stmt, 1, 1);
>    if (ret != SQLITE_OK) {
>        fprintf(stderr, "sqlite3_bind_int64: %s.\n",
>            sqlite3_errmsg(db));
>        goto out4;
>    }
>
>    ret = sqlite3_step(stmt);
>    if (ret != SQLITE_ROW) {
>        fprintf(stderr, "sqlite3_step: %s.\n",
>            sqlite3_errmsg(db));
>        goto out4;
>    }
>
>    num_bytes = sqlite3_column_bytes(stmt, 0);
>    printf("*** num_bytes = %d ***\n", num_bytes);
>
>    memcpy(buf, sqlite3_column_text(stmt, 0), num_bytes);
>    printf("*** buf = %s ***\n", buf);
>
>    exit(0);
>
>  out4:
>    ret = sqlite3_finalize(stmt);
>  out3:
>    ret = sqlite3_close(db);
>  out2:
>    ret = sqlite3_shutdown();
>  out1:
>    exit(1);
> }
> /*****************************************************************************/
>
>
> The output shows that sqlite3_column_bytes() returns a count value that
> includes the '\0'. Please correct me if I am arriving at the wrong
> conclusion.

This is what I would expect given that you are inserting a string that
includes the '\0'. Your initial statement that the db contains 'linux'
is wrong; it contains 'linux\0'.

Regards,
Simon
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to