mysql error

New to FreeBASIC? Post your questions here.
Post Reply
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

mysql error

Post by dasyar »

When I compile the program below, I get: ld: cannot find -lmysqlclient

I am running this on a Raspberry Pi Desktop PC, which is basically a Debian Linux. I have installed mysql-server and mysql-client. Is their something else that should be installed on the Linux system?

Thanks

Code: Select all

' testdb.bas
'
' November 16, 2017
'
#include once "mysql\mysql.bi"
#define NULL 0

	dim db as MYSQL ptr

 	print "Client info: "; *mysql_get_client_info()
  


Sleep
End
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: mysql error

Post by St_W »

Try installing libmysqlclient-dev
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: mysql error

Post by dasyar »

Thanks St_W, that did the trick, I think.

Taking a step further, the program below compiles fine but...

Code: Select all

' testdb.bas
'
' November 16, 2017
'
#include once "mysql\mysql.bi"
#define NULL 0

	dim db as MYSQL ptr

 	print "Client info: "; *mysql_get_client_info()
	print "Host info: "; *mysql_get_host_info( db )
	print "Server info: "; *mysql_get_server_info( db )

Sleep
End
when I run the program, I get-
Client info: 5.5.58
Host info: Segmentation fault
This does not look good.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: mysql error

Post by St_W »

Your "db" variable is a NULL pointer. I guess that this is not allowed for the mysql_get_host_info() and mysql_get_server_info() calls. You probably have to initialize the DB-connection first.

The application probably crashes because mysql_get_host_info() returns an error value (invalid pointer) due to the invalid db=0. This return value is not checked in your program and always derefenced (*) and printed.

So:
- initialize the db-connection (and the db variable) before trying to use the db
- check for errors properly
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: mysql error

Post by dasyar »

I guess I missed a line of code, now I do not get any errors.

Code: Select all

' testdb.bas
'
' November 16, 2017
'
#include once "mysql\mysql.bi"
#define NULL 0

	dim db as MYSQL ptr	
	db = mysql_init( NULL )

 	print "Client info: "; *mysql_get_client_info()
	print "Host info: "; *mysql_get_host_info( db )
	print "Server info: "; *mysql_get_server_info( db )

Sleep
End
Results when I run the program-
Client info: 5.5.58
Host info:
Server info:
I am trying to get a feel for how freeBasic and the mysql\mysql.bi work together. The only thing that I found that could be a tutorial is mysql_test.bas, which is found in .../Freebasic/examples/database. If there is better tutorial that is available, please show me a link.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: mysql error

Post by St_W »

You're still missing any error checks.

Regarding tutorials: you can use any C tutorial; e.g.:
http://zetcode.com/db/mysqlc/
http://textsegment.com/mysql-c-api-acce ... -language/
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: mysql error

Post by dasyar »

I included this line in the program, to see what happens next.

Code: Select all

	if( mysql_real_connect( db, NULL, NULL, NULL, NULL, MYSQL_PORT, NULL, 0 ) = 0 ) then
		print "Can't connect to the mysql server on port"; MYSQL_PORT
		mysql_close( db )
		end 1
	end if	
And I get this-
Can't connect to the mysql server on port 3306
I went ahead and did 'mysql -uroot -p' to log into mysql, plus enter my password, I was still getting a "Can't connect ...".

When I mentioned some tutorials, in the previous post, I was thinking along the lines of freeBasic based. Plus, now I am not sure as to what is contained within mysql\mysql.bi. Does it contain all of the mysql commands and other things, or is it just somebodies code snippet for a limited use of mysql. Is this leading me down the path of, if you want to really use freeBasic mysql, you have to create your functional mysql.bi.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: mysql error

Post by St_W »

How do you think you ever could connect with leaving servername, username and password empty?? Please read the documentation: https://dev.mysql.com/doc/refman/5.7/en ... nnect.html
And please ensure that you've set up you MySQL server correctly and you can connect to it from the command line using the MySQL tools.
dasyar wrote:When I mentioned some tutorials, in the previous post, I was thinking along the lines of freeBasic based. Plus, now I am not sure as to what is contained within mysql\mysql.bi. Does it contain all of the mysql commands and other things, or is it just somebodies code snippet for a limited use of mysql. Is this leading me down the path of, if you want to really use freeBasic mysql, you have to create your functional mysql.bi.
Whether C or FreeBasic, it's the same. The "inc"/"include" directory contains the include files for libraries. These are not code snippets or anything like that and you are not supposed to change them.

As you are cleary overchallended with this it seems better if you'd start with learning some programming basics first before you start trying to use mySQL or other external libraries. And especially learn to read and search for documentation and read/understand/skim it for important information!
Post Reply