How to create a simple class in QT ?
-
Hi,
Then create a console application using the same code to connect to the database.
-
GUI itself should not guarantee database connection; the business logic classes should. If you need help with connecting to a MySQL database show us some code you have come up with so far.
-
I try this function to make the connection:
@
bool connectDatabase()
{
QSqlDatabase db = SqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("evoting");
db.setUserName("root");
db.setPassword("");
if (!db.open())
{
return false; }
return true;
}
@It shows me this error : QSqlDatabase was not declared in this scope
-
You need to include QSqlDatabase as well as change your .pro file to reflect such library usage:
@ QT += sql @ -
I try this function to make the connection:
@
bool connectDatabase()
{ QSqlDatabase db = SqlDatabase::addDatabase(“QMYSQL”);
db.setHostName(“localhost”);
db.setDatabaseName(“evoting”);
db.setUserName(“root”);
db.setPassword(”“);
if (!db.open()) {
return false; }
return true;
}
@
It shows me this error : QSqlDatabase was not declared in this scope -
Indeed. It is QSqlDatabase that is the name of the class, not SqlDatabase.
-
I have another problem :(
I made a query that will do the comparison for the voter id that a voter has in database and the id that the voter inputs.
the code is :
@
QSqlQuery query;
query.prepare("SELECT* FROM evoting WHERE v_id =? ");
query.addBindValue.....???
query.exec();
if (query.size()>0)
{
query.next();
cout<<"ID is correct";
}
else
{ cout<<"Incorrect ID";
}
@The problem is that I dont know how to take the value of Id from my constructor and put it to the query to make the comparison..
Please help me -
It would be appropriate for you to create another forum thread for this different issue. This way, the moderators will live longer and anyone with similar issues might find such a thread easier. I am not sure about the scope of this forums, though - the question you ask is isolated and has no ties to Qt.
Basically, if you want to execute a SELECT query for a given value, you compare this value with the name of the table's column:
@SELECT * FROM evoting WHERE v_id = found_id;@Please also do use "@ for code wrapping":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 whenever you want to paste a block of code into your post. :-)