我有以下疑问:
考虑从 sqlite 数据库中选择下表:
id | label
1 | P1
1 | P2
1 | P3
1 | P4
我运行以下测试代码:
const unsigned char * new1;
const unsigned char * new2;
const unsigned char * new3;
const unsigned char * new4;
sqlite3_step(stmt);
new1=sqlite3_column_text(stmt,1);
sqlite3_step(stmt);
new2=sqlite3_column_text(stmt,1);
sqlite3_step(stmt);
new3=sqlite3_column_text(stmt,1);
sqlite3_step(stmt);
new4=sqlite3_column_text(stmt,1);
printf("%s\n%s\n%s\n%s\n",new1,new2,new3,new4);
printf("%p\n%p\n%p\n%p\n",new1,new2,new3,new4);
我得到:
P4
P4
P4
P4
0x1d5b7f8
0x1d5b7f8
0x1d5b7f8
0x1d5b7f8
我要打印
P1
P2
P3
P4
Address1
Address2
Address3
Address4
有什么方法可以仅使用字符串文字来打印此内容,而不使用缓冲区 const char value[N];、strcpy 等...?
最佳答案
The pointers returned are valid until a type conversion occurs as described above, or until sqlite3_step() or sqlite3_reset() or sqlite3_finalize() is called. The memory space used to hold strings and BLOBs is freed automatically.
除非您在每个 sqlite3_column_text
之后执行 printf
(即使如此,地址仍然相同),否则您需要制作副本。
关于c - 将 sqlite3_column_text() 返回到文字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42263109/