[V4] include file for 'libmysql.lib'

Share your advanced PureBasic knowledge/code with the community.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

[V4] include file for 'libmysql.lib'

Post by Flype »

it's a start, not complete, anybody can contribute :P

File:1->libmysql5.zip
Image

include 'libmysql.pb' :

Code: Select all

;-----------------------------------------------------------------------------
; 
; Object:       MySQL 5.0 Client Library
; 
; Support:      http://www.purebasic.com
;               http://dev.mysql.com/doc/refman/5.0/en/c-api-functions.html
; 
; Requirements: Purebasic 4.0+
;               libmysql.lib
;               libmysql.dll
;-----------------------------------------------------------------------------

;- Structures

Structure MYSQL
EndStructure
Structure MYSQL_RES
EndStructure
Structure MYSQL_BIND
EndStructure
Structure MYSQL_STMT
EndStructure

Structure MYSQL_ROW
  field.s[255]
EndStructure
Structure MYSQL_FIELD
  name.s
  org_name.s
  table.s
  org_table.s
  db.s
  catalog.s
  def.s
  length.l
  max_length.l
  name_length.l
  org_name_length.l
  table_length.l
  org_table_length.l
  db_length.l
  catalog_length.l
  def_length.l
  flags.l
  decimals.l
  charset_nr.l
  type.l
EndStructure
Structure MY_CHARSET_INFO
  number.l
  state.l
  csname.s
  name.s
  comment.s
  dir.s
  mbminlen.l
  mbmaxlen.l
EndStructure

;- Imports

Import "libmysql.lib" ; mysql
  mysql_affected_rows.l(*mysql.MYSQL)
  mysql_autocommit.l(*mysql.MYSQL,*mode.BYTE)
  mysql_change_user.l(*mysql.MYSQL,user.s,passwd.s,db.s)
  mysql_character_set_name.l(*mysql.MYSQL)
  mysql_close.l(*mysql.MYSQL)
  mysql_commit.l(*mysql.MYSQL)
  mysql_data_seek.l(*result.MYSQL_RES,offset.d)
  mysql_debug.l(string.s)
  mysql_disable_reads_from_master.l(*mysql.MYSQL)
  mysql_disable_rpl_parse.l(*mysql.MYSQL)
  mysql_dump_debug_info.l(*mysql.MYSQL)
  mysql_embedded.l()
  mysql_enable_reads_from_master.l(*mysql.MYSQL)
  mysql_enable_rpl_parse.l(*mysql.MYSQL)
  mysql_eof.l(*result.MYSQL_RES)
  mysql_errno.l(*mysql.MYSQL)
  mysql_error.l(*mysql.MYSQL)
  mysql_escape_string.l(strTo.s,strFrom.s,length.l)
  mysql_fetch_field.l(*result.MYSQL_RES)
  mysql_fetch_field_direct.l(*result.MYSQL_RES,fieldnr.l)
  mysql_fetch_fields.l(*result.MYSQL_RES)
  mysql_fetch_lengths.l(*result.MYSQL_RES)
  mysql_fetch_row.l(*result.MYSQL_RES)
  mysql_field_count.l(*mysql.MYSQL)
  mysql_field_seek.l(*result.MYSQL_RES,offset.l)
  mysql_field_tell.l(*result.MYSQL_RES)
  mysql_free_result.l(*result.MYSQL_RES)
  mysql_get_character_set_info.l(*mysql.MYSQL,*cs.MY_CHARSET_INFO)
  mysql_get_client_info.l()
  mysql_get_client_version.l()
  mysql_get_host_info.l(*mysql.MYSQL)
  mysql_get_parameters.l()
  mysql_get_proto_info.l(*mysql.MYSQL)
  mysql_get_server_info.l(*mysql.MYSQL)
  mysql_get_server_version.l(*mysql.MYSQL)
  mysql_hex_string.l(strTo.s,strFrom.s,length.l)
  mysql_info.l(*mysql.MYSQL)
  mysql_init.l(*mysql.MYSQL)
  mysql_insert_id.l(*mysql.MYSQL)
  mysql_kill.l(*mysql.MYSQL,pid.l)
  mysql_library_end.l(*mysql.MYSQL)
  mysql_library_init.l(argc.l,argv.l,groups.l)
  mysql_list_dbs.l(*mysql.MYSQL,wild.s)
  mysql_list_fields.l(*mysql.MYSQL,table.s,wild.s)
  mysql_list_processes.l(*mysql.MYSQL)
  mysql_list_tables.l(*mysql.MYSQL,wild.s)
  mysql_master_query.l(*mysql.MYSQL,query.s,length.l)
  mysql_more_results.l(*mysql.MYSQL)
  mysql_next_result.l(*mysql.MYSQL)
  mysql_num_fields.l(*result.MYSQL_RES)
  mysql_num_rows.l(*result.MYSQL_RES)
  mysql_options.l(*mysql.MYSQL,option.l,arg.s)
  mysql_ping.l(*mysql.MYSQL)
  mysql_query.l(*mysql.MYSQL,query.s)
  mysql_read_query_result.l(*mysql.MYSQL)
  mysql_real_connect.l(*mysql.MYSQL,host.s,user.s,passwd.s,db.s,port.l,unix_socket.s,client_flag.l)
  mysql_real_escape_string.l(*mysql.MYSQL,strTo.s,strFrom.s,length.l)
  mysql_real_query.l(*mysql.MYSQL,query.s,length.l)
  mysql_refresh.l(*mysql.MYSQL,options.l)
  mysql_reload.l(*mysql.MYSQL)
  mysql_rollback.l(*mysql.MYSQL)
  mysql_row_seek.l(*mysql.MYSQL,offset.l)
  mysql_row_tell.l(*result.MYSQL_RES)
  mysql_select_db.l(*mysql.MYSQL,db.s)
  mysql_rpl_parse_enabled.l(*mysql.MYSQL)
  mysql_rpl_probe.l(*mysql.MYSQL)
  mysql_rpl_query_type.l(*mysql.MYSQL,type.l)
  mysql_send_query.l(*mysql.MYSQL,query.s,length.l)
  mysql_server_end.l()
  mysql_server_init.l(argc.l,argv.l,groups.l)
  mysql_set_character_set.l(*mysql.MYSQL,csname.s)
  mysql_set_server_option.l(*mysql.MYSQL,option.l)
  mysql_shutdown.l(*mysql.MYSQL,shutdown_level.l)
  mysql_slave_query.l(*mysql.MYSQL,query.s,length.l)
  mysql_sqlstate.l(*mysql.MYSQL)
  mysql_ssl_set.l(*mysql.MYSQL,key.s,cert.s,ca.s,capath.s,cipher.s)
  mysql_stat.l(*mysql.MYSQL)
  mysql_store_result.l(*mysql.MYSQL)
  mysql_thread_end.l()
  mysql_thread_id.l(*mysql.MYSQL)
  mysql_thread_init.l()
  mysql_thread_safe.l()
  mysql_use_result.l(*mysql.MYSQL)
  mysql_warning_count.l(*mysql.MYSQL)
EndImport
Import "libmysql.lib" ; stmt
  mysql_stmt_affected_rows.l(*stmt.MYSQL_STMT)
  mysql_stmt_attr_get.l(*stmt.MYSQL_STMT,option.l,*arg.l)
  mysql_stmt_attr_set.l(*stmt.MYSQL_STMT,option.l,*arg.l)
  mysql_stmt_bind_param.l(*stmt.MYSQL_STMT,*bind.MYSQL_BIND)
  mysql_stmt_bind_result.l(*stmt.MYSQL_STMT,*bind.MYSQL_BIND)
  mysql_stmt_close.l(*stmt.MYSQL_STMT)
  mysql_stmt_data_seek.l(*stmt.MYSQL_STMT,offset.d)
  mysql_stmt_errno.l(*stmt.MYSQL_STMT)
  mysql_stmt_error.l(*stmt.MYSQL_STMT)
  mysql_stmt_execute.l(*stmt.MYSQL_STMT)
  mysql_stmt_fetch.l(*stmt.MYSQL_STMT)
  mysql_stmt_fetch_column.l(*stmt.MYSQL_STMT,*bind.MYSQL_BIND,column.l,offset.l)
  mysql_stmt_field_count.l(*stmt.MYSQL_STMT)
  mysql_stmt_free_result.l(*stmt.MYSQL_STMT)
  mysql_stmt_init.l(*mysql.MYSQL)
  mysql_stmt_insert_id.l(*stmt.MYSQL_STMT)
  mysql_stmt_num_rows.l(*stmt.MYSQL_STMT)
  mysql_stmt_param_count.l(*stmt.MYSQL_STMT)
  mysql_stmt_param_metadata.l(*stmt.MYSQL_STMT)
  mysql_stmt_prepare.l(*stmt.MYSQL_STMT,query.s,length.l)
  mysql_stmt_reset.l(*stmt.MYSQL_STMT)
  mysql_stmt_result_metadata.l(*stmt.MYSQL_STMT)
  mysql_stmt_row_seek.l(*stmt.MYSQL_STMT,offset.l)
  mysql_stmt_row_tell.l(*stmt.MYSQL_STMT)
  mysql_stmt_send_long_data.l(*stmt.MYSQL_STMT,parameter_number.l,datas.s,length.l)
  mysql_stmt_sqlstate.l(*stmt.MYSQL_STMT)
  mysql_stmt_store_result.l(*stmt.MYSQL_STMT)
EndImport

;- End
example 'libmysql_test.pb' :

Code: Select all

;-----------------------------------------------------------------------------
; 
; Object:       MySQL 5.0 Client Application
; 
; Support:      http://www.purebasic.com
;               http://dev.mysql.com/doc/refman/5.0/en/c-api-functions.html
; 
; Requirements: Purebasic 4.0+
;               libmysql.pb
;               libmysql.lib
;               libmysql.dll
;-----------------------------------------------------------------------------

XIncludeFile "libmysql.pb"

Procedure Test_Client()
  Debug "[Client]"
  Debug mysql_get_client_version()
  Debug PeekS(mysql_get_client_info())
  Debug Hex(mysql_get_parameters())
EndProcedure
Procedure Test_Error(handle.l)
  Debug "[Error]"
  Debug mysql_errno(handle)
  Debug PeekS(mysql_sqlstate(handle))
  Debug PeekS(mysql_error(handle))
EndProcedure
Procedure Test_Server(handle.l)
  Debug "[Server]"
  Debug mysql_get_server_version(handle)
  Debug PeekS(mysql_get_server_info(handle))
  Debug PeekS(mysql_get_host_info(handle))
EndProcedure
Procedure Test_Fields(result.l)
  fields.s = ""
  For i = 0 To mysql_num_fields(result) - 1
    *field.MYSQL_FIELD = mysql_fetch_field(result)
    fields + *field\name + ", "
  Next
  Debug "fields>"+fields
EndProcedure
Procedure Test_Rows(result.l)
  For i = 0 To mysql_num_rows(result) - 1
    *row.MYSQL_ROW = mysql_fetch_row(result)
    If *row
      rows.s = ""
      For j = 0 To mysql_num_fields(result) - 1
        rows + *row\field[j] + ", "
      Next
      Debug "rows>"+rows
    EndIf
  Next
EndProcedure
Procedure Test_Query(handle.l,query.s)
  Debug "[Query]"
  If mysql_query(handle,query)
    Test_Error(handle)
  Else
    id = mysql_insert_id(handle)
    If id
      Debug "id>"+Str(id)
    EndIf
    info = mysql_info(handle)
    If info
      Debug "info>"+PeekS(info)
    EndIf
    If mysql_field_count(handle)
      result = mysql_store_result(handle)
      If result
        Test_Fields(result)
        Test_Rows(result)
        mysql_free_result(result)
      EndIf
    EndIf
  EndIf
EndProcedure
Procedure Test_Processes(handle.l)
  Debug "[Processes]"
  result = mysql_list_processes.l(handle)
  If result
    Test_Fields(result)
    Test_Rows(result)
    mysql_free_result(result)
  EndIf
EndProcedure
Procedure Test_Tables(handle.l)
  Debug "[Tables]"
  result = mysql_list_tables.l(handle,"")
  If result
    Test_Fields(result)
    Test_Rows(result)
    mysql_free_result(result)
  EndIf
EndProcedure
Procedure Test_Dbs(handle.l)
  Debug "[Dbs]"
  result = mysql_list_dbs.l(handle,"")
  If result
    Test_Fields(result)
    Test_Rows(result)
    mysql_free_result(result)
  EndIf
EndProcedure
Procedure Test_String(handle.l)
  Debug "[EscapeString]"
  sFrom.s = "What's this"
  sTo.s = Space(Len(sFrom)*2+1)
  If mysql_escape_string(sTo,sFrom,Len(sFrom))
    Debug sTo
  EndIf
  sFrom.s = "What's this"
  sTo.s = Space(Len(sFrom)*2+1)
  If mysql_real_escape_string(handle,sTo,sFrom,Len(sFrom))
    Debug sTo
  EndIf
  sFrom.s = "ABCDEF"
  sTo.s = Space(Len(sFrom)*2+1)
  If mysql_hex_string(sTo,sFrom,Len(sFrom))
    Debug "0x"+sTo
  EndIf
EndProcedure
Procedure Test_CharSet(handle.l)
  Debug "[CharSet]"
  mysql_get_character_set_info(handle,cs.MY_CHARSET_INFO)
  Debug cs\number
  Debug cs\state
  Debug cs\csname
  Debug cs\name
  Debug cs\comment
  Debug cs\dir
  Debug cs\mbminlen
  Debug cs\mbmaxlen
EndProcedure
Procedure Test_Stmt(handle.l)
  Debug "[Stmt]"
  stmt = mysql_stmt_init(handle)
  If stmt
    query.s = "SELECT * FROM people"
    mysql_stmt_prepare(stmt,query,Len(query))
    mysql_stmt_execute(stmt)
    If mysql_stmt_field_count(stmt)
      result = mysql_stmt_result_metadata(stmt) 
      If result
        Test_Fields(result)
        Test_Rows(result)
        mysql_free_result(result)
      EndIf
    EndIf
    mysql_stmt_close(stmt)
  Else
    Test_Error(handle)
  EndIf
EndProcedure

;-------------

Test_Client()
hDB = mysql_init(#Null)
If hDB
  If mysql_real_connect(hDB,"localhost","root","","",3306,"NULL",#Null)
    If mysql_select_db(hDB,"test")
      Test_Error(hDB)
    Else
      mysql_autocommit(hDB,#True)
      Test_Server(hDB)
      Test_Processes(hDB)
      Test_Dbs(hDB)
      Test_Tables(hDB)
      Test_String(hDB)
      Test_CharSet(hDB)
      Test_Query(hDB,"SELECT NOW()")
      Test_Query(hDB,"SELECT NOW() 'DateTime', (1+2*16) 'Expression'")
      Test_Query(hDB,"DROP TABLE people")
      Test_Query(hDB,"CREATE TABLE people (id INT(8) NOT NULL AUTO_INCREMENT, firstname VARCHAR(64), name VARCHAR(64), PRIMARY KEY (id), UNIQUE KEY id (id), UNIQUE KEY name (name))")
      Test_Query(hDB,"INSERT INTO people VALUES('','Jean','Dupont')")
      Test_Query(hDB,"INSERT INTO people VALUES('','Patrice','Durand')")
      Test_Query(hDB,"INSERT INTO people VALUES('','Hélène','Dubois')")
      Test_Query(hDB,"INSERT INTO people VALUES('','Léon','Martin'),('','Nathalie','Morin'),('','Hélène','Dujardin')")
      Test_Query(hDB,"UPDATE people SET firstname='Valérie' WHERE firstname='Hélène'")
      Test_Query(hDB,"SELECT firstname,name FROM people ORDER BY name ASC")
      Test_Stmt(hDB)
    EndIf
  Else
    Test_Error(hDB)
  EndIf
  mysql_close(hDB)
EndIf

;-------------
Last edited by Flype on Wed Feb 22, 2006 2:38 pm, edited 2 times in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Looks good ! A little tip: you should be about to remove all the 'As "symbols"' because the compiler already do this standard decoration by itself ;).
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Fred wrote:Looks good ! A little tip: you should be about to remove all the 'As "symbols"' because the compiler already do this standard decoration by itself ;).
You mean something like this :?: :

Code: Select all

Import "libmysql.lib"
mysql_affected_rows.l(*mysql.MYSQL)
...
...
...
EndImport 
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Num3 wrote:
Fred wrote:Looks good ! A little tip: you should be about to remove all the 'As "symbols"' because the compiler already do this standard decoration by itself ;).
You mean something like this :?: :

Code: Select all

Import "libmysql.lib"
mysql_affected_rows.l(*mysql.MYSQL)
...
...
...
EndImport 
Yes, he means like this. Very handy, thanx for the tips.
:P
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

i have updated the lib :

1/ no more symbols '@',
2/ stmt support,
3/ some errors fixed,
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

This is too weird!

I have Pb4b3 at my portable computer and this code works fine, but in my PC with Pb4b4 this always throws an error:
Garbage at the end of line

Code: Select all

Import "libmysql.lib" ; mysql
  mysql_affected_rows.l(*mysql.MYSQL)
  mysql_autocommit.l(*mysql.MYSQL,*mode.BYTE)
No matter which line i comment the next one throws the error!
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Num3 wrote:TPC with Pb4b4 this always throws an error:
Garbage at the end of line

Code: Select all

Import "libmysql.lib" ; mysql
  mysql_affected_rows.l(*mysql.MYSQL)
  mysql_autocommit.l(*mysql.MYSQL,*mode.BYTE)
No matter which line i comment the next one throws the error!
change it without .l

Code: Select all

Import "libmysql.lib" ; mysql
  mysql_affected_rows(*mysql.MYSQL)
  mysql_autocommit(*mysql.MYSQL,*mode.BYTE)
but i think, it's a new bug
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

yes it's a bug, you're right but it is corrected for the next beta.
viewtopic.php?p=128780#128780
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

just to say that it works like a charm with PB4 B11 :P
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Thanks, i try this soon :D
z3phir2003
User
User
Posts: 49
Joined: Wed Jan 12, 2005 3:50 pm

bug ?

Post by z3phir2003 »

i downloaded the file but when i compile the example i just get the procedure entry point mysql_hex_string could not be located in the dynamic link library libmysql.dll
what's the problem ?
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

no problem here,
do you really use the provided libmysql.lib & libmysql.dll ? (.zip)

should be used this way :

Code: Select all

sFrom.s = "ABCDEF"
sTo.s = Space( Len(sFrom) * 2 + 1 )

If mysql_hex_string(sTo, sFrom, Len(sFrom))
  Debug "0x" + sTo  ;  display 0x414243444546
EndIf
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post by jqn »

2Flype

Cool tip!. Very useful. I'm using it in my programs and it's very fast.

But, in 5.0 Reference manual I'm reading about "mysqlclient.lib". I've tried to compile with this lib and PB say me a lot of linking errors.

What is difference between "libmysql.lib" and "mysqlclien.lib"? do you know it?.
I'm looking for a "lib" that not need a "dll" to work, but includes invoked functions (mysql_init(), mysqlclose()... ) in my compiled "exe" program.

Regards
JOAQUIN
mskuma
Enthusiast
Enthusiast
Posts: 573
Joined: Sat Dec 03, 2005 1:31 am
Location: Australia

Post by mskuma »

Hi Flype, thanks for this lib, but I'm getting a similar problem to z3phir2003, i.e. upon running the test pb program, I get a dialog saying "the procedure entry point mysql_get_parameters could not be located in the dynamic link library libmysql.dll".

If I comment that out of Test_Client(), I get another similar error - "the procedure entry point mysql_sqlstate could not be located in the dynamic link library libmysql.dll".

I'm using the lib & dll in the zip (compiling the pb files with the lib & dll in the same folder). I'm using MySQL 5.0.22-community-nt.

Any suggestion to resolve this appreciated. Thanks.

[Edit: Flype, I just found your simple ODBC example code elsewhere on the forum - thanks, it was a good kick start.. I think I might just use ODBC - seems like PB has supported it well].
Post Reply