如何将自然语言转换为sql查询?不精确模糊查询。SQLf

发布于 2024-10-24 03:10:00 字数 378 浏览 2 评论 0原文

我想实现一个功能,允许用户输入类似这样的内容:

"select * from products where low(price);" 

什么等于:

"select * from products where (price >=0) And (price <= 50);"

您可能知道对我有用的任何解决方案吗?我的第二个问题是我应该在应用程序结构中的哪里进行转换?在应用程序代码中还是在数据库中?

我的应用程序是用 C# 编写的,并通过 ADO.NET 连接到 SQL Server 2008。

如果有任何提示、伪代码等,我将非常感激

。提前致谢!

I`d like to implement a feature, that will allow user to type something like this:

"select * from products where low(price);" 

what is equal to:

"select * from products where (price >=0) And (price <= 50);"

do you maybe know any solutions that will be useful for me ? My second question is where in the application structure should I transform it ? In the application code or somehow in the database ?

My app is written in C# and connects to SQL Server 2008 via ADO.NET.

I would be very greatful for any hints, pseudocode, etc.

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-10-31 03:10:00

SQL Server 允许您定义用户定义的函数(请参阅例如本文)。如果您定义了一个函数low,那么您编写的第一个代码将是一个完全有效的 SQL 查询,并且您根本不需要进行任何预处理。声明大致如下:

 CREATE FUNCTION low(@price)
 RETURNS boolean AS
 BEGIN
   RETURN (@price >= 0) AND (@price <= 50)
 END

如果您希望允许更多模糊语言而不仅仅是函数调用,那么这将是另一个(并且明显更复杂)问题。我不知道有哪个图书馆可以做到这一点,并且您自己实现它可能是一个很大的挑战。 (您可能需要添加更多示例,以便我们明白您的意思)。

当然,如果您允许用户编写原始 SQL 查询,则该用户应该是您可以信任的人(因为他们可以轻松删除数据库中的所有数据)。

SQL Server allows you to define user-defined functions (see for example this article). If you defined a function low then the first code you wrote would be a perfectly valid SQL query and you wouldn't need to do any pre-processing at all. The declaration would look roughly like this:

 CREATE FUNCTION low(@price)
 RETURNS boolean AS
 BEGIN
   RETURN (@price >= 0) AND (@price <= 50)
 END

If you want to allow more fuzzy language than just function calls, then that would be another (and significantly more complicated) problem. I'm not aware of any library that does that and implementing that yourself could be quite a challange. (You may want to add more examples, so that we can see what you mean).

Of course, if you allow the user to write raw SQL queries, the user should be someone you can trust (because they can easily drop all data from your database).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文