Home » Community » Newbie corner » Need help with SQL code
Need help with SQL code [message #36627] |
Thu, 21 June 2012 18:32  |
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  |
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();
}
|
|
|
Goto Forum:
Current Time: Tue Apr 29 05:39:26 CEST 2025
Total time taken to generate the page: 0.03176 seconds
|