Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

Mysql

Uploader:Mitgliedjakidomi
Datum/Zeit:12.11.2009 13:00:34

'Ein kleiner datenbank skrypt
'Beispiel aufbau
'
'#include once "mysql.bi"
'dim as mysql_type sql
'sql.host="localhost"
'sql.user="user"
'sql.pass="pass"
'sql.db="database"
'sql.update "user","ip='"+"192.168.0.5"+"',online=1,logins=logins+1,tsneid="+str(5)+",status='online',lastlogin='"+current.datetime()+"'","id="+str(3)
'sql.update "user","online=0","id=3"


#include once "mysql\mysql.bi"
#include once "windows.bi"
#include once "vbcompat.bi"
#define NULL 0
type mysql_type
    host as string="localhost"
    user as string
    pass as string
    db as string
    port as uinteger=3306
    insertid as integer
    mysqlptr as mysql ptr
    declare sub connect()
    declare sub disconnect()
    declare sub insert(table as string,values as string)
    declare sub dataselect(res() as string,byval cols as string="*",table as string, byval where as string="")
    declare function query(sql as string) as MYSQL_RES ptr
    declare sub listcols(cols() as string,table as string)
    declare sub listtable(table() as string)
    declare sub update(table as string,cols as string,byval where as string="")
    declare sub tdelete(table as string,byval where as string="")
    declare sub drop(table as string)
    declare sub fetch_array(res() as string,sres as MYSQL_RES ptr)
end type
sub mysql_type.connect ()
    'Verbindung zum MySQL Server herstellen und Informationen zur
    'Serversoftware abfragen - Zweck: Test, ob die Verbindung so
    'funktioniert, wie sie funktionieren soll.
    Dim ServerInfo As String
    this.mysqlptr= mysql_init( NULL )
    if( mysql_real_connect( this.mysqlptr,this.host, this.user, this.pass, NULL, this.port, NULL, 0 ) = 0 ) then
        print "Es konnte keine Verbindung zum MySQL-Server hergestellt werden."
        PRINT " "; *mysql_error(this.mysqlptr)
        mysql_close( this.mysqlptr )
    end if
    ServerInfo = *mysql_get_server_info( this.mysqlptr )
    if mysql_select_db( this.mysqlptr, this.db ) then ?"Kann die Datenbank nicht Selectieren"
END sub
sub mysql_type.disconnect()
    Mysql_close(this.mysqlptr)
end sub
sub mysql_type.insert(tabel as string,value as string)
    this.connect()
    Dim sQuery As String
    Dim ID As Integer
    Dim result As Integer
    Dim Fehler as ZString Ptr
    squery = "INSERT INTO "+tabel+" VALUES ("+value+")"
    result = mysql_real_query (this.mysqlptr,STRPTR(squery),LEN(squery))
    If result = 0 THEN
        ID = mysql_insert_id(this.mysqlptr)
        this.insertid= ID
    ELSE
        Fehler = mysql_error(this.mysqlptr)
        If (*Fehler)[0] <> 0 Then
            PRINT " "; *mysql_error(this.mysqlptr)
        END IF
    END IF
    this.disconnect()
end sub
sub mysql_type.dataselect(res() as string,byval cols as string="*",table as string, byval where as string="")
    this.connect()
    dim row as MYSQL_ROW
    dim sres as MYSQL_RES ptr
    Dim As String sQuery
    Dim As Integer u
    squery = "SELECT "+cols+" FROM "+table
    if where<>"" then squery+=" where "+where
    If mysql_real_query (this.mysqlptr,STRPTR(squery),LEN(squery)) <> 0 THEN
        this.disconnect()
        ? *mysql_error(this.mysqlptr)
    END IF
    sres = mysql_use_result(this.mysqlptr)

    Do
        row = mysql_fetch_row( sres )
        if( row = NULL ) then
            Exit Do
        ELSE
            dim as string r
            redim preserve res (u,mysql_num_fields(sres)-1)
            for i as integer=0 to mysql_num_fields(sres)-1
                if *row[i]="" then
                    res(u,i)="null"
                else
                    res(u,i)=*row[i]
                endif
            next
            u+=1
        END IF
    LOOP
    this.disconnect()
end sub
sub mysql_type.fetch_array(res() as string,sres as MYSQL_RES ptr)
    dim row as MYSQL_ROW
    Dim As Integer u
    Do
        row = mysql_fetch_row( sres )
        if( row = NULL ) then
            Exit Do
        ELSE
            dim as string r
            redim preserve res (u,mysql_num_fields(sres)-1)
            for i as integer=0 to mysql_num_fields(sres)-1
                if *row[i]="" then
                    res(u,i)="null"
                else
                    res(u,i)=*row[i]
                endif
            next
            u+=1
        END IF
    LOOP
end sub
function mysql_type.query(sql as string) as MYSQL_RES ptr
    Dim result As Integer
    Dim Fehler as ZString Ptr
    result = mysql_real_query (this.mysqlptr,STRPTR(sql),LEN(sql))
    If result = 0 THEN
        return mysql_use_result(this.mysqlptr)
    ELSE
        Fehler = mysql_error(this.mysqlptr)
        If (*Fehler)[0] <> 0 Then
            PRINT " "; *mysql_error(this.mysqlptr)
        END IF
    END IF
end function
sub mysql_type.update(table as string,cols as string,byval where as string="")
    this.connect()
    Dim sQuery As String
    Dim ID As Integer
    Dim result As Integer
    Dim Fehler as ZString Ptr
    squery = "update "+table+" set "+cols
    if where <>"" then squery+=" where "+where
    result = mysql_real_query (this.mysqlptr,STRPTR(squery),LEN(squery))
    If result = 0 THEN
        ID = mysql_insert_id(this.mysqlptr)
        this.insertid= ID
    ELSE
        Fehler = mysql_error(this.mysqlptr)
        If (*Fehler)[0] <> 0 Then
            PRINT " "; *mysql_error(this.mysqlptr)
        END IF
    END IF
    this.disconnect()
end sub
sub mysql_type.tdelete(table as string,byval where as string="")
    this.connect()
    Dim sQuery As String
    Dim ID As Integer
    Dim result As Integer
    Dim Fehler as ZString Ptr
    squery = "delete from "+table
    if where <>"" then squery+=" where "+where
    result = mysql_real_query (this.mysqlptr,STRPTR(squery),LEN(squery))
    If result = 0 THEN
        ID = mysql_insert_id(this.mysqlptr)
        this.insertid= ID
    ELSE
        Fehler = mysql_error(this.mysqlptr)
        If (*Fehler)[0] <> 0 Then
            PRINT " "; *mysql_error(this.mysqlptr)
        END IF
    END IF
    this.disconnect()
end sub
sub mysql_type.drop(table as string)
    this.connect()
    Dim sQuery As String
    Dim ID As Integer
    Dim result As Integer
    Dim Fehler as ZString Ptr
    squery = "drop table "+table
    result = mysql_real_query (this.mysqlptr,STRPTR(squery),LEN(squery))
    If result = 0 THEN
        ID = mysql_insert_id(this.mysqlptr)
        this.insertid= ID
    ELSE
        Fehler = mysql_error(this.mysqlptr)
        If (*Fehler)[0] <> 0 Then
            PRINT " "; *mysql_error(this.mysqlptr)
        END IF
    END IF
    this.disconnect()
end sub
sub mysql_type.listcols(cols() as string,table as string)
    this.connect()
    dim sres as MYSQL_RES ptr
    this.fetch_array(cols(),this.query("SHOW COLUMNS FROM "+table))
    this.connect()
end sub
type current_type
    form as string
    declare function date() as string
    declare function time() as string
    declare function datetime() as string
end type
function current_type.date() as string
    return format(now,"yyyy-mm-dd")
end function
function current_type.time() as string
    return time
end function
function current_type.datetime() as string
    return format(now,"yyyy-mm-dd hh:mm:ss")
end function
dim shared as current_type current