I'm currently working on my project within C++ using Qt. I have MySQL as database storage and the idea is to make a small messenger like MSN and Skype.
However, my MySQL queries are failing. It simply gets no results or gives me an error. There's loads of code coming up, I'm sorry for that.
This is my mysql.cpp which creates and opens the database connection:
#include "mysql.h"
mysql::mysql()
{
this->db = QSqlDatabase::addDatabase("QMYSQL", "QMYSQL");
this->db.setHostName("localhost");
this->db.setUserName("root");
this->db.setPassword("Eequi4");
this->db.setDatabaseName("test");
this->db.open();
}
mysql::~mysql()
{
}
mysql_result mysql::create_result(QString query)
{
return mysql_result(this->db.exec(query));
}
QSqlError mysql::error()
{
return this->db.lastError();
}
The connection is opened. That works correctly.
This is my mysql_result.cpp, the file I use to add parameters, insert, get results etc:
#include "mysql_result.h"
mysql_result::mysql_result(QSqlQuery query)
{
this->query = query;
}
void mysql_result::add_parameter(QVariant value)
{
this->query.addBindValue(value);
}
void mysql_result::add_parameter(QString key, QVariant value)
{
this->query.bindValue(key, value);
}
int mysql_result::get_last_id()
{
return this->query.lastInsertId().toInt();
}
void mysql_result::execute()
{
this->query.execBatch();
}
QSqlQuery mysql_result::get_query()
{
return this->query;
}
mysql_result::~mysql_result()
{
}
Okay, this should work. If I have the following code, it correctly returns all member first_name's from the database:
mysql_result res = _mysql->create_result("SELECT * FROM members");
QSqlQuery qry = res.get_query();
while (qry.next())
{
qDebug() << qry.value("first_name");
}
In member_controller.cpp (the class I use to retrieve members by name/id), I got this:
member* member_controller::get_member(int id)
{
mysql_result result = engine::get_mysql().create_result("SELECT * FROM members WHERE member_id = :ID");
result.add_parameter(":ID", id);
QSqlQuery query = result.get_query();
if (query.exec() && query.next())
{
return new member(id, query.value("first_name").toString(), query.value("second_name").toString(), query.value("screen_name").toString(), query.value("email").toString(), query.value("status").toString());
}
else
{
qDebug() << engine::get_mysql().error() << "\n";
qDebug() << query.lastError() << "\n";
}
return new member(0, "", "", "", "", "");
}
What it does it will go to the else, and I get the error invalid syntax near :ID. If I replace :ID with #ID (just like in C#), it will go to the else without error code.. I don't know what the problem is.
Two things. The code needs to be optimized a bit and made easier, I'm gonna work on that. Also, is it possible/allowed to put code in a pastebin and paste the URL rather than put the code here?
Try changing your query to this:
"SELECT * FROM members WHERE member_id = ?"
and add your param like this:
result.add_parameter(0, id);
I'd also suspect, if (query.exec() && query.next()) is incorrect, and the check for .next() should be removed as I'd imagine that requires another record to exist in the result set.
Related
I'm using a C++ interpreter (ROOT, the CERN framework) to access several mySQL tables in a loop. Every time I query a table that doesn't exist, the program quits on me:
for (int run = 19000; run < 22000; run ++) {
s << run;
num = s.str();
schema = "run_0"+num+"_R007";
s.str("");
//creating our query
query = "select distinct *whatever* from "+schema+".kTrack;";
res = conPtr->Query(query);
conPtr->Close();
//Here is where I don't know what to do:
if (*success*) {
do stuff
}
else {
do stuff
}
}
I don't have a problem if the table returns 0 rows, I have a problem if the table doesn't exist.
What is wrong with my code?
Assuming conPtr is a pointer to a TMySQLServer object, ROOT's documentation for TMySQLServer::Query() says:
Returns a pointer to a TSQLResult object if successful, 0 otherwise. The result object must be deleted by the user.
So Query() returns a NULL pointer on failure.
Also, since your loop is not re-opening a new DB connection on each iteration, you should not be calling conPtr->Close() until after you are done performing queries with it.
Try something more like this:
for (int run = 19000; run < 22000; run ++) {
s << run;
num = s.str();
schema = "run_0"+num+"_R007";
s.str("");
//creating our query
query = "select distinct *whatever* from "+schema+".kTrack;";
res = conPtr->Query(query);
if (res) {
// use res as needed...
delete res;
}
else {
// ...
}
}
conPtr->Close();
So, basically all this code does is search the database for a user with a certain username and password, and returns a nullptr if it doesn't find one.
Gebruiker* Gebruiker_DAO::getGebruiker(string login, string wachtwoord)
{
PreparedStatement *prep_stmt = nullptr;
ResultSet *res = nullptr;
Gebruiker* gebruiker = nullptr;
int rowcount = 0;
try {
prep_stmt = dbConn->getConnection()->prepareStatement("SELECT * FROM Gebruiker WHERE Gebruiker_login = ? AND Gebruiker_wachtwoord = ?");
prep_stmt->setString(1, login);
prep_stmt->setString(2, wachtwoord);
res = prep_stmt->executeQuery();
while (res->next()) {
gebruiker = new Gebruiker(login, res->getBoolean("Gebruiker_isAdmin"), res->getString("Bedrijf"),res->getBoolean("Gebruiker_isActief"),res->getInt("Werknemer_ID"));
}
}
catch(SQLException& e) {
console.log(e.what(), console.Error);
return nullptr;
}
return gebruiker;
}
The issue is that setString throws an "std::bad_alloc at memory location ..."
Ive noticed before that trying to put a string in an SQLString also throws an error. Until now ive used a workaround by putting stuff in SQLStrings.
SQLString schema = "groep003B";
dbConn->getInstance()->setSchema(schema);
dbConn->getInstance()->connect();
For some weird reason this DOES work...
Is there something i am doing wrong? I have seen examples where people use strings, but for some reason this does not work for me... Every time i try to put a variable std::string in an SQLString it gives a memory issue.
I have been trying to load an SQL database into a datatable in C++, however; it doesn't seem to want to work. The connection is working though, as DataReader works. Here is my code
void importDatabase() {
SqlConnection con;
SqlDataAdapter^ da;
SqlCommand cmd;
DataTable^ dt;
int count = 1;
try {
con.ConnectionString = "Data Source=MYNAME\\SQLEXPRESS;Initial Catalog=VinylRecords;Integrated Security=True";
cmd.CommandText = "SELECT * FROM Records";
cmd.Connection = %con;
con.Open();
da = gcnew SqlDataAdapter(%cmd);
dt = gcnew DataTable("Records");
Console::Write(da->ToString());
da->Fill(dt);
for (int i = 0; i < dt->Rows->Count - 1; i++) {
String^ value_string;
value_string = dt->Rows[i]->ToString();
Console::WriteLine(dt->Rows[i]->ToString());
count++;
}
cout << "There are " << count << " many records";
}
catch (Exception^ ex) {
Console::WriteLine(ex->ToString());
}
}
Please note, that I slightly altered the source name to post here, but only the first part.
What is wrong with my code?
So, the problem is here:
dt->Rows[i]->ToString()
Rows[i] is a Row object. And the Row class's ToString() method always prints out the fully qualified typename, which is what you are seeing. So this is technically working just fine. What you will need to do to get something useful is: you will need to access a specific column in that row and get it's value, then output that.
Something along the lines of:
foreach (DataRow dr in dt.Rows)
{
Console.Write(dr.Field<int>("ColumnOne"));
Console.Write(" | ");
Console.WriteLine(dr.Field<string>("ColumnTwo"));
}
I am not entirely sure on the syntax for accessing a specific cell inside of a DataTable when using C++\CLI. So I have provided the C# equivalent to explain why it is you were getting output of managed type names (e.g. "System.Data.DataRow") instead of the info inside of the Row's columns.
Also, I noticed you tagged this question with "mysql", but you are using the ADO.NET System.Data.SqlClient namespace. The SqlDataReader and SqlDataAdapter classes only work with TSQL (Microsoft's SQL Server databases) If you are actually connecting to a mysql database you will want to use the System.Data.OdbcDataAdapter class. You can read a little more here: https://msdn.microsoft.com/en-us/library/ms254931.aspx
I have the following problem that has me stumped. As a note to keep in mind, I am using a precompiled sqlite3.dll rather than Qt's built-in SQLite support.
Table creation code:
CREATE TABLE STUDY(
Name NOT NULL,
UserName NOT NULL REFERENCES USERS(UserName),
Description DEFAULT NULL,
PathToOsirixDB DEFAULT NULL,
PRIMARY KEY(Name, UserName)
)
The following C++ code fails to update the value in column PathToOsirixDB if it already contains a value.
It fails silently, with no error returned. That would imply that no rows are matched in the UPDATE. However, if I take the same UPDATE with valid entries for user and study to match a row and run it via either the SQLite Manager Firefox plugin, or the command line tool, it works properly.
void CStudyDatabase::SetOsirixDBForStudy(const QString user, const QString study, const QString path)
{
if (mp_db)
{
int before = sqlite3_total_changes(mp_db);
QString insert = QString("UPDATE STUDY SET PathToOsirixDB = '%1' WHERE Name = '%2' AND UserName = '%3'").arg(path, study, user);
if (!InsertData(insert))
{
int after = sqlite3_total_changes(mp_db);
if (after - before >= 1)
{
SetOsirixDB(path.toAscii().data());
emit ReturnOsirixDB(osirix_db);
}
else
{
emit DatabaseError(QString("Failed to update the target path."));
}
}
}
}
And for Insert Data
int CStudyDatabase::InsertData(const QString insert)
{
char *err_msg = 0;
int rc = sqlite3_exec(mp_db,
insert.toStdString().c_str(),
NULL,
this,
&err_msg);
if (rc)
SignalDatabaseError(&err_msg);
return rc;
}
Any insights are appreciated. Thank you.
UPDATE: added the following code to SetOsiriXDBForStudy to see if we actually find a row to update:
osirix_db = QString("");
QString query = QString("SELECT PathToOsirixDB FROM STUDY WHERE Name = '%1' AND UserName = '%2'").arg(study, user);
int rc = 0;
char *err_msg = 0;
rc = sqlite3_exec(mp_db,
query.toStdString().c_str(),
&OsirixDBCallback,
this,
&err_msg);
if (rc)
SignalDatabaseError(&err_msg);
if (!(QString(osirix_db).isEmpty()))
studyrowfound = true;
In this case, it leaves osirix_db as an empty string.
But, if I execute this function:
void CStudyDatabase::QueryOsirixDB(const QString user, const QString study)
{
int rc = 0;
char *err_msg = 0;
// we query the OrisixDB via the callback and then emit the signal if all went well
QString query = QString("SELECT PathToOsirixDB FROM STUDY WHERE Name = '%1' AND UserName = '%2'").arg(study, user);
rc = sqlite3_exec(mp_db,
query.toStdString().c_str(),
&OsirixDBCallback,
this,
&err_msg);
if (rc)
SignalDatabaseError(&err_msg);
if (QString(osirix_db).isEmpty())
emit NeedToSetOsirixDB(user, study, QString());
else
emit ReturnOsirixDB(osirix_db);
}
Then it does correctly retrieve the expected value into osirix_db.
Last update: found the problem. It was leading spaces on user and study. Had been staring at the same debugger statements for too long and glossed over the extra spaces. The failing SELECT was a big clue that there was something wrong in the construction of the SQL.
There were leading spaces on user and study. Had been staring at the same debugger statements for too long and glossed over the extra spaces. The failing SELECT was a big clue that there was something wrong in the construction of the SQL.
I am still new to using Qt4/Dbus, and i am trying to get a list of acccess points with Qt API to send/receive Dbus messeges.
I got the following error:
org.freedesktop.DBus.Error.UnknownMethod
Method "GetAccessPoint" with signature "" on interface "org.freedesktop.NetworkManager.Device.Wireless" doesn't exist
The code is:
QStringList *netList = new QStringList();
QDBusConnection sysbus = QDBusConnection::systemBus();
QDBusInterface callNM("org.freedesktop.NetworkManager","/org/freedesktop/NetworkManager","org.freedesktop.NetworkManager.Device.Wireless",sysbus);
if(callNM.isValid())
{
QDBusMessage query= callNM.call("GetAccessPoints");
if(query.type() == QDBusMessage::ReplyMessage)
{
QDBusArgument arg = query.arguments().at(0).value<QDBusArgument>();
arg.beginArray();
while(!arg.atEnd())
{
QString element = qdbus_cast<QString>(arg);
netList->append(element);
}
arg.endArray();
}else{
std::cout<< query.errorName().toStdString() << std::endl;
std::cout<< query.errorMessage().toStdString() << std::endl;
}
int x= netList->size();
for(int y=0; y< x ;y++)
{
widget.avail_nets->addItem(netList->at(y)); // just print it to my gui from the stringlist array
}
}else{
std::cout<<"fail" << std::endl;
}
Whats wrong?My naming was correct and I am following the exact specs from here
The method name is GetAccessPoints.
While your error is:
org.freedesktop.DBus.Error.UnknownMethod
Method "GetAccessPoint" with signature
"" on interface
"org.freedesktop.NetworkManager.Device.Wireless"
doesn't exist
Highlight on "GetAccessPoint". Thus you might have misspelled the method name in the code, although the code you pasted here uses the correct method name, maybe you fixed it and forgot to rebuild or clean the project?
I had the same issue, but then I noticed that it only happened when I called the GetAccessPoints method on a wired device. Make sure the device is a wireless device (i.e. DeviceType equals NM_DEVICE_TYPE_WIFI), and everything should work fine.
i modify this and works for me
QDBusInterface callNM("org.freedesktop.NetworkManager","/org/freedesktop/NetworkManager/Devices/0","org.freedesktop.NetworkManager.Device.Wireless",sysbus);
and the result is
"/org/freedesktop/NetworkManager/AccessPoint/2"
"/org/freedesktop/NetworkManager/AccessPoint/1"
i think /org/freedesktop/NetworkManager is not correct path for specific device (wireless devices).
QDBusInterface dbus_iface("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Devices/0", "org.freedesktop.NetworkManager.Device.Wireless", bus);
QDBusMessage query = dbus_iface.call("GetAccessPoints");
if(query.type() == QDBusMessage::ReplyMessage) {
QDBusArgument arg = query.arguments().at(0).value<QDBusArgument>();
arg.beginArray();
while(!arg.atEnd()) {
QString element = qdbus_cast<QString>(arg);
netList->append(element);
showAccessPointProperties(element);
}
arg.endArray();
} else {
qDebug() << "got dbus error: " << query.errorName();
qDebug() << "check the parameters like service, path, interface and method name !!!";
}
Hope this will help.