flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Fasm and MySql Example

Author
Thread Post new topic Reply to topic
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
I was wondering if it is dificult to use fasm with a sql server... for my surprise, its more easier than using C. (becouse no strange structs are strictly required)




0) You will need a running mysql server with a database called "fasmdb"
I'm using easyphp 1.8 (yes, 1.8... really old, one of the first ones, and I guess the best, every time I tried a different version always with some php problems).

Here the link:
http://ufpr.dl.sourceforge.net/project/quickeasyphp/Archives/1.8/easyphp1-8_setup.exe

After instalation, go to phpMyAdmin and create a db called 'fasmdb'

What does the example:
1)Conects to the datebase:
2)Create a Table
3)imports values to the Table from a SQL file
4)Make a query for showing them all.



Code:
format PE Console
entry start 

include "%fasminc%\win32a.inc"
include "%fasminc%\macro\if.inc"



section ".data" data readable writeable 



szPause     db "PAUSE",0
szUser        db "root",0
szPass db 0
szHost  db "localhost",0      ;hostname
szDB       db "fasmdb",0
mysql        dd ?    ;pointer to a mysql struct. 
result  dd ?    ;pointer to a mysql query result
row dd ?    ;
szTestq    db "testq.sql",0 ;file with sql insert values
szStr        db "%s",10,13,0
szStrStrStr db "%s, %s, %s",10,13,0
szReadOnly db "r",0

szOk_connect db "MySql connection established",0
szOk_createt db "Table 'testq' was created ok",0
szOk_import  db "Values from testq.sql where imported into the database. Making query",10,13,0



szError_connect db "Fatal Error: Couldn't connect to MySql Server. Check if DataBase 'fasmdb' exits",0
szError_createt db "Error: Couldn't create Table 'testq', probably already exists",0  
szError_file db "Error: Couldn't open testq.sql",0
szError_query db "Bad query",0

pFile       dd 0
sztest  db "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",0
Buffer     db 255 dup ?  


szSql_createtable db "CREATE TABLE  testq (ID INT NOT NULL AUTO_INCREMENT ,NAME CHAR(32) ,VALUE INT, UNIQUE(ID));",0
szSql_select db "SELECT * FROM testq;",0

section ".code" code readable executable 
start: 
    push sztest
 ;init mysql struct
  invoke  mysql_init, NULL
    mov     [mysql],eax     
    
    ;connect to sql server
      invoke  mysql_real_connect, [mysql], szHost, szUser, szPass, szDB, NULL, NULL, NULL
 .if eax <> [mysql]
            cinvoke printf, szStr, szError_connect
      .else
               cinvoke printf, szStr, szOk_connect
 .endif
      
    ;select the DataBase 'fasmbd'
     invoke mysql_select_db, [mysql], szDB
       
    ;create the 'testq' table
 invoke mysql_query, [mysql], szSql_createtable
      .if eax <> 0
          cinvoke printf, szStr, szError_createt
      .else
               cinvoke printf, szStr, szOk_createt
 .endif
      
    ;open testq.sql
     cinvoke fopen, szTestq, szReadOnly
  mov     [pFile], eax
        .if eax = 0
         cinvoke printf, szStr, szError_file
         jmp     __exit
      .endif
      
    ;insert values from testq.sql into table testq  
    .repeat
             cinvoke fgets, Buffer, 255, [pFile]
         invoke mysql_query, [mysql], Buffer
         cinvoke feof, [pFile]
       .until eax <> 0   ;until end of file
  cinvoke printf, szStr, szOk_import
  
    
    ;making query to testq for all data.
        invoke mysql_query, [mysql], szSql_select
   
    ;store the result of the query, also check mysql_store_result
       invoke mysql_use_result, [mysql]
    mov     [result], eax
       .if eax = 0
         cinvoke printf, szStr, szError_query
        .else
               ;get the first row of the valid query
               invoke mysql_fetch_row, [result]
            .repeat
                     mov     [row], eax
                  ;print the value of the row ID, NAME, VALUE
                 cinvoke printf, szStrStrStr, [eax],[eax+4],[eax+8]
                  invoke mysql_fetch_row, [result]
            .until eax = 0
              ;free the result struct
             invoke mysql_free_result, [result]
          
    .endif
      
__exit:
     cinvoke system, szPause
     invoke  mysql_close, [mysql]
        invoke  ExitProcess,0



section ".idata" import data readable writeable 

library kernel32, "kernel32.dll",\ 
       user32,"user32.dll",\ 
   msvcrt, "msvcrt.dll",\
   libmysql, "libmysql.dll"

include "%fasminc%\api\kernel32.inc"
include "%fasminc%\api\user32.inc"
include "%fasminc%\api\msvcrt.inc"
include "%fasminc%\api\libmysql.inc"
    


libmysql.inc is included in the atachment. Was created with dll2inc by comrade.
Also included a very old version off mysqllib.dll. I guess the smallest one 196kb in size (aprox 85kb zipped)


Description:
Download
Filename: mysql_example.zip
Filesize: 101.63 KB
Downloaded: 723 Time(s)

Post 16 Jun 2011, 05:20
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Very interesting. Is there some "libmysql.dll" reference?
I personally prefer sqlite when I need some database engine, but mysql is OK when you need true client-server architecture.
Post 16 Jun 2011, 06:03
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
feels like php and js
Post 16 Jun 2011, 07:00
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
JohnFound wrote:
Very interesting. Is there some "libmysql.dll" reference?
I personally prefer sqlite when I need some database engine, but mysql is OK when you need true client-server architecture.


mysql c api

reference
http://dev.mysql.com/doc/refman/5.0/en/c.html

funcion overview
http://dev.mysql.com/doc/refman/5.0/en/c-api-function-overview.html


Quote:

feels like php and js

I used macros and c rtl so the reading would be more easier.

only one thing still bother me. The fields of the ROW that is returned by mysql_fetch_row all are STRINGS. If the DATATYPE is INT, the result is returned as a string.
Strange, becouse mysqllib.dll is the c api... so why not to return int for INT.
Post 16 Jun 2011, 13:14
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
AFAIK the returned data is returned into a string buffer and you have to separate it yourself.

in php you just write the name of the column to retrieve the value. for example
Code:
mysql_query($link,'SELECT * FROM users WHERE userid=1');$res=mysql_fetch_row($link);$user_Id_INT= $res['userid'];$userName =$res['username'];    
Post 16 Jun 2011, 16:52
View user's profile Send private message Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
Enko
typedef wrote:
AFAIK the returned data is returned into a string buffer and you have to separate it yourself.

in php you just write the name of the column to retrieve the value. for example
Code:
mysql_query($link,'SELECT * FROM users WHERE userid=1');
$res=mysql_fetch_row($link);
$user_Id_INT= $res['userid'];
$userName =$res['username'];
    

There is other diference...

after a query, you can fetch the result in php.
with the c api, after the query, you need to execute sotre_result or use_result, otherwise, it wont work.
Post 16 Jun 2011, 20:17
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
@Enko

Hmmm..Yes you are right, ... But one can implement the fetch method using store_result

Razz
Post 16 Jun 2011, 21:52
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.