Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Need help with SQL code
Need help with SQL code [message #36627] Thu, 21 June 2012 18:32 Go to next message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
Hello!
I tried this simple code:
void MainWindow::BtnAddPush()
{
	
	String name = ~editName;
	String phone = ~editPhone;
	
	arr.Add(name, phone);
	
	Sqlite3Session sqlite3ses;
	if (!sqlite3ses.Open("test.db")) {
		printf("Couldn't open database!");
		return;
	}
	
	Sql sql(sqlite3ses);
	sql.ClearError();	
	sql.Execute("INSERT INTO PHONEBOOK VALUES(?,?);", name, phone);
	sqlite3ses.Close();
}

void MainWindow::BtnRefreshPush()
{
	Sqlite3Session sqlite3ses;
	if (!sqlite3ses.Open("test.db")) {
		printf("Couldn't open database!");
		return;
	}
	
	Sql sql(sqlite3ses);
	sql.ClearError();
	sql.Execute("SELECT * FROM PHONEBOOK;");
	while(sql.Fetch()) {
		arr.Add(sql[0], sql[1]);
	}
	sqlite3ses.Close();
}


MainWindow::MainWindow()
{
	CtrlLayout(*this, "Window title");
	arr.AddColumn("Name");
	arr.AddColumn("Phone");
	btnAdd.WhenPush = THISBACK(BtnAddPush);
	btnRefresh.WhenPush = THISBACK(BtnRefreshPush);	
}

GUI_APP_MAIN
{
	MainWindow().Run();
}


It compiles fine. But when I click the 'Refresh' button I get an error. What's wrong?

The error message is: "Assertion failed in Sqlite3upp.cpp, line 361 SQLITE_OK == retval".

PS: The 'test.db' database already exists (created by sqlite3 command line tool).
Re: Need help with SQL code [message #36628 is a reply to message #36627] Thu, 21 June 2012 19:11 Go to previous message
lectus is currently offline  lectus
Messages: 329
Registered: September 2006
Location: Brazil
Senior Member
Well, after looking at the manual and examples I found the solution.
Looks like I need a global SQL and then I use local Sql for SELECT queries.

So, here's the working code for anyone else struggling with this:
#include "testsql.h"
#include <plugin/sqlite3/Sqlite3.h>

Sqlite3Session sqlite3ses;

void MainWindow::BtnAddPush()
{
	
	String name = ~editName;
	String phone = ~editPhone;
	
	arr.Add(name, phone);		
	SQL.Execute("INSERT INTO PHONEBOOK VALUES(?,?);", name, phone);
}

void MainWindow::BtnRefreshPush()
{
	arr.Clear();
	Sql sql;
	sql.Execute("SELECT * FROM PHONEBOOK");
	while(sql.Fetch()) {
		arr.Add(sql[0], sql[1]);
	}
}


MainWindow::MainWindow()
{
	CtrlLayout(*this, "Window title");
	arr.AddColumn("Name");
	arr.AddColumn("Phone");
	btnAdd.WhenPush = THISBACK(BtnAddPush);
	btnRefresh.WhenPush = THISBACK(BtnRefreshPush);

	
	if (!sqlite3ses.Open("test.db")) {
		printf("Couldn't open database!");
		return;
	}
	
	SQL = sqlite3ses;
	
}

GUI_APP_MAIN
{
	MainWindow().Run();
}

Previous Topic: Serious Hex Editor
Next Topic: Can Upp develope applications on Symbian or Android or WinCE?[FEATURE REQUEST]
Goto Forum:
  


Current Time: Thu Apr 18 23:26:35 CEST 2024

Total time taken to generate the page: 0.97735 seconds