Qt Query Top Number Record Selecting - c++

I have a query that should request the top X number of records using the Qt framework to actually make the request to the SQL database. I have verified when I place an hard-coded number the query is successful, but when I attempt to bind to it I get an error.
query.prepare("SELECT TOP :numberToSelect"
" deviceId"
" , latitude"
" , longitude"
" , [timeStamp]"
" FROM Positions "
" WHERE [address] = ''"
" ORDER BY [timeStamp] DESC");
query.bindValue(":numberToSelect", numberMissing);
The variable numberMissing is an unsigned short which is passed in. Upon execution I receive this error:
Unable to execute statement: "[Microsoft][ODBC SQL Server Driver][SQL
Server]Incorrect syntax near '#P1'. [Microsoft][ODBC SQL Server
Driver][SQL Server]Statement(s) could not be prepared. QODBC3: Unable
to execute statement" "SELECT TOP ? deviceId , latitude ,
longitude , [timeStamp] FROM Positions WHERE [address] = ''
ORDER BY [timeStamp] DESC"
I do not see what the error would be.

Oracle parameters are signified with a preceding : - SQLServer's closest equivalent would be an # sign. Try changing :numberToSelect to #numberToSelect.

When you do a select top with a variable, the top value needs to be in parenthesis.
Try this:
query.prepare("SELECT TOP (:numberToSelect)"
" deviceId"

Related

QuickSight could not generate any output column after applying transformation Error

I am running a query that works perfectly on AWS Athena however when I use athena as a data source from quicksight and tries to run query it keeps on giving me QuickSight could not generate any output column after applying transformation error message.
Here is my query:
WITH register as (
select created_at as register_time
, serial_number
, node_name
, node_visible_time_name
from table1
where type = 'register'),
bought as (
select created_at as bought_time
, node_name
, serial_number
from table1
where type= 'bought')
SELECT r.node_name
, r.serial_number
, r.register_time
, b.bought_time
, r.node_visible_time_name
FROM register r
LEFT JOIN bought b
ON r.serial_number = b.serial_number
AND r.node_name = b.node_name
AND b.bought_time between r.deploy_time and date(r.deploy_time + INTERVAL '1' DAY)
LIMIT 11;
I've did some search and found similar question Quicksight custom query postgresql functions In this case adding INTERVAL '1' DAY had the problem. I've tried other alternatives but no luck. Furthermore running query without it still outputs same error message.
No other lines seems to be getting transformed in any other way.
Re-creating dataset and running exact same query works.
I think queries that has been ran on existing dataset transforms the data. Please let me know if anyone knows why this is so.

Error Converting UNIX time to Date in PowerBI

I'm using "SQL Server" for Power BI as a source of data. The problem is to convert this back to date on Power BI side.
I’m getting the following message:
Expression.Error: We cannot convert the value " 1205751600" to type Number.
Details:
Value= 1205751600
Type=Type
There's a space at the front of the string: " 1205751600". The value should be trimmed first, eg with Text.Trim or Text.TrimStart

Qt Sql not able to bind variables to QSqlQuery prepare Statement

I am trying to fetch some records from MySQL database by using a prepared statement by using QSqlQuery as:
QString username=ui->textEdit_password->toPlainText();
QString password=ui->textEdit_password->toPlainText();
QSqlQuery query;
query.prepare("SELECT * FROM login_access WHERE username=? AND password=?");
query.addBindValue(username);
query.addBindValue(password);
query.exec();`
When i run :
std::string q_str1=query.executedQuery().toUtf8().constData();
std::cout<<"Query : "<<q_str1<<"\n";
It outputs : Query : SELECT * FROM login_access WHERE username=? AND password=? where the "?" has not been replaced and the query returns nothing since the "?" character is compared to the database records.
On running the query: SELECT * FROM login_access, the query returns all the database records in the login_access table.
I have also tried replacing the "?" with placeholders ":uname",":pass" and changed query.addBindValue(username); to query.bindValue(":uname",username);, and done same with password field.
I am running QtCreator 4.4.1
Thanks.
Use query.bindValue( ...) because this sets the placeholder value.
I tested executedQuery() on one of my SQL statements with placeholders and it returned a string with just the placeholders, not the values. The documentation does say that in most cases it the same string as lastQuery().
http://doc.qt.io/qt-5/qsqlquery.html#executedQuery
You have confirmed that your SQL statement without the where clause works so the next stage is to check you are binding what you think you are binding. To do this use boundValue(const QString placeholder) to find out if the placehold value is being bound.
It might also be useful to check the query has run OK.
So, after your query.exec you should put the following (assuming these are your placeholders) just to check these things:
qDebug() << query.lasterError();
qDebug() << query.boundValue(":uname");
qDebug() << query.boundValue(":pass");

SQLite3 C++ Query near "CASE": syntax error

Using SQLite3 C++ implementation though I believe this error is more general.
I'm simply trying to do a statement that says:
"If the column 'lastPlayed' does not exist in the table 'Players', ALTER the table and add it to said table"
Here's my query:
query = string("CASE WHEN COL_LENGTH('Players', 'lastPlayed') IS NULL ")
+ " BEGIN "
+ "ALTER TABLE Players "
+ "ADD lastPlayed DATETIME DEFAULT now"
+ " END "
;
The error:
near "CASE": syntax error
What am I doing wrong here?
Your CASE statement is fine. The error is in the fact that COL_LENGTH is not an inbuilt function in SQLite.
What you can do to achieve your result is remove the CASE statement and in your code, just attempt to alter the Players table to add a lastPlayed column. Then wrap your alter statement in a try-catch block. When the column already exists, the exception will be thrown, otherwise the table will be altered successfully and you can proceed.
Try this syntax
CASE
WHEN expr THEN expr
WHEN expr THEN expr
.
.
END
https://www.sqlite.org/lang_expr.html

problems with django raw query parameters

I was trying to make a custom raw query for a model and i get this error:
DatabaseError: error de sintaxis en o cerca de
«E'positions_statusrecord'» LINE 1: SELECT id FROM
E'positions_statusrecord' WHERE "type"=E'Leav...
Here is the raw query i'm trying to use (it's a manager method):
def get_status_ids(self, model):
"""
This query returns the latest statuses ids for each entry
of "model" in StatusRecord.
"""
db_table = self.model._meta.db_table
model_type = model.get_status_type()
raw_query = (
"SELECT id "
"FROM %s "
"WHERE \"type\"=%s AND "
"(identifier,date_time) IN "
"( "
"SELECT identifier, Max(date_time) "
"FROM %s "
"WHERE \"type\"=%s "
"GROUP BY identifier"
")"
)
params = (db_table, model_type, db_table, model_type, )
return self.raw(raw_query, params)
I've tried with a simpler query (just a SELECT ... FROM ..) and had the same problem. It seems to be that raw queries couldn't have the FROM part completed with a parameter.
Am i right? or have i made a mistake and i'm not finding it?
I'm using postgreSQL 8.4.10, django 1.3 and python 2.6.
I've searched information about raw queries parameters to see if there are some forbidden formatting options, but i didn't find anything that helps me.
Does anyone knows what's causing this error?
It seems to be that raw queries couldn't have the "FROM" part completed with a parameter.
Yes, the ORM will quote the string argument making it ... FROM 'some_table' ....
Since the db_table parameter is trusted (not originated from user input), you can do something like:
raw_query = (
"SELECT id "
"FROM %s "
"WHERE \"type\"=%%s AND "
"(identifier,date_time) IN "
"( "
"SELECT identifier, Max(date_time) "
"FROM %s "
"WHERE \"type\"=%%s "
"GROUP BY identifier"
")"
) % (db_table, db_table)
return self.raw(raw_query, (model_type, model_type))
Since Django Aggregation can be used in filters, probably you can get the same results without resorting to raw SQL (look also the in lookup example).
Look if you can replace raw() with QuerySet.extra() and SQL IN with EXISTS.