我正在尝试从Eclipse运行以下查询:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
String name = session.getAttribute("user").toString();
String query = "SELECT DISTINCT t.text, t.user, t.date"
+ " FROM users u, tweets t, follows f"
+ " Where t.parent is null"
+ " AND u.id ="+name
+ " AND ( f.follower = u.id"
+ " AND f.followed = t.user"
+ " OR t.user = u.id)"
+ " ORDER BY t.date DESC;";
但是我收到以下错误:
Sesion(user):Takeshi
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Takeshi' in 'where clause'
我可以想象该错误是因为我做错了u.id和名称之间的比较,但是我应该怎么做呢?有一些特殊的字符?提前致谢。
最佳答案
您能否使用以下代码段:
String name = session.getAttribute("user").toString();
name = name.replace("\'", "''");
String query = " SELECT DISTINCT t.text, t.user, t.date"
+ " FROM users u, tweets t, follows f"
+ " Where t.parent is null"
+ " AND u.id = '"+name+"'"
+ " AND ( f.follower = u.id"
+ " AND f.followed = t.user"
+ " OR t.user = u.id)"
+ " ORDER BY t.date DESC";