Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Super Contributor
JeffLemon
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA
Accepted Solution

Query a database - duplicate connection error.

I use the following to select data from my database:

 

QVariantList Database::queryDatabase(const QString query)
{
	QVariantList sqlData;
	SqlDataAccess sqlDataAccess(mDbNameWithPath);
	sqlData.clear();
	sqlData = sqlDataAccess.execute(query).value<QVariantList>();

	if (sqlDataAccess.hasError()) {
		DataAccessError err = sqlDataAccess.error();
		qWarning() << "SQL error: type=" << err.errorType() << ": " << err.errorMessage();
		return sqlData;
	}
	return sqlData;
}

 The problem is, when I run the above method for a second time, I get the warning:

 

Warning: QSqlDatabasePrivate::addDatabase: duplicate connection name 'data/drums.db', old connection removed.

 This is due to the line:

 

SqlDataAccess sqlDataAccess(mDbNameWithPath);

 

I was wondering how I could go about removing the warning, I have no idea how to create sqlDataAccess as a global var, perhaps this would solve it.

 

If anyone can help then I would appreciate it.

 

Thanks

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Query a database - duplicate connection error.

[ Edited ]

In header file forward-declare SqlDataAccess:

 

namespace bb
{
  namespace data
  {
    class SqlDataAccess;
  }
}

 At the end of DataBase class definition add member variable:

 

protected:
  bb::data&colon;:SqlDataAccess *sqlDataAccess;

In constructor:

sqlDataAccess = NULL;

 in queryDataBase:

if (!sqlDataAccess)
  sqlDataAccess = new SqlDataAccess(mDbNameWithPath, this); // set parent to this, so it will destroy sqlDataAccess automatically

This way it will be created only once. 

 

Replace sqlDataAccess.method() with sqlDataAccess->method();

 

Consider using QtSql instead - it's a lot more solid.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Super Contributor
JeffLemon
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Query a database - duplicate connection error.

Thanks but I get the error:

 

no matching function for call to 'bb::data&colon;:SqlDataAccess::SqlDataAccess(QString&, Database* const)'

 On the line:

 

sqlDataAccess = new SqlDataAccess(mDbNameWithPath, this); // set parent to this, so it will destroy sqlDataAccess automatically

 

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Query a database - duplicate connection error.

Does DataBase class inherit QObject or any QObject-derived classes?

Please post the header file.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Super Contributor
JeffLemon
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Query a database - duplicate connection error.

#ifndef DATABASE_H_
#define DATABASE_H_

#include <QtSql/QtSql>
#include <bb/data/SqlDataAccess>

using namespace bb::data;

namespace bb
{
  namespace data
  {
    class SqlDataAccess;
  }
}

class Database
{
	public:
		Database();
	    ~Database();
		void loadDatabase(const QString databaseName);
		QVariantList queryDatabase(const QString query);
		bool updateDatabase(const QString query);
	private:
		bool copyDbToDataFolder(const QString databaseName);
		QSqlDatabase mDb;
		QString mTable;
		QString mDbNameWithPath;
	protected:
	  bb::data&colon;:SqlDataAccess *sqlDataAccess;
};

#endif /* DATABASE_H_ */

 

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Query a database - duplicate connection error.

[ Edited ]

Thanks! Inherit the class from QObject:

 

1) Add #include <QObject>

 

2)

class DataBase : public QObject
{
  Q_OBJECT
public:
  Database();
  ...

 This way you can use QObject's parent-child mechanism for controlling object's lifespan.

 

It seems forum has some formatting issues.

bb::data&colon;:SqlDataAccess

 is bb[colon][colon]data[colon][colon]SqlDataAccess, right?

 

p.s.

It's not recommended to use "using namespace" in .h files, this will pollute namespace of every .cpp file which includes this header. It's better to use fully qualified names in .h files and to move this line to .cpp file:

using namespace bb::data;

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Super Contributor
JeffLemon
Posts: 440
Registered: ‎01-13-2011
My Device: Playbook
My Carrier: NA

Re: Query a database - duplicate connection error.

Thanks very much for the detailed explanation.

--------------------------------------------------------------------------------------------------
BlackBerry Apps: Instruments | ARTPAD | Piano | Drums | Xylophone
Please use plain text.