QSqlQuery row affected result - c++

SHA256 sha256;
std::string s = sha256(pass.toStdString());
QString myquery = "declare #identifier nvarchar(100) = NEWID()\
declare #user_name nvarchar(50) = '"+user+"'\
declare #pass_word nvarchar(100) = '"+QString::fromStdString(s)+"'\
declare #hint nvarchar(50) = '"+hint+"'\
if NOT exists(select * from user_table where (userid=#identifier or username = #user_name))\
insert into user_table (username,password,password_salt,userid) values(#user_name,#pass_word,#hint,#identifier)";
qDebug()<<myquery;
openSqlConnection();
QSqlQuery q3;
q3.exec(myquery);
After executing this query, I should see result which will be (1 row(s) affected) if executed successfully.
If username already exists in the database, the result will be
Command(s) completed successfully.
To see the result from the select I use q3.next().
How do I know that my query has been executed successfully?

How do I know that my query has been executed successfully?
You have a number of options here. You could check either:
The returned value of QSqlQuery::exec
Returns true and sets the query state to active if the query was successful; otherwise returns false.
for example:
if (!q3.exec(myquery))
\\ show error
The status of the query, i.e. QSqlQuery::isActive
An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.
for example:
q3.exec(myquery);
if (!q3.isActive())
\\ show error
The type of QSqlQuery::lastError
QSqlError::NoError 0 No error occurred.
for example:
q3.exec(myquery);
if (q3.lastError().type() != QSqlError::NoError)
\\ show error
If you pass the check (the one you have selected), you could process your query further, e.g. to see if the username already exists and how many rows have been affected.

Related

How to handle error in ibm_db python package while calling stored procedure?

I'm trying call stored procedure using following code
conn = ibm_db.connect("database","username","password")
sql = "CALL DB2INST1.KPI_VALIDATE()"
stmt = ibm_db.exec_immediate(conn, sql)
But this procedure does not return any rows & It will only return code. Now I need to handle error whether procedure run successfully or not. Could anyone help me how to handle this?
Thanks
For test purposes, I've created a table:
db2 "create table so(c1 int not null primary key)"
and my procedure will simply insert a row into this table - this will allow me to easily force an error with a duplicate key:
db2 "create or replace procedure so_proc(in insert_val int)
language sql
insert into so values(insert_val)"
db2 "call so_proc(1)"
Return Status = 0
db2 "call so_proc(1)"
SQL0803N One or more values in the INSERT statement, UPDATE statement, or
foreign key update caused by a DELETE statement are not valid because the
primary key, unique constraint or unique index identified by "1" constrains
table "DB2V115.SO" from having duplicate values for the index key.
SQLSTATE=23505
now with Python:
conn = ibm_db.connect("DATABASE=SAMPLE;HOSTNAME=localhost;PORT=61115;UID=db2v115;PWD=xxxxx;","","")
stmt = ibm_db.exec_immediate(conn, "CALL SO_PROC(2)")
stmt = ibm_db.exec_immediate(conn, "CALL SO_PROC(2)")
Exception Traceback (most recent call last)
<ipython-input-8-c1f4b252e70a> in <module>
----> 1 stmt = ibm_db.exec_immediate(conn, "CALL SO_PROC(2)")
Exception: [IBM][CLI Driver][DB2/LINUXX8664] SQL0803N One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "DB2V115.SO" from having duplicate values for the index key. SQLSTATE=23505 SQLCODE=-803
so if a procedure hits an exception then you'll get it, you just need to handle exception Try/Except block:
try:
stmt = ibm_db.exec_immediate(conn, "CALL SO_PROC(2)")
except Exception:
print("Procedure failed with sqlstate {}".format(ibm_db.stmt_error()))
print("Error {}".format(ibm_db.stmt_errormsg()))
Procedure failed with sqlstate 23505
Error [IBM][CLI Driver][DB2/LINUXX8664] SQL0803N One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "DB2V115.SO" from having duplicate values for the index key. SQLSTATE=23505 SQLCODE=-803
Or you are actually interested with CALL return code/status? E.g.:
create or replace procedure so_proc_v2(in insert_val int)
language sql
if not exists (select 1 from so where c1 = insert_val)
then
insert into so values(insert_val);
return 0;
else
return -1;
end if#
test:
db2 "call so_proc_v2(10)"
Return Status = 0
db2 "call so_proc_v2(10)"
Return Status = -1
then this is a bit tricky. With CLI trace enabled (I have ibm_db installed in my local path so it fetched CLI package there too):
export LD_LIBRARY_PATH=$HOME/.local/lib/python3.7/site-packages/clidriver/lib/
$HOME/.local/lib/python3.7/site-packages/clidriver/bin/db2trc on -cli -f /tmp/cli/trc
<run_code>
$HOME/.local/lib/python3.7/site-packages/clidriver/bin/db2trc off
$HOME/.local/lib/python3.7/site-packages/clidriver/bin/db2trc fmt -cli /tmp/cli.trc /tmp/cli.fmt
trace does show the returns status:
SQLExecute( hStmt=1:8 )
---> Time elapsed - -7.762688E+006 seconds
( Row=1, iPar=1, fCType=SQL_C_LONG, rgbValue=10 )
( return=-1 )
( COMMIT REQUESTED=1 )
( COMMIT REPLY RECEIVED=1 )
but I don't see anywhere in python-ibmdb API a way to fetch it... (e.g. ibm_dbcallproc doesn't have such option). Which means, that unless I'm missing something, you would have to raise an issue on Github to extent the API

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");

trying to insert the data to the specific column and the row in query

I have a 2nd query where the column names are appearing and I want to insert the data in the main query. I have managed to bring all the columns of 2nd query to the main query, but the data is empty for all newly added columns.
Now I am trying to loop over the first query and trying to find the uuid which exists to insert the specific data at the specific column it finds and at the specific row based upon the uuid search. This is my try as of now:
<cfset lstusers = '51840915-e570-430d-9911-7247d076f6e7,5200915-g675-430d-9911-7247d076f6e7,56674915-e570-430d-9911-7247d076f6e7,2134563-e570-430d-9911-7247d076f6e7'>
<cfloop query="quserList">
<cfdump var="#quserList.uuid[currentRow]#">
<cfif ListContainsNoCase(lstusers,quserList.uuid[currentRow])>
<cfset QuerySetCell(quserList,"BUFFEREDRANGENOTES","name",quserList[uuid][currentRow])>
<cfset QuerySetCell(quserList,"BufferNotes","name2",quserList[uuid][currentRow])>
</cfif>
</cfloop>
</cfif>
However, it is giving me an error on the quserList[uuid][currentRow] line that says not indexable by the data:
coldfusion.sql.QueryColumn#276249a2] ] is not indexable by
51840915-e570-430d-9911-7247d076f6e7
If I try it in other way:
quserList.uuid[currentRow]
I still get an error, but it says "cannot convert to int ...". How do I fix it?
Update:
In image 1, I am doing a create column for all the above 1st query product_types into the main query and based upon the userid of 1st query and uuid of second query. I want to insert data in correct location and correct row for the user based upon uuid and userid match. Image 2 is the uuid in the second table:
In both the queries, the userid you see in the first section is common. Meaning that the same usedid exists below that tells us that this user has completed these trainings. Now I want the first query to get merged in second one so it should add correct data to the correct row and that is what messing me up.
SQL:
Query #1:
SELECT ct.trainingid,
ct.userid,
ct.trainingtype,
ct.trainingstatus,
ct.trainingscore,
ct.trainingdate,
dbo.Fn_stripcharacters(ctt.product_type, '^a-z0-9') AS product_type,
ctt.product_type AS oldName
FROM clienttraining AS ct
INNER JOIN clienttraningtypes AS ctt ON ct.trainingtype = ctt.typeid
WHERE 1 = 1
AND userid IN (
'51840915-e570-430d-9911-7247d076f6e7'
, '51927ada-6370-4433-‌​8a06-30d2d076f6e7'
)
AND trainingtype IN (
SELECT typeid
FROM complaincetestlinks
WHERE pid = 1039
AND isactive = 1
AND isdeleted = 0
)
Query 2:
SELECT id,
NAME,
username,
email,
password,
first_name,
last_name,
usertyp‌​e,
block,
sendemail,
registerdate,
lastvisitdate,
activation,
params‌​,
uuid
FROM users
WHERE uuid IN (
'51840915-e570-430d-9911-7247d076f6e7'
, '51912193-6694-4ca5-‌​94c9-9f31d076f6e7'
, '51927ada-6370-4433-8a06-30d2d076f6e7'
, '51c05ad7-d1d0-4eb6-b‌​c6b-424bd076f6e7'
, 'd047adf1-a6af-891e-94a2d0b225dcd1b6'
, '2aba38f2-d7a7-0a7a-ef‌​f2be3440e3b763'
)
Update:
Looking at it again with fresh eyes, it still seems like maybe you are over-complicating things? A simple JOIN should return the information needed, ie All users and the completed training info (if any).
Runnable SQLFiddle
SELECT u.id
, u.first_name
, u.last_name
, ct.trainingid
, ct.userid
, ct.trainingtype
, ct.trainingstatus
, ct.trainingscore
, ct.trainingdate
, ctt.product_type
, ctt.product_type AS oldName
FROM users u
LEFT JOIN clientTraining AS ct ON ct.UserID = u.UUID
LEFT JOIN clientTraningTypes AS ctt ON ct.trainingtype = ctt.typeid
LEFT JOIN (
SELECT typeID
FROM complainceTestLinks
WHERE parent_client_id = 1039
AND isactive = 1
AND isdeleted = 0
) ctl ON ctl.TypeID = ct.trainingType
WHERE u.uuid IN
(
'51840915-e570-430d-9911-7247d076f6e7'
, '51912193-6694-4ca5-94c9-9f31d076f6e7'
, '51927ada-6370-4433-8a06-30d2d076f6e7'
, '51c05ad7-d1d0-4eb6-bc6b-424bd076f6e7'
, 'd047adf1-a6af-891e-94a2d0b225dcd1b6'
, '2aba38f2-d7a7-0a7a-eff2be3440e3b763'
)
ORDER BY last_name, first_name, product_type
;
How you want to present the information, on the front end, is different question. For example, you could use a <cfoutput group="..."> to only display each user's name once, and a list of completed training courses beneath it (see below). If you need more specific advice, please post an example of the desired output.
Smith, John
Course 1
Course 2
Allen, Mark
Course 2
Course 3
...
coldfusion.sql.QueryColumn#276249a2] ] is not indexable by
51840915-e570-430d-9911-7247d076f6e7
That just means you are referencing a column name that does not exist. By omitting the quotes around uuid here quserList[uuid][currentRow], you are actually passing in the variable value as the column name, NOT the literal string "UUID". Obviously the query does not contain a column named "51840915-e570-430d-9911-7247d076f6e7". Hence the error.
it says cannot convert to int
That is pretty self explanatory. You are trying to populate a numeric column with a non-numeric value. Clearly the UUID string, ie "51840915-e570-430d-9911-7247d076f6e7" is not an integer. Either you are using the wrong value or need to change the column type.
It may be related to the fact that your QuerySetCell call is passing in the wrong parameters. The third and fourth parameter should be the "value" and query "row number". However, your code is passing in a hard coded string for "value" and the UUID string instead of a row number
QuerySetCell(quserList,"BUFFEREDRANGENOTES","name",quserList[uuid][currentRow])
That said, technically you do not even need that function. Just use associative array notation to "set" the values, ie <cfset queryName["columnName"][currentRow] = "some value here">
<cfif ListContainsNoCase(lstusers,quserList.uuid[currentRow])>
Nothing to do with the error, but ListContainsNoCase is the wrong function here, as it searches for partial matches. To match whole elements only, use ListFindNoCase.

App connection with Sqlite in my app developed in qt

In my app in QT5 I have this code
QString sql = "Select * from table";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./au.sqlite");
db.open();
QSqlQuery query(sql);
query.exec();
But when I get the results only get one result, the select query only give me one result and I don't know how fix this. If I add query.next() in a while loop I only get one iteration.
There is a bool function that returns true if the query succeeded. Looking back to the docs:
Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive.
This is the way you can check the result. If it's false, then You should try to rewrite your sql query text so that functional sql words (such as SELECT, FROM and etc.) are in the upper case (the way it's shown in the docs) and try to execute the query once again.
Here's an example from the docs again:
QSqlQuery query("SELECT country FROM artist");
while (query.next()) {
QString country = query.value(0).toString();
doSomething(country);
}
You might check sqlbrowser example that is included with Qt5 distribution. Run your query there on your db.

How to catch Unique constraint error in POSTGRESQL

If I create a table with a unique contraint, for example:
CREATE TABLE distributors (
did integer,
name varchar(40) UNIQUE
);
What would happen if I try to enter an entry with the name that already exists. I tried to do so and it just quit without displaying any error message. Is there a way to check whether a new entry was actually inserted?
If the insert failed than there should be error code set somewhere, readable by some method of the interface you're using - more details are definitely in documentation to your access library/module.
Alternatively you can change your insert to:
INSERT INTO distributors (did, name) values ( ... ) RETURNING did;
And if it didn't return anything - there has been error.
If you try to insert record with name that already exists, you'll receive error message like this:
ERROR: duplicate key value violates unique constraint "distributors_name_key"
DETAIL: Key (name)=(aaa) already exists.
and record will not be inserted.
If you do it from allplcation level, the exception will be thrown with a message similar to this. It's up to the programmer how to handle this exception.
If your ID field is autogenerating (SERIAL or BIGSERIAL), and you insert just name, if you insert name which already exists, ID sequence will increase by 1, even if you didn't insert any record.
To avoid that you can make "SELECT" query before INSERT to check, it the record already exists. Possible to do all in one transaction, in pseudo-code:
BEGIN TRANSACTION;
int records = SELECT name FROM table WHERE name = 'aaa' FOR UPDATE; //FOR UPDATE to lock the row from being read by other user until transaction finishes.
if (records == 0)
INSERT INTO table VALUES (1, 'aaa');
else
MessageBox.Show("Record already exists");
COMMIT TRANSACCTION;