flat assembler
Message board for the users of flat assembler.
Index
> High Level Languages > Fasm and MySql Example |
Author |
|
Enko 16 Jun 2011, 05:20
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)
|
|||||||||||
16 Jun 2011, 05:20 |
|
typedef 16 Jun 2011, 07:00
feels like php and js
|
|||
16 Jun 2011, 07:00 |
|
Enko 16 Jun 2011, 13:14
JohnFound wrote: Very interesting. Is there some "libmysql.dll" reference? 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:
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. |
|||
16 Jun 2011, 13:14 |
|
typedef 16 Jun 2011, 16:52
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']; |
|||
16 Jun 2011, 16:52 |
|
Enko 16 Jun 2011, 20:17
typedef wrote: AFAIK the returned data is returned into a string buffer and you have to separate it yourself. 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. |
|||
16 Jun 2011, 20:17 |
|
typedef 16 Jun 2011, 21:52
@Enko
Hmmm..Yes you are right, ... But one can implement the fetch method using store_result |
|||
16 Jun 2011, 21:52 |
|
NETSTALKER 04 Feb 2023, 17:37
How to work with SQL without libraries?
|
|||
04 Feb 2023, 17:37 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.