When running this query using dart and the postresql driver
getPost(int limit, int offset, String order_by){
connect(uri).then((conn){
conn.query('''select * from posts
order by @order_by
limit @limit offset @offset''', {'limit': limit, 'offset': offset, 'order_by': order_by})
.toList()
.then((rows){
print(rows);
})
.whenComplete(() => conn.close());
})
.catchError((err) => print('Error in getPost: $err'));
}
I get the Error: 42601 non-integer constant in ORDER BY
.
The code above is a method of a helper class. I run it with the following code.
dbUtil.getPost(10, 10, "posted_at");
I read here that postgresql expects a string literal. That's why I also tried this code dbUtil.getPost(10, 10, r"posted_at");
with no success.
If I replace @order_by
with posted_at
the query returns the values correctly.
Does anybody know how to solve this problem?
Working code inspired by the answer below.
getPost(int limit, int offset, String order_by){
connect(uri).then((conn){
var sb = new StringBuffer();
sb.write("select * from posts ");
sb.write(order_by);
sb.write(" limit @limit offset @offset");
String query = sb.toString();
conn.query(query, {'limit': limit, 'offset': offset})
.toList()
.then((rows){
print(rows);
})
.whenComplete(() => conn.close());
})
.catchError((err) => print('Error in getPost: $err'));
}