MySQL 在 FreeBASIC 中访问读取

MySQL Access Read in FreeBASIC

我在开发这个示例代码时遇到了问题。

这段代码将有助于理解在 FreeBASIC 中如何与 MySQL 服务器进行通信,目前我已经能够在 C 中找到一些示例,但我不明白如何在 FreeBASIC 中调整所有内容。

应该显示内容的部分编译时出错,不知如何是好。

#Include Once "mysql\mysql.bi"
#define NULL 0



    Dim Shared URLServer As String
    Dim Shared SVRDataBase As String
    Dim Shared SVRUser As String
    Dim Shared SVRPassword As String

    DIM Shared Conn As MYSQL PTR
    DIM Shared MySQLOut As MYSQL_RES PTR
    DIM Shared Row As MYSQL_ROW

Declare Function MySQLRead(MyQuery As String, MyWork As String) As Integer    


Function MySQLRead(MyQuery As String, MyWork As String) As Integer    
  
    URLServer = "localhost"
    SVRDataBase = "test"
    SVRUser = "testuser"
    SVRPassword = "testpassword"

    MyQuery = "SELECT * FROM `test_table`"
    Conn = mysql_init(NULL)
    
    if (Conn = 0) then Print "Error  Conn= 0"
    if mysql_real_connect(Conn, URLServer, SVRUser, SVRPassword, SVRDataBase, 0, NULL, 0) = NULL then Print "Real Connect = 1"
    if mysql_query(Conn, MyQuery) then Print "Query = 1"

    MySQLOut = mysql_store_result(Conn)

    if MySQLOut = 0 then Print "No out"
    Return 1
   
End Function  
    
Sub Reading()
    DIM TotalCol As Integer
    DIM TotalRow As Integer
    Dim CiR As Integer
    Dim CiC As Integer
    Dim NE as Integer
    
    
    IF MySQLRead("SELECT * FROM `cape_anagrafica`", "read") = 0 then Print "Errore"
    
    TotalCol = mysql_num_fields(MySQLOut)
    TotalRow = mysql_num_rows(MySQLOut)
    
    print TotalRow
    print MySQLOut(0)

    

    mysql_free_result(MySQLOut)
    mysql_close(Conn)
End Sub

Reading()

end 0

代码写对了,错误的只是显示,把mysql对象当作数组,而它是类型对象。

下面我会改写正确的代码

#Include Once "mysql\mysql.bi"
#define NULL 0

    Dim Shared URLServer As String
    Dim Shared SVRDataBase As String
    Dim Shared SVRUser As String
    Dim Shared SVRPassword As String

    DIM Shared Conn As MYSQL PTR
    DIM Shared MySQLOut As MYSQL_RES PTR
    DIM Shared Row As MYSQL_ROW

Declare Function MySQLRead(MyQuery As String, MyWork As String) As Integer    


Function MySQLRead(MyQuery As String, MyWork As String) As Integer    
  
    URLServer = "localhost"
    SVRDataBase = "test"
    SVRUser = "testuser"
    SVRPassword = "testpassword"

    MyQuery = "SELECT * FROM `test_table`"
    Conn = mysql_init(NULL)
    
    if (Conn = 0) then Print "Error  Conn= 0"
    if mysql_real_connect(Conn, URLServer, SVRUser, SVRPassword, SVRDataBase, 0, NULL, 0) = NULL then Print "Real Connect = 1"
    if mysql_query(Conn, MyQuery) then Print "Query = 1"

    MySQLOut = mysql_store_result(Conn)

    if MySQLOut = 0 then Print "No out"
    Return 1
   
End Function  
    
Sub Reading()
    DIM TotalCol As Integer
    DIM TotalRow As Integer
    Dim CiR As Integer
    Dim CiC As Integer
    Dim NE as Integer
    
    
    IF MySQLRead("SELECT * FROM `cape_anagrafica`", "read") = 0 then Print "Errore"
    
    TotalCol = mysql_num_fields(MySQLOut)
    TotalRow = mysql_num_rows(MySQLOut)
    
    print TotalRow
' remove print MySQLOut(0) because is not array
' and put this cicle FOR NEXT
' Remember Row[] is not a array if you want store the data in array you have ' to convert it 
    
    For CiR = 0 to TotalRow -1 step 1
    Row = mysql_fetch_row(MySQLOut)
      For Cic = 0 To TotalCol - 1 step 1
        if Row[CiC] = NULL then print "| NULL "; else print "| "; *Row[CiC]
      Next CiC
    Next Cir

    mysql_free_result(MySQLOut)
    mysql_close(Conn)
End Sub

Reading()

end 0