Connect Livecode to DB

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Abeja
Posts: 6
Joined: Wed Jan 30, 2019 9:46 pm

Connect Livecode to DB

Post by Abeja » Sun Feb 03, 2019 3:10 pm

Hi,
I am new in Livecode programming and I am struggling :|
I have to connect a project in Livecode to SQlite DB which has worked well. The problem was when I wanted to select the elements from my DB-table which are similar to a field content in a card ( user input) it gives me an error.
This is my script:

On mouseup
put revDataFromQuery(tab, return,gConnID, "SELECT * FROM tbl_Events WHERE City= '"& gCityfield &"' AND Date = '"& gDatefield &"',") into tConxn
end mouseup

Where City and Date are columns in my DB-table and I have previously put "Cityfield" and "Datefield" ( user input) in global Variable to be able to use them in any card of the stack. I am almost sure it is a syntax error.
would be great if somebody could help :mrgreen:

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Connect Livecode to DB

Post by Klaus » Sun Feb 03, 2019 4:31 pm

Hi Abeja,

welcome to the forum!

What does the error say?
Please always provide as much info as possible!

Quick guess:
In LC you need to declare a global, and I presume gConnID is supposed to be a global variable, in every script you use it! So maybe this is all you need?

Code: Select all

global gConnID
## Same for all scripts where you use this variable!

On mouseup
   put revDataFromQuery(tab, return,gConnID, "SELECT * FROM tbl_Events WHERE City= '"& gCityfield &"' AND Date = '"& gDatefield &"',") into tConxn
end mouseup
Best

Klaus

Abeja
Posts: 6
Joined: Wed Jan 30, 2019 9:46 pm

Re: Connect Livecode to DB

Post by Abeja » Sun Feb 03, 2019 7:29 pm

Hi Klaus,
thnak you very much for your answer! Sorry no to be very precise.
I declared all the gVariables on top.Actually it is:

global gConnID
global gCityfield
global gDate
on mouseUp
put fld "Cityfield" into gCityfield
put fld "Date" into gDate
put revDataFromQuery(tab, return,gConnID, "SELECT * FROM tbl_Events WHERE City = '"& gCityfield &"' AND Date = '"& gDate &"',") into tConxn
put tConxn into field 1
end mouseUp
the error is: button "SearchButton": execution error at line 5 (Chunk: no such object), char 1

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Connect Livecode to DB

Post by Klaus » Sun Feb 03, 2019 7:56 pm

OK, just wanted to be sure.

Hm, the error tells you that there is no field named "Cityfield" on the card where the "mouseup" is being executed. Is there?

Sorry, have to guess...

Abeja
Posts: 6
Joined: Wed Jan 30, 2019 9:46 pm

Re: Connect Livecode to DB

Post by Abeja » Sun Feb 03, 2019 8:20 pm

yes field "Cityfield" is there and has a script:
on mouseup
set the locktext of me to "false"
put "" into me
select before me
end mouseup
So The idea is that the user could inter a city's name in the field and after clicking the button it switches to the next card and dispalys the result in field1.

ghettocottage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 366
Joined: Tue Apr 10, 2012 9:18 am

Re: Connect Livecode to DB

Post by ghettocottage » Sun Feb 03, 2019 9:12 pm

This may not be related, but I am seeing

Code: Select all

gDatefield
and

Code: Select all

gDate
being used intermittently...are these meant to be the same thing?

Abeja
Posts: 6
Joined: Wed Jan 30, 2019 9:46 pm

Re: Connect Livecode to DB

Post by Abeja » Mon Feb 04, 2019 8:57 am

Hi,
yes, I have always used gDate which is a global vat´riable where I saved the user input of field "Date".
g Datefield is a Tipo. But thank you for trying :mrgreen:

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Connect Livecode to DB

Post by AxWald » Mon Feb 04, 2019 12:53 pm

Hi,
Abeja wrote:
Sun Feb 03, 2019 3:10 pm
put revDataFromQuery(tab, return,gConnID, "SELECT * FROM tbl_Events WHERE City= '"& gCityfield &"' AND Date = '"& gDatefield &"',") into tConxn
This results in an SQL query like that:

Code: Select all

SELECT * FROM  tbl_Events WHERE City= 'Hell Central'  AND Date = '2019-01-30',
Check the last char - this query will fail unavoidably. You might want to try to use a semicolon ;-)

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

Abeja
Posts: 6
Joined: Wed Jan 30, 2019 9:46 pm

Re: Connect Livecode to DB

Post by Abeja » Mon Feb 04, 2019 5:38 pm

Hi,
thank you Form your answer! but I want to Select from my DB-table the attributes which are similar to the user's answer in field " Cityfield". Your example works perfectly because am already connected to the DB but this ist not what I need.

Abeja
Posts: 6
Joined: Wed Jan 30, 2019 9:46 pm

Re: Connect Livecode to DB

Post by Abeja » Mon Feb 04, 2019 8:09 pm

Hi there, thank you very much! The semicolon was the failure, now it works :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”