巨大八爪鱼
武林盟主 二十一級
|
#include <stdio.h> #include <string.h>
int main(void) { int num, num2; printf("Please input one number:\n"); scanf("%d", &num); printf("Please input another number:\n"); scanf("%d", &num2); int num3 = num * num2 + 8 * num;
char sql[150]; memset(sql, '\0', sizeof(sql)); sprintf(sql, "INSERT INTO Numbers VALUES (NULL, %d, %d, 'STR%d')", num, num2, num3); printf("%s;\n", sql); return 0; }
|
巨大八爪鱼
武林盟主 二十一級
|
[octopus@pc3 sqlconcat]$ ./sqlconcat Please input one number: 147 Please input another number: 552 INSERT INTO Numbers VALUES (NULL, 147, 552, 'STR82320'); [octopus@pc3 sqlconcat]$
|
巨大八爪鱼
武林盟主 二十一級
|
在PHP中用“.“運算符可以非常簡單地連接字符串。但在C語言中,則需要使用sprintf函數。sprintf函數可以把數字轉換為字符串。
|
巨大八爪鱼
武林盟主 二十一級
|
其实在PHP中也可以用sprintf函数来连接SQL语句。
|