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
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T
Accepted Solution

Unable to Open Database created in C++ from in QML page

Okay, I've tried many ways (searching for examples) to get the database to open, but nothing has worked.

 

I create a database with a table in my appUI c++ page.

No problem.

 

QSqlDatabase appDatabase = QSqlDatabase::addDatabase("QSQLITE");
appDatabase.setDatabaseName(./data/myDatabase.db);

 From there, I am able to open that database and inserts some records in another c++ page.

No problem.

 

Then, I kick of a signal to populate a ListView in one of the tabs.  I have a Toast appear to know that the signal is working.  I added error handling for the DataSource to see if there is a problem.

 

The ListView code

ListView {
  id: lvGroups
  dataModel: dmGroups
           
  attachedObjects: [
      GroupDataModel {
         id: dmGroups
         grouping: ItemGrouping.None
      },
      DataSource {
          id: dsGroups
          source: "data/myDatabase.db"
          query: "SELECT * FROM groups"
             onSourceChanged: {
                dmGroups.setSortedAscending(true);
             }
             onDataLoaded: {
                dmGroups.clear();
                dmGroups.insertList(data);
             onError: {
                 console.log("Load Error[" + errorType + "]: " + errorMessage);
             }
             onQueryChanged: {
                 console.log("query : " + query);
             }
      } 
   ]
}

 I have tried:

./sql/mydatabase.db

/sql/mydatabase.db

mydatabase.db

./mydatabase.db

/mydatabase.db

/data/mydatabase.db

 

SqlDataAccess error:  "unable to open new database file "/accounts/1000/appdata/myAppTest/app/native/assets/sql/myDatabase.db""

 

Any ideas?

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Unable to Open Database created in C++ from in QML page

[ Edited ]

But have you tried "data/myDatabase.db" which is not in your list and the one it will be. Smiley Happy

 

You will also need to reference it using;

 

QDir::homePath() + "/myDatabase.db"


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Retired
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: Unable to Open Database created in C++ from in QML page

Well, opening a database in your assets folder could be problematic because you can't write there. It would have to be in the data folder. You could package up a default database in the assets folder and then copy it into the data folder if it didn't exist at application launch, if you wanted.

 

Anyway, it should be "mydatabase.db", though most people seem to put it in a "sql" directory first. "sql/mydatabase.db"

 

Not sure about the QSqlDatabase stuff. I'm not sure that setDatabaseName works like that.. It definitely needs to be a string, at least.

 

You can use the SqlDataAccess class directly to execute SQL statements.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Unable to Open Database created in C++ from in QML page

Without looking at the rest of your code I also suspect you are actually creating the subfolder and database in the assets folder, this will work for debug code but not for a release.

 

Use Momentics Target File System Navigator to see if you are actually creating it where you think you are.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T

Re: Unable to Open Database created in C++ from in QML page

Thanks for the tip for the viewer, I forgot about it.

 

First off, I can only create the database in c++ with "data/mydatabase.db" .

If I take out the data/ or try some other path, I get an error.

 

The database is located at

accounts/1000/appdata/myApplication/data

 

If I use just

"mydatabase.db" in the source, then I get an error that the table doesn't exist.  Which is better than not being able to find the database, but I can't get the data out of it.

 

If I click on the file, there is data there.

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Retired
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: Unable to Open Database created in C++ from in QML page

Sounds like maybe your table isn't being created, or at least not the way you think it is. What's in the database if you open it up with an SQLite editor?

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T

Re: Unable to Open Database created in C++ from in QML page

Here is a screen shot (if it works)

 

dbAppDirectory.png

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T

Re: Unable to Open Database created in C++ from in QML page

SQLite Manager shows 1 table "groups" with 5 records.

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment
Retired
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30
My Carrier: Bell

Re: Unable to Open Database created in C++ from in QML page

Hmmm, that looks like it should work. What exactly is the error message you are getting?

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Developer
Posts: 265
Registered: ‎01-02-2011
My Device: Z30, PlayBook
My Carrier: AT&T

Re: Unable to Open Database created in C++ from in QML page

From the error line

 

SqlDataAccess error:  "SELECT * FROM groups" . QSqlError::type= "QSqlError::StatementError" , QSqlError::number= 1 , databaseText= "no such table: groups" , driverText= "Unable to execute statement" 

Load Error[3]: no such table: groups / Unable to execute statement

 I still don't think it is finding the database correctly.

I treid to set the source and query with the function that kicks off the load and it would die trying to set the source.

_________________
Meetup for BlackBerry 10
Meetup Search Tool - not available at the moment