Author Topic: Need Help to Understand Code  (Read 5047 times)

0 Members and 1 Guest are viewing this topic.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Need Help to Understand Code
« on: February 07, 2019, 07:44:13 am »
Hi,

I am trying to use the sql code to build and access sql tables. My knowledge of QB64 code is limited to a point so I need some help to understand the code.

- why doesn't debug work ??
DIM SHARED DB_Debug_Mode AS LONG: DB_Debug_Mode = 1
IF DB_Debug_Mode THEN PRINT "Columns:"; columns
- shouldn't the line say IF DB_DEBUG_MODE =1 THEN PRINT .....

- What does the ".object" do ?? I don't understand this line
IF Database(DB_Selected).Object = 0

- A ":" signifies end of command (more than one command on a line) .... what does ";" signify ??
PRINT CHR$(218);: FOR x = 1 TO columns: PRINT STRING$(max_width(x), CHR$(196)) + CHR$(194);: NEXT: LOCATE , POS(0) - 1: PRINT CHR$(191);: PRINT

Thank you,

Mike

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Need Help to Understand Code
« Reply #1 on: February 07, 2019, 08:13:16 am »
The IF value THEN condition means that the command executes if the variable has a value. So not specifically just value 1 but, if is has value. So if is  not zero.

The semicolon for the print command means that the print should be stacked one line at a time without indenting. If is PRINT in row with a comma, it prints in the line with space. If the PRINT command ends with a semicolon, the next print will continue from the last print location.
PRINT alone start new line for print.
« Last Edit: February 07, 2019, 08:14:45 am by Petr »

FellippeHeitor

  • Guest
Re: Need Help to Understand Code
« Reply #2 on: February 07, 2019, 08:13:54 am »
DIM SHARED DB_Debug_Mode AS LONG: DB_Debug_Mode = 1
IF DB_Debug_Mode THEN PRINT "Columns:"; columns
- shouldn't the line say IF DB_DEBUG_MODE =1 THEN PRINT .....

Just to clear context to anyone else reading this, we're talking about http://www.qb64.org/wiki/SQL_Client

IF DB_Debug_Mode THEN is seen by QB64 as IF DB_Debug_Mode <> 0 THEN, that's why it works.

Quote
- What does the ".object" do ?? I don't understand this line
IF Database(DB_Selected).Object = 0
If you notice a few lines up in that example, Database() is DIMmed as DB_TYPE. That's a custom type defined in that program as:

Code: QB64: [Select]
  1. TYPE DB_TYPE
  2.     Object AS _OFFSET

So that's just a fancy way to do DIM SHARED Database(DB_Last) AS _OFFSET instead. No idea why Galleon would go that route.

Quote
- A ":" signifies end of command (more than one command on a line) .... what does ";" signify ??
PRINT CHR$(218);: FOR x = 1 TO columns: PRINT STRING$(max_width(x), CHR$(196)) + CHR$(194);: NEXT: LOCATE , POS(0) - 1: PRINT CHR$(191);: PRINT

A semicolon after a PRINT statement (only place where it's valid in code) is used to keep the cursor after the printed text instead of moving it to a new line.

Edit: Petr beat me to it by a few minutes as I typed this :-)

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #3 on: February 07, 2019, 05:56:42 pm »
Hi Fellippe,

Many thanks for your help.

Mike

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #4 on: February 08, 2019, 01:04:51 am »
I have another question ...

_OFFSET  is defined as

"_OFFSET (function) returns the memory offset of a variable"

What does this mean ??

Mike

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Need Help to Understand Code
« Reply #5 on: February 08, 2019, 06:49:30 am »
_OFFSET is the location of a variable in memory.

X = 3

X is the variable name.

3 is the value of the variable.

_OFFSET(X) is the location of X in memory.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #6 on: February 08, 2019, 04:25:56 pm »
Thank you,
Mike

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #7 on: February 08, 2019, 04:39:21 pm »
Hi,

I don't understand, why would I use _OFFSET instead of x = "3" ?

Similarly with _MEM ...

Mike

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Need Help to Understand Code
« Reply #8 on: February 08, 2019, 04:54:54 pm »
Hi,

I don't understand, why would I use _OFFSET instead of x = "3" ?

Similarly with _MEM ...

Mike

Think of it as printing to a file.
OPEN "temp.txt" FOR OUTPUT AS #1
FOR i = 1 to 10
    PRINT #1, i * 2
NEXT

The resulting temp.txt data file would look like the following:
2
4
6
8
10
12
14
16
18
20

Now, what's the 3rd line in that file?  Saying it's "3" wouldn't be right, now would it?

To have any idea what the content is, we have to actually specify that we're looking at a specific LINE in the text file, which is this case, the value of the 3rd line is 6...

Many programs work with LOCATION (which is basically what an _OFFSET is), instead of a variable name itself.

Another way to think of it is to say x$ = "9876543210".   

Now, where is the "7" in that string?  It's in the 3rd position... OFFSET 3 steps to the right, from the start....



_OFFSET(X) is the location of X in memory.
X is the variable/value it.



MyCar is a Ford.  It's parked in MyDriveway. 

In this case:

MyCar$ = "Ford"
_OFFSET(MyCar$) = "MyDriveway"

It's the difference of WHAT is it, verses WHERE is it.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #9 on: February 09, 2019, 05:55:33 pm »
Many thks ... but still not convinced.

Using INSTR or Arrays and Indexes or MID$ .. I can still perform the same calcs.

I think I will have to have a lie down and digest what you have said.

_offset 3 .... X$(3)  .. same thing ??

redim as _OFFSET ... dim x(10) .. same thing ??

Mike

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Need Help to Understand Code
« Reply #10 on: February 09, 2019, 08:10:05 pm »
Hi MLambert,

Do you know what a pointer is in computer jargon?

All variables names and values are stored in computer memory. Pointers point to these addresses. The Basic Language manages all the address access to these values "under the hood". But you can have direct access to these in order to do some crucial calculations really fast like you would need with DB, managing huge amounts of data. As I understand it an _OFFSET is a memory pointer.
« Last Edit: February 09, 2019, 08:56:34 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Need Help to Understand Code
« Reply #11 on: February 09, 2019, 11:18:46 pm »
Rob (Galleon) wrote the original MySQL code examples. He doesn't participate in the project (his project btw) anymore. Did you see his entry in the WIKI?

https://www.qb64.org/wiki/SQL_Client

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #12 on: February 10, 2019, 03:30:05 am »
Hi ,

I was brought up on assembler .. so I understand pointers. I appreciate using _MEM for speed but it is interesting that Pete mentioned the SQL.
I have been plodding my way thru' this code.
This code caused me headaches trying to work out _MEM and redims _OFFSET, BYVAL   etc and the result was this current topic.
The _MEM caused me to think that to access SQL variables and selects, I had to know the memory locations where SQL would store some detail.

_MEM(_MEMGET(mem_mysql_row, mem_mysql_row.OFFSET + (x - 1) * LEN(an_offset%&), _OFFSET), length) .....

If the program had been originally written with some comments and not try to cram everything onto 1 line then maybe it would have been easier.

Again... thks for your help

Mike

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Need Help to Understand Code
« Reply #13 on: February 10, 2019, 04:56:46 am »
Mike, I feel your pain.

Just for some history, the guy who wrote that MySQL code in the wiki is the one and only original developer of this language. He stayed current with this (his) project up until a couple of years ago. For 10 years, he worked solely on taking QB64 from an idea, to a nearly 100% QuickBASIC language, and then beyond to a GL and cross platform language. Of course, QB64 is a c++ translator, rather than a language that converts existing code to machine language, but still, quite an accomplishment for a single individual.... who was married, employed, and had a kid on the way. Anyway. long story longer.... yeah, no comments. Rob and I had that in common. I had worked on several office programs in QuickBASIC, which I consolidated into a single 80,000+ QB64 program. Because of how fast I needed  them done and the amount of work it was for a single person, I didn't comment anything, either. I actually used all caps, didn't indent, and used single letter variables, too. Well, that was to save memory in QuickBASIC but I did not change them when I converted over to 64. Anyway, if anyone had to read my code, they would chop off a foot and hop off a cliff before they were 10,000 line into it. So yes, I feel your pain when trying to digest this MySQL stuff, as presented.

I wish I was in a position to help, but I never needed MySQL. I just set up my own database structure and I never needed it online. I believe the reason why you haven't had more help is because most the other programmers I know who frequent these boards don't have any MySQL projects, either. At best maybe I just Murphy'd Law'd that and someone else will now come forward.

Best of luck with what you have managed to figure out so far. If you come up with a working model, I wouldn't blame you if you took it and ran but if you're inclined to post it here for others, will see if Bill will also place it in the "Library" so others can find their way through such a project in the future.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Need Help to Understand Code
« Reply #14 on: February 10, 2019, 10:06:35 pm »
Hi Pete,

Thank you very much for the background. I too have worked all day and night to get the work done .... but I have always documented as I went .. and indented because I new that one day I would have to read it again myself. I am 70 now and writing a statistical package for my son-in-law ( a real nice person else I wouldn't bother).

I too have been using my own home grown database but I have not written the .. add a new key .. logic. I.e. database is up and running, all keys setup and everything working ... but now I need to insert a new row in the 'middle' of the table....  I thought I would try MySQL first .. also because it will give the application growth and support. ... I may slip off the planet soon ....

The database is big .. 5 billion records and growing at  500,000 per month now. It takes 14 hours to perform calculations but I am re-writing the basic methodology and should just take 20-30 seconds and I believe I can get this faster.

Regards,

Mike
« Last Edit: February 11, 2019, 09:16:37 pm by MLambert »