Sqlite 查询问题

发布于 2024-11-29 03:23:52 字数 1078 浏览 1 评论 0原文

好吧,所以我正在尝试将数据存储在 Sqlite 中。所以我试图用“line”来存储用户的id。然而,where 子句似乎让我失望了。

    String query = "SELECT * FROM Users WHERE username='Fellixombc";
    ResultSet result = this.sqlStatement.executeQuery(query);
    int userId = 0;
    while(result.next()) {
        System.out.println("TRUE");
        userId = result.getInt("id");
        System.out.println(result.getString("username"));
    }

真正的不是印刷。然而,如果我从查询中删除 WHERE 子句,它会打印出所有用户名/id,当然还有“TRUE”。

我是否遗漏了 Sqlite 及其语法的某些内容?

编辑:只是为了澄清“用户”列中有一个 id 为 1、用户名为 Fellixombc 的用户 编辑:所以我接受了你的建议并尝试准备声明,现在是我的代码:

        PreparedStatement sqlStatement = this.sqlConnection.prepareStatement("SELECT * FROM Users WHERE username=?");
        sqlStatement.setString(1, "Fellixombc");
        ResultSet result = sqlStatement.executeQuery();
        int userId = 0;
        while(result.next()) {
            System.out.println("TRUE");
            userId = result.getInt("id");
            System.out.println(result.getString("username"));
        }
        result.close();

Alright, so I'm trying to store data in Sqlite. So I'm trying to store the id of the user with the a "line". Yet, it seems that the where clause is failing me.

    String query = "SELECT * FROM Users WHERE username='Fellixombc";
    ResultSet result = this.sqlStatement.executeQuery(query);
    int userId = 0;
    while(result.next()) {
        System.out.println("TRUE");
        userId = result.getInt("id");
        System.out.println(result.getString("username"));
    }

And true is not printing. Yet, if I remove the WHERE clause from the query, it will print out all of the usernames/id's fine, and of course "TRUE".

Am I missing something with Sqlite and it's syntax?

edit: Just to clarify there is a user in the Users column with the id of 1 and with the username Fellixombc
edit: So I took your guy's suggestion and tried prepare statement, heres my code now:

        PreparedStatement sqlStatement = this.sqlConnection.prepareStatement("SELECT * FROM Users WHERE username=?");
        sqlStatement.setString(1, "Fellixombc");
        ResultSet result = sqlStatement.executeQuery();
        int userId = 0;
        while(result.next()) {
            System.out.println("TRUE");
            userId = result.getInt("id");
            System.out.println(result.getString("username"));
        }
        result.close();

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

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

发布评论

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

评论(2

小女人ら 2024-12-06 03:23:52

本质上,您想存储用户名,对吗?

您当前的查询将检索与用户名相关的所有信息。这就是 SELECT 查询的作用,它进行检索。

如果您需要创建新用户,请使用:

INSERT INTO Users (username) Values ('Fellixombc');

我应该告诉您,您可能需要在此查询中提供更多信息,具体取决于您的用户表具有哪些其他字段。

编辑:

在您的选择语句中,您有一个开放的单引号。

"SELECT * FROM Users WHERE username='Fellixombc"

为了

"SELECT * FROM Users WHERE username='Fellixombc'"

避免 SQL 注入问题,请考虑 prepareStatement 函数。

Essentially, you want to store the username, correct?

Your current query will retrieve all information on relative to a username. It's what a SELECT query does, it retrieves.

If you need to create a new user, use:

INSERT INTO Users (username) Values ('Fellixombc');

I should tell you that you might need to provide more information in this query, depending on what other fields your Users table has.

EDIT:

In your select statement, you have an open single quote.

"SELECT * FROM Users WHERE username='Fellixombc"

should be

"SELECT * FROM Users WHERE username='Fellixombc'"

To avoid issues with SQL injection, consider the prepareStatement function.

故人如初 2024-12-06 03:23:52

尝试修剪您的用户名字段。数据中的某个位置可能存在虚假空间,导致您的 where 子句失败。

"SELECT * FROM Users WHERE trim(username) ='Fellixombc'"

另外,同意@MPelletier。您绝对应该使用PreparedStatements。

Try doing a trim on your username field. Its possible there's a spurious space somewhere in the data thats causing your where clause to fail.

"SELECT * FROM Users WHERE trim(username) ='Fellixombc'"

Also, agree with @MPelletier. You should definitely be using PreparedStatements.

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