gpt4 book ai didi

c++ - 在 C++ 中格式化 SQL 查询

转载 作者:太空狗 更新时间:2023-10-29 21:50:32 24 4
gpt4 key购买 nike

我想知道是否可以在 C++ 中使用类似于 "sqlparse" module in Python 的东西格式化我的查询。你知道我能用什么吗?

很抱歉之前没有提供示例。我想要这样的东西:

SELECT MEMB.NAME, MEMB.AGE, AGE.GROUP FROM MEMB, AGE WHERE MEMB.AGE = AGE.AGE

变成这样:

SELECT MEMB.NAME,
MEMB.AGE,
AGE.GROUP
FROM MEMB,
AGE
WHERE MEMB.AGE = AGE.AGE

非常感谢。

最佳答案

您可以编写自己的 pretty-print 。在这种情况下,它不会有任何困难。只需替换如下内容:

"FROM" -> "\nFROM"
"WHERE" -> "\nWHERE"
"," -> ",\n\t"
"AND" -> "AND\n\t"
"OR" -> "OR\n\t"

等等

编辑:由于您不编写代码,因此这里是此功能的一个小版本。

#include <string>
using std::string; /* put these lines in the top of your file */

string replace(string a, string b, string c) {
unsigned x;
for(x = a.find(b); x != string::npos;) {
a.erase(x, b.length());
a.insert(x, c);
}
return a;
}




string formatSQL(string sql) {

replace(sql, "FROM", "\nFROM");
replace(sql, "WHERE", "\nWHERE");
replace(sql, "," , ",\n\t");
replace(sql, "AND", "AND\n\t");
replace(sql, "OR", "OR\n\t");
}

所以调用 formatSql("SELECT MEMB.NAME, MEMB.AGE, AGE.GROUP FROM MEMB, AGE WHERE MEMB.AGE = AGE.AGE") 会给你想要的结果。

关于c++ - 在 C++ 中格式化 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5939613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com