我是C的新手,我正在尝试用sqlite3 db编写简单的程序。
我有这样的查询:
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS) " \
"VALUES (1, 'Paul', 32, 'California' ); " \
我在char *变量中有值,是否可以在printf上使用%s,例如:
sql = somefunction("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS) " \
"VALUES (%d, '%s', %d, '%s');", 1, 'Paul', 32, 'California');
如果不可能,最好的方法是什么?
谢谢!
答案 0 :(得分:2)
答案 1 :(得分:1)
Create a buffer and fill it with the string using sprintf. Here's a very generic example: int main () { char buffer [50]; int n, a=5, b=3; n=sprintf (buffer, "%d plus %d is %d", a, b, a+b); printf ("[%s] is a string %d chars long\n",buffer,n); return 0; } Output: [5 plus 3 is 8] is a string 13 chars long