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
Highlighted
Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA
Accepted Solution

SQL Function Warning and Error

[ Edited ]

Hi all, from the thread i've made before, i finally made a decision to use SQL than JSON for my app database. I've tried to write a c++ function to insert data to the SQL database, but i got an error, and a warning. Here is my code :

 

void insertincome(const QString &incomeName, const QString &incomeValue){

	QDir home = QDir::home();
	QFile file(home.absoluteFilePath("database.db"));

	// Open the file that was created
	if (file.open()) {
	    // Create an SqlDataAccess object
	    SqlDataAccess sda(home.absoluteFilePath("database.db"));

	    // Create a table in the database file
	    sda.execute("CREATE TABLE income (incomeName VARCHAR(50), incomeValue INT);");
           // Insert records into the table
	    sda.execute("INSERT INTO income (incomeName, lincomeValue) VALUES (:incomeName, :incomeValue)");
	    }
}

 

Warning : unused parameter "income Name",  unused parameter "incomeValue"

Error : no matching function for call to "QFile:: open()"

 

Any help would be appreciated. Thank you.

Developer
Posts: 217
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: SQL Function Warning and Error

You can try this.

 

// Create a table in the database file
sda.execute("CREATE TABLE income (incomeName TEXT, incomeValue INTEGER");
// Insert records into the table
sda.execute("INSERT INTO income (incomeName, lincomeValue) VALUES ('incomeName', 1)");

 

Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA

Re: SQL Function Warning and Error

Thank you for your answer oliver_kranz, but the same error and warning are still there.

 

Screenshot from 2014-03-27 18:11:45.png

 

I've searched through the Internet, but i still haven't understand what is the main problem here, as i am new to SQL database management. Any help would be appreciated.

 

Regards

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: SQL Function Warning and Error

[ Edited ]

Firstly you will not be able to create your database in the top level or assets folder you will need to create it in the data folder.

 

The reason for this is that on a release build the assets folder will be read only.

 


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: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: SQL Function Warning and Error

Something like this should suffice...

 

    QString dataFolder = QDir::homePath();
    QString newFileName = dataFolder + "/" + databaseName;
    QFile newFile( newFileName );

 


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: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: SQL Function Warning and Error

Secondly and this is just my personal opinion but I find it easier to create the database file outside the app using (on a Windows PC) apps like sqlitestudio or SQLite Browser, both of which are free.

 

Doing it this way allows quicker development of the table structure with the ability to test your SQL commands quickly without having to rebuild an app everytime. Once you know your structure is correct and you sql statements are working then you can place them in the code.

 

If you do it this way then all you need to do on app startup is check if your database exists in the data folder and if not copy the one (you created using the apps above) over from the assets folder.


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: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: SQL Function Warning and Error

I believe the sample code on which the code is based is broken. QFile is not needed, SqlDataAccess takes a filename and opens it internally.

 

The code should look like this (typed in browser, untested):

 

void insertincome(…)
{
  QString dbPath = "data/database.db";
  SqlDataAccess sda(dbPath);

  sda.execute("CREATE TABLE income (incomeName TEXT, incomeValue INTEGER");

  QVariantMap params;
  params["incomeName"] = incomeName;
  params["incomeValue"] = incomeValue;
  
  sda.execute("INSERT INTO income (incomeName, incomeValue) VALUES (:incomeName, :incomeValue)", params);

------------
     OR
------------  

  QVariantList params;
  params << incomeName << incomeValue;
  sda.execute("INSERT INTO income (incomeName, incomeValue) VALUES (?, ?)", params);

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA

Re: SQL Function Warning and Error

Thank you BBSJdev and Zmey for the possible solution and suggestion, i've learned a lot from you guys. 

Now, i'll try it and give a report.  

Developer
Posts: 39
Registered: ‎03-09-2014
My Device: Simulator
My Carrier: NA

Re: SQL Function Warning and Error

[ Edited ]

Hi guys,


I've tried your code Zmey, and it successfully get rid of the warning and error. What i learned from your code is that i have to set the paramater correctly in the code, correct me if i'm wrong. That's what i didn't know before.

 

But i still get a problem. When i try to build my app, i get this error :

 

make[2]: *** [o.le-v7-g/CascadesProject] Error 1
make[1]: *** [debug] Error 2
make: *** [Device-Debug] Error 2

 In javascript resource (project explorer), i got red cross mark on the project folder. How could i solve this?

 

I have one more question : 

 

I hope you would help me understand it. I've read on "working with SQL" documentation and sample apps, that there is certain "phase" when we try to insert data to database we have to open the database, then we close the database after we're done inserting data. What's the function of this "open and close phase" in SQL? I'm sorry if the answer is actually obvious, i'm still learning and very new to SQL.

 

Sorry, if a have plenty of questions. Any help would be appreciated. Thank you.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: SQL Function Warning and Error

Are there any warning or error lines above those? these will signal where the problem lies.

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.