The author has 4 posts.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1383 3
C語言連接SQL字符串的示例
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 7/15/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;
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 2 Posted at: 7/15/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]$

巨大八爪鱼
武林盟主 二十一级
Reply
Floor 3 Posted at: 7/15/15 22:42
在PHP中用“.“運算符可以非常簡單地連接字符串。但在C語言中,則需要使用sprintf函數。sprintf函數可以把數字轉換為字符串。
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 4 Posted at: 11/19/15 23:37
其实在PHP中也可以用sprintf函数来连接SQL语句。
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1383 Replies: 3
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 11/19/15 23:37
Announcements