How to get a column from an sql table after running query
up vote
0
down vote
favorite
i have recently been writing a program for a simple login window as a part of a project i have been working on. I have an SQL database structured as follows:
User:
__________________________________________
| id | name | username | password | status |
| ___+______+__________+__________+________|
| 1 | niko | ******** | ******** | admin |
| 2 | andy | ******** | ******** | user |
------------------------------------------
and a few more columns that go on like that.
I have prepared an sql query in c++ in QTcreator as follows:
qry.prepare("SELECT name FROM Database.User WHERE username = :username AND password = :password")
which ideally is supposed to get either niko or andy or any other name for a given password and username. The only problem is i am not sure how to work with it. I have read the QT documentation multiple times and haven't found any way of obtaining the name as a string. I have tried to print it using:
qDebug<<qry.result()
however that only returns "0x5561eb32d240" I am not sure on what this is? Qt hasn't documented result() and the only thing i know about the return of result is that it is a const QSql type.
Any ideas on how may i be able to return name as a string for later use?
c++ sql qt qsqlquery
add a comment |
up vote
0
down vote
favorite
i have recently been writing a program for a simple login window as a part of a project i have been working on. I have an SQL database structured as follows:
User:
__________________________________________
| id | name | username | password | status |
| ___+______+__________+__________+________|
| 1 | niko | ******** | ******** | admin |
| 2 | andy | ******** | ******** | user |
------------------------------------------
and a few more columns that go on like that.
I have prepared an sql query in c++ in QTcreator as follows:
qry.prepare("SELECT name FROM Database.User WHERE username = :username AND password = :password")
which ideally is supposed to get either niko or andy or any other name for a given password and username. The only problem is i am not sure how to work with it. I have read the QT documentation multiple times and haven't found any way of obtaining the name as a string. I have tried to print it using:
qDebug<<qry.result()
however that only returns "0x5561eb32d240" I am not sure on what this is? Qt hasn't documented result() and the only thing i know about the return of result is that it is a const QSql type.
Any ideas on how may i be able to return name as a string for later use?
c++ sql qt qsqlquery
After that prepare instruction, in where is use SELECT, you bind parameters? So, you can have two statements like qry.bindValue(":username", username); qry..bindValue(":password", password); where username and password is two variables for proper values to substitute in SELECT.
– user1929959
Nov 9 at 22:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
i have recently been writing a program for a simple login window as a part of a project i have been working on. I have an SQL database structured as follows:
User:
__________________________________________
| id | name | username | password | status |
| ___+______+__________+__________+________|
| 1 | niko | ******** | ******** | admin |
| 2 | andy | ******** | ******** | user |
------------------------------------------
and a few more columns that go on like that.
I have prepared an sql query in c++ in QTcreator as follows:
qry.prepare("SELECT name FROM Database.User WHERE username = :username AND password = :password")
which ideally is supposed to get either niko or andy or any other name for a given password and username. The only problem is i am not sure how to work with it. I have read the QT documentation multiple times and haven't found any way of obtaining the name as a string. I have tried to print it using:
qDebug<<qry.result()
however that only returns "0x5561eb32d240" I am not sure on what this is? Qt hasn't documented result() and the only thing i know about the return of result is that it is a const QSql type.
Any ideas on how may i be able to return name as a string for later use?
c++ sql qt qsqlquery
i have recently been writing a program for a simple login window as a part of a project i have been working on. I have an SQL database structured as follows:
User:
__________________________________________
| id | name | username | password | status |
| ___+______+__________+__________+________|
| 1 | niko | ******** | ******** | admin |
| 2 | andy | ******** | ******** | user |
------------------------------------------
and a few more columns that go on like that.
I have prepared an sql query in c++ in QTcreator as follows:
qry.prepare("SELECT name FROM Database.User WHERE username = :username AND password = :password")
which ideally is supposed to get either niko or andy or any other name for a given password and username. The only problem is i am not sure how to work with it. I have read the QT documentation multiple times and haven't found any way of obtaining the name as a string. I have tried to print it using:
qDebug<<qry.result()
however that only returns "0x5561eb32d240" I am not sure on what this is? Qt hasn't documented result() and the only thing i know about the return of result is that it is a const QSql type.
Any ideas on how may i be able to return name as a string for later use?
c++ sql qt qsqlquery
c++ sql qt qsqlquery
edited Nov 9 at 21:44
eyllanesc
68.9k93052
68.9k93052
asked Nov 7 at 19:24
Niko
55
55
After that prepare instruction, in where is use SELECT, you bind parameters? So, you can have two statements like qry.bindValue(":username", username); qry..bindValue(":password", password); where username and password is two variables for proper values to substitute in SELECT.
– user1929959
Nov 9 at 22:24
add a comment |
After that prepare instruction, in where is use SELECT, you bind parameters? So, you can have two statements like qry.bindValue(":username", username); qry..bindValue(":password", password); where username and password is two variables for proper values to substitute in SELECT.
– user1929959
Nov 9 at 22:24
After that prepare instruction, in where is use SELECT, you bind parameters? So, you can have two statements like qry.bindValue(":username", username); qry..bindValue(":password", password); where username and password is two variables for proper values to substitute in SELECT.
– user1929959
Nov 9 at 22:24
After that prepare instruction, in where is use SELECT, you bind parameters? So, you can have two statements like qry.bindValue(":username", username); qry..bindValue(":password", password); where username and password is two variables for proper values to substitute in SELECT.
– user1929959
Nov 9 at 22:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This is the example with qsqlite. It have to be work.
QSqlDatabase data_base;
data_base = QSqlDatabase::addDatabase("QSQLITE", id);
data_base.setDatabaseName(data_base_path);
if (data_base.open())
QString query = "SELECT name FROM Database.User WHERE username = :username AND password = :password";
QSqlQuery SqlQuery = QSqlQuery( data_base );
SqlQuery.exec( query );
while (SqlQuery.next())
int field_idx = SqlQuery.record().indexOf("name");
QString name = SqlQuery.record().value( field_idx ).toString();
qDebug() << name;
;
1
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This is the example with qsqlite. It have to be work.
QSqlDatabase data_base;
data_base = QSqlDatabase::addDatabase("QSQLITE", id);
data_base.setDatabaseName(data_base_path);
if (data_base.open())
QString query = "SELECT name FROM Database.User WHERE username = :username AND password = :password";
QSqlQuery SqlQuery = QSqlQuery( data_base );
SqlQuery.exec( query );
while (SqlQuery.next())
int field_idx = SqlQuery.record().indexOf("name");
QString name = SqlQuery.record().value( field_idx ).toString();
qDebug() << name;
;
1
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
add a comment |
up vote
1
down vote
accepted
This is the example with qsqlite. It have to be work.
QSqlDatabase data_base;
data_base = QSqlDatabase::addDatabase("QSQLITE", id);
data_base.setDatabaseName(data_base_path);
if (data_base.open())
QString query = "SELECT name FROM Database.User WHERE username = :username AND password = :password";
QSqlQuery SqlQuery = QSqlQuery( data_base );
SqlQuery.exec( query );
while (SqlQuery.next())
int field_idx = SqlQuery.record().indexOf("name");
QString name = SqlQuery.record().value( field_idx ).toString();
qDebug() << name;
;
1
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This is the example with qsqlite. It have to be work.
QSqlDatabase data_base;
data_base = QSqlDatabase::addDatabase("QSQLITE", id);
data_base.setDatabaseName(data_base_path);
if (data_base.open())
QString query = "SELECT name FROM Database.User WHERE username = :username AND password = :password";
QSqlQuery SqlQuery = QSqlQuery( data_base );
SqlQuery.exec( query );
while (SqlQuery.next())
int field_idx = SqlQuery.record().indexOf("name");
QString name = SqlQuery.record().value( field_idx ).toString();
qDebug() << name;
;
This is the example with qsqlite. It have to be work.
QSqlDatabase data_base;
data_base = QSqlDatabase::addDatabase("QSQLITE", id);
data_base.setDatabaseName(data_base_path);
if (data_base.open())
QString query = "SELECT name FROM Database.User WHERE username = :username AND password = :password";
QSqlQuery SqlQuery = QSqlQuery( data_base );
SqlQuery.exec( query );
while (SqlQuery.next())
int field_idx = SqlQuery.record().indexOf("name");
QString name = SqlQuery.record().value( field_idx ).toString();
qDebug() << name;
;
answered Nov 9 at 22:38
EzR1d3r
426
426
1
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
add a comment |
1
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
1
1
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
when defining field_idx as a SqlQuery.record() type, it returns an error saying "calling 'record' with incomplete return type QsqlRecord"?
– Niko
Nov 14 at 19:55
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
Perhaps, you have to #include <QSqlRecord>
– EzR1d3r
Nov 15 at 13:22
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
That did seem to work however there is one problem. It does not print anything to the console.
– Niko
Nov 16 at 18:39
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
Never mind, it worked perfectly. Thanks!
– Niko
Nov 16 at 18:49
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196410%2fhow-to-get-a-column-from-an-sql-table-after-running-query%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
After that prepare instruction, in where is use SELECT, you bind parameters? So, you can have two statements like qry.bindValue(":username", username); qry..bindValue(":password", password); where username and password is two variables for proper values to substitute in SELECT.
– user1929959
Nov 9 at 22:24