Sqlite 查询问题
好吧,所以我正在尝试将数据存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本质上,您想存储用户名,对吗?
您当前的查询将检索与用户名相关的所有信息。这就是 SELECT 查询的作用,它进行检索。
如果您需要创建新用户,请使用:
我应该告诉您,您可能需要在此查询中提供更多信息,具体取决于您的用户表具有哪些其他字段。
编辑:
在您的选择语句中,您有一个开放的单引号。
为了
避免 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:
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.
should be
To avoid issues with SQL injection, consider the prepareStatement function.
尝试修剪您的用户名字段。数据中的某个位置可能存在虚假空间,导致您的 where 子句失败。
另外,同意@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.
Also, agree with @MPelletier. You should definitely be using PreparedStatements.