Mysql advice

General FreeBASIC programming questions.
St_W
Posts: 1058
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Mysql advice

Postby St_W » Sep 01, 2017 10:56

jj2007 wrote:mysql_num_fields for both rows and columns?

It should probably be mysql_num_rows for the row count, should it?
MrSwiss
Posts: 1930
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Mysql advice

Postby MrSwiss » Sep 01, 2017 11:22

While it is a very nice tutorial, plenty info and explanations etc. ...

Keep in mind it's age: author writes FB ver. 0.20.0 (which is 32bit only),
and all the Integer's used, will probably have to be converted to Long.

Remember: Int (in C) = Long (in FB) || Integer (in FB) = Long (in C)
St_W
Posts: 1058
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Mysql advice

Postby St_W » Sep 01, 2017 13:52

caseih wrote:https://dev.mysql.com/doc/refman/5.7/en/mysql-num-fields.html
https://dev.mysql.com/doc/refman/5.7/en ... -rows.html

Yes, probably a typo.

I've fixed the description of the sample application here: https://www.freebasic-portal.de/code-be ... n-218.html
and contacted the author of the tutorial.
Making the example 64-bit ready probably needs some more work; I'll maybe do that at a later time.
Gablea
Posts: 850
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Mysql advice

Postby Gablea » Sep 01, 2017 15:13

In them examples how would i handle no items found?
St_W
Posts: 1058
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Mysql advice

Postby St_W » Sep 01, 2017 15:19

Gablea wrote:In them examples how would i handle no items found?

No special code needed for handling that. mysql_num_rows will return zero in that case. You can test for nrows > 0 like in the example. Just don't forget to call mysql_free_result when you don't need the result (whether empty or not) anymore (e.g. have copied it to your own data structures or saved in a file).
Gablea
Posts: 850
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Mysql advice

Postby Gablea » Sep 01, 2017 16:17

@St_W
That could be where I am going wrong I assume that mysql_free_result is the same as in Vb.net where i do DbCon.close (to close the connection to
the database so I can access it again from within another part of the program)

If it ok with you guys can I post here my test code to see if I am doing it right?
St_W
Posts: 1058
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Mysql advice

Postby St_W » Sep 01, 2017 17:16

Gablea wrote:That could be where I am going wrong I assume that mysql_free_result is the same as in Vb.net where i do DbCon.close (to close the connection to the database so I can access it again from within another part of the program)

hm, not really. mysql_close would rather correspond to that. Freeing memory is not needed in .NET (or Java) as those languages do have a garbage collector.
caseih
Posts: 1011
Joined: Feb 26, 2007 5:32

Re: Mysql advice

Postby caseih » Sep 01, 2017 18:40

As soon as you are done with a result you should call the mysql_result_free on it. But you don't need to call it before you use the connection for another query. As long as the connection is open you can make queries to it. The result structure is created on your program's heap, which is why it needs to be freed. It's an independent entity you can work with, once it is created and filled with data. Hope that makes sense.
Gablea
Posts: 850
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: Mysql advice

Postby Gablea » Sep 01, 2017 22:19

So it sort of makes a dataset that I work with

That makes more sense :)
caseih
Posts: 1011
Joined: Feb 26, 2007 5:32

Re: Mysql advice

Postby caseih » Sep 01, 2017 23:29

That's my understanding, though it's possible they are client-side representation of server objects, which means you need to keep the connection open while you work with the results. Either way, though, a new result object is created every time you make a query so things can overlap safely.

How you work with connections and what life cycle a connection has depends on how you've designed your program. I've seen some database programs that keep a connection open for the life of the program. Others open and close connections frequently (for every web request even). Some keep a cache of connections that they use.

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests