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?










share|improve this question























  • 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















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?










share|improve this question























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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













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






share|improve this answer
















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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






share|improve this answer
















  • 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














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






share|improve this answer
















  • 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












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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Ruanda

Makov (Slowakei)

Kleinkühnau