c中sql命令的可读格式更易读

时间:2012-03-12 22:45:53

标签: sql c

关于在C中格式化长mysql插入命令的更可读方式的任何建议。我认为如果字符串可以分解为多行会有帮助吗?

char sqlcmd[1000];
snprintf(sqlcmd, "INSERT INTO cdrs(col1,col2,col3,col2,col4,col6,col5,col7,col9,col8,col10,col12,col11,col13,col15,col14,col16,col18,col17,col19,col21,col20,col22,col24,col23,col25,col27,col26,col28,col30,col29,col31,col33,col32,col34 VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", one,two,three,four,one,two,three,four,one,two,three,four,one,two,three,four,one ,two,three,four,one,two,three,four,one,two,three,four,one ,two,three,four,one,two,three);

4 个答案:

答案 0 :(得分:3)

您可以将C中的字符串拆分为多个双引号,只需在它们之间放置空格即可。这里的三个字符串完全相同:

"hello world"
"hello" " world"
"hel" "lo " "wor" "ld"

空白包含换行符,因此在您的情况下,您可以:

snprintf(sqlcmd, "INSERT INTO cdrs("
                    "col1, col2, col3, ..."
                 ") values ("
                 // etc

答案 1 :(得分:1)

编译C源代码的一个阶段是通过“预处理器”运行代码,该预处理器将扩展宏,执行字符串化任务以及连接相邻字符串。这使您可以重写这样的代码:

snprintf(sqlcmd, "INSERT INTO cdrs(col1,col2,col3,col2,col4,col6,col5,"
                              "col7,col9,col8,col10,col12,col11,col13,"
                              "col15,col14,col16,col18,col17,col19,col21,"
                              /* .. and so on .. */
                              "'%s','%s','%s','%s','%s','%s','%s')",
                              one,two,three,four,one,two,
                              three,four,one,two,three,four,
                              /* .. and so on .. */
                              one,two,three,four,one,two,three);

您可以将列与变量对齐以生成变量 - >列映射更明显。由于这些只是嘈杂的名字,我没有打扰,但是当应用实际的列和变量名时,结果会更加令人满意。

答案 2 :(得分:1)

它可以分为多行。预处理器在其中一个阶段进行“字符串连接”。

char sqlcmd[1000];
snprintf(sqlcmd, "INSERT INTO cdrs(col1, col2, col3, col2, col4, col6, col5, col7, "
                 "col9, col8, col10, col12, col11, col13, col15, col14, col16, "
                 "col18, col17, col19, col21, col20, col22, col24, col23, col25, "
                 "col27, col26, col28, col30, col29, col31, col33, col32, col34 "
                 "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', "
                 "'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', "
                 "'%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', "
                 "'%s', '%s', '%s', '%s','%s')",
                one, two, three, four, one, two, three, four, one, two, three,
                four, one, two, three, four, one, two, three, four, one, two,
                three, four, one, two, three, four, one, two, three, four,
                one, two, three);

另外,你在VALUES之前错过了一个括号:)

答案 3 :(得分:1)

C中的字符串可以分成多行,例如:

snprintf(sqlcmd, "INSERT INTO cdrs("
"col1,col2,col3,col2,col4,"
"col6,col5,col7,col9,col8,"
"col10,col12,col11,col13,"
"col15,col14,col16,col18,"
"col17,col19,col21,col20,"
"col22,col24,col23,col25,"
"col27,col26,col28,col30,"
"col29,col31,col33,col32,col34"
"VALUES("         
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s','%s','%s',"
"'%s','%s')",    
one,two,three,four,
one,two,three,four,
one,two,three,four,
one,two,three,four,
one,two,three,four,
one,two,three,four,
one,two,three,four,
one,two,three,four,
one,two,three);

实际上,声明如下的字符串相当于"one two three"

const char* msg = "one " "two " "three";