設置 | 登錄 | 註冊

目前共有4篇帖子。

C語言連接SQL字符串的示例

1樓 巨大八爪鱼 2015-7-15 22:41
#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;
}
2樓 巨大八爪鱼 2015-7-15 22:41
[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]$

3樓 巨大八爪鱼 2015-7-15 22:42
在PHP中用“.“運算符可以非常簡單地連接字符串。但在C語言中,則需要使用sprintf函數。sprintf函數可以把數字轉換為字符串。
4樓 巨大八爪鱼 2015-11-19 23:37
其实在PHP中也可以用sprintf函数来连接SQL语句。

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。