Spanish Optimizar Clase para Base de datos SqLite

carlos7000

Well-Known Member
Licensed User
Longtime User
Escribí un clase para poder administrar los datos que emplea una aplicación. La clase funciona pero creo que es ineficiente. He leído varios turoriales de SqLite, pero con la mayoría esta en ingles. Aunque entiendo bastante hay frases que me son imposibles y no he podido mejorar la clase para el manejo de la base de datos.

Les agradeciri su ayuda para mejorar la Clase

Este es el código:

B4X:
Sub Class_Globals
    Private Sql1 As SQL
End Sub

'Inicializa el objecto. Puede agregar parámetros al metodo si lo necesita.
Public Sub Initialize(DataBase As String)
    
    If (File.Exists(File.DirAssets, DataBase)) Then
        If Not (File.Exists(File.DirDefaultExternal, DataBase))Then
            Try
                File.Copy(File.DirAssets, DataBase, File.DirDefaultExternal, DataBase)
            Catch
                ToastMessageShow(LastException, True)
            End Try
        End If
    End If
    
    If File.Exists(File.DirDefaultExternal, DataBase) Then
        Sql1.Initialize(File.DirDefaultExternal, DataBase, True)
    End If
End Sub

Public Sub GetApiKey () As String
    Dim Id As Int
    Dim Query As String
    
    Id = 1
    
    Query = "SELECT * FROM `Api` WHERE Id = " & Id
    Dim Key As String
    
    Dim Cursor As Cursor

    Try
        If Sql1.IsInitialized Then
            Cursor = Sql1.ExecQuery(Query)
        End If
    Catch
        ToastMessageShow(LastException, True)
    End Try

    If Cursor.IsInitialized Then
        If Cursor.RowCount > 0 Then
            For i = 0 To Cursor.RowCount - 1
                Cursor.Position = i
                Key =  Cursor.GetString("Key")
            Next
        End If
    End If
    
    If Key == Null Then
        Key = ""
    End If

    Return Key
End Sub

Public Sub GetApiSecret () As String
    Dim Id As Int
    Dim Query As String
    
    Id = 1
    
    Query = "SELECT * FROM `Api` WHERE Id = " & Id
    Dim Secret As String
    
    Dim Cursor As Cursor

    Try
        If Sql1.IsInitialized Then
            Cursor = Sql1.ExecQuery(Query)
        End If
    Catch
        ToastMessageShow(LastException, True)
    End Try
    
    If Cursor.IsInitialized Then
        If Cursor.RowCount > 0 Then
            For i = 0 To Cursor.RowCount - 1
                Cursor.Position = i
                Secret =  Cursor.GetString("Secret")
            Next
        End If
    End If
    
    If Secret == Null Then
        Secret = ""
    End If

    Return Secret
End Sub

Public Sub UpdateAsk(Ask As String)
    Dim Query As String
    Query = "UPDATE `Data` SET `Ask`='" & Ask & "' WHERE `_rowid_`='1';"
    
    Try
        Sql1.ExecNonQuery(Query)
    Catch
        ToastMessageShow(LastException, True)
        Log(LastException)
    End Try
End Sub

Public Sub GetAsk () As Double
    Try
        Dim id As Int
        Dim Query As String
    
        id  = 1
        Query = "SELECT * FROM `Data` WHERE Id = " & id
        Dim Ask As String
        
        Dim Cursor As Cursor

        Try
            If Sql1.IsInitialized Then
                Cursor = Sql1.ExecQuery(Query)
            End If
        Catch
            ToastMessageShow(LastException, True)
        End Try
    
        If Cursor.IsInitialized Then
            If Cursor.RowCount > 0 Then
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    Ask =  Cursor.GetString("Ask")
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try

'    If (Ancla == Null) Then
'        Ancla = "0"
'    End If
    
    Return Ask
End Sub

Public Sub UpdateCoinsAvailables(CoinsAvailables As String)
    Dim Query As String
    Query = "UPDATE `Data` SET `CoinsAvailables`='" & CoinsAvailables & "' WHERE `_rowid_`='1';"
    
    Try
        Sql1.ExecNonQuery(Query)
    Catch
        ToastMessageShow(LastException, True)
        Log(LastException)
    End Try
End Sub

Public Sub GetCoinsAvailables () As Double
    Try
        Dim id As Int
        Dim Query As String
    
        id  = 1
        Query = "SELECT * FROM `Data` WHERE Id = " & id
        Dim CoinsAvailables As String
        
        Dim Cursor As Cursor

        Try
            If Sql1.IsInitialized Then
                Cursor = Sql1.ExecQuery(Query)
            End If
        Catch
            ToastMessageShow(LastException, True)
        End Try
    
        If Cursor.IsInitialized Then
            If Cursor.RowCount > 0 Then
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    CoinsAvailables =  Cursor.GetString("CoinsAvailables")
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try

'    If (Ancla == Null) Then
'        Ancla = "0"
'    End If
    
    Return CoinsAvailables
End Sub

Public Sub UpdateUsdAvailable(UsdAvailable As String)
    Dim Query As String
    Query = "UPDATE `Data` SET `UsdAvailable`='" & UsdAvailable & "' WHERE `_rowid_`='1';"
    
    Try
        Sql1.ExecNonQuery(Query)
    Catch
        ToastMessageShow(LastException, True )
        Log(LastException)
    End Try
End Sub

Public Sub GetUsdAvailable () As Double
    Try
        Dim id As Int
        Dim Query As String
    
        id  = 1
        Query = "SELECT * FROM `Data` WHERE Id = " & id
        Dim UsdAvailable As String
    
        Dim Cursor As Cursor

        Try
            If Sql1.IsInitialized Then
                Cursor = Sql1.ExecQuery(Query)
            End If
        Catch
            ToastMessageShow(LastException, True)
        End Try
    
        If Cursor.IsInitialized Then
            If Cursor.RowCount > 0 Then
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    UsdAvailable =  Cursor.GetString("UsdAvailable")
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try

    If (UsdAvailable == Null) Then
        UsdAvailable = "0"
    End If
    
    Return UsdAvailable
End Sub

Public Sub UpdateAncla(Ancla As String)
    Dim Query As String
    Query = "UPDATE `Data` SET `Ancla`='" & Ancla & "' WHERE `_rowid_`='1';"
    
    Try
        Sql1.ExecNonQuery(Query)
    Catch
        ToastMessageShow(LastException, True )
        Log(LastException)
    End Try
End Sub

Public Sub GetAncla () As Double
    Try
        Dim id As Int
        Dim Query As String
    
        id  = 1
        Query = "SELECT * FROM `Data` WHERE Id = " & id
        Dim Ancla As String
    
        Dim Cursor As Cursor

        Try
            If Sql1.IsInitialized Then
                Cursor = Sql1.ExecQuery(Query)
            End If
        Catch
            ToastMessageShow(LastException, True)
        End Try
    
        If Cursor.IsInitialized Then
            If Cursor.RowCount > 0 Then
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    Ancla =  Cursor.GetString("Ancla")
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try

'    If (Ancla == Null) Then
'        Ancla = "0"
'    End If
    
    Return Ancla
End Sub


Public Sub UpdateRequests(Requests As String)
    Dim Query As String
    Query = "UPDATE `Data` SET `Requests`='" & Requests & "' WHERE `_rowid_`='1';"
    
    Try
        Sql1.ExecNonQuery(Query)
    Catch
        ToastMessageShow(LastException, True )
        Log(LastException)
    End Try
End Sub

Public Sub GetRequests () As Long
    Try
        Dim id As Int
        Dim Query As String
    
        id  = 1
        Query = "SELECT * FROM `Data` WHERE Id = " & id
        Dim Requests As String
    
        Dim Cursor As Cursor

        Try
            If Sql1.IsInitialized Then
                Cursor = Sql1.ExecQuery(Query)
            End If
        Catch
            ToastMessageShow(LastException, True)
        End Try
    
        If Cursor.IsInitialized Then
            If Cursor.RowCount > 0 Then
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    Requests =  Cursor.GetString("Requests")
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try

    If (Requests == Null) Then
        Requests = "0"
    End If
    
    Return Requests
End Sub

Public Sub UpdateBtcAvailable(BtcAvailable As String)
    Dim Query As String
    Query = "UPDATE `Data` SET `BtcAvailable`='" & BtcAvailable & "' WHERE `_rowid_`='1';"
    
    Try
        Sql1.ExecNonQuery(Query)
    Catch
        ToastMessageShow(LastException, True )
        Log(LastException)
    End Try
End Sub

Public Sub GetBtcAvailable () As Double
    Try
        Dim id As Int
        Dim Query As String
    
        id  = 1
        Query = "SELECT * FROM `Data` WHERE Id = " & id
        Dim BtcAvailable As String
    
        Dim Cursor As Cursor

        Try
            If Sql1.IsInitialized Then
                Cursor = Sql1.ExecQuery(Query)
            End If
        Catch
            ToastMessageShow(LastException, True)
        End Try
    
        If Cursor.IsInitialized Then
            If Cursor.RowCount > 0 Then
                For i = 0 To Cursor.RowCount - 1
                    Cursor.Position = i
                    BtcAvailable =  Cursor.GetString("BtcAvailable")
                Next
            End If
        End If
    Catch
        Log(LastException)
    End Try

'    If (Ancla == Null) Then
'        Ancla = "0"
'    End If
    
    Return BtcAvailable
End Sub
 
Top