Android Code Snippet Check if a column exists in SQLite database (DBUtils)

SubName: FieldExists
Author:
asales
Description: checks if a column exists in a SQLite database

B4X:
Sub FieldExists(SQL As SQL, TableName As String, TableField As String) As Boolean
    Dim result As Boolean
    result = False

    Dim t As List
    t = ExecuteMemoryTable(SQL, "PRAGMA table_info ('" & TableName & "')", Null, 0)

    For i = 0 To t.Size - 1
        Dim values() As String
        values = t.Get(i)

        If values(1).ToLowerCase = TableField.ToLowerCase Then  ' case insensitive
            result = True
            Exit
        End If
    Next

    Return result
End Sub

Usage:
B4X:
If DBUtils.FieldExists(SQL1,"MyTable","NAME") = True Then
    Log("Field EXISTS!")
Else
    Log("Field DO NOT exists.")
End If

Dependencies: DBUtils (ExecuteMemoryTable)
Tags: DBUtils, Table, Tables, Column, Columns, Field, Fields, Database, DB, SQLite
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
thank you for this. Nice one.

Can you please update your post #1 to match the rules of this forum (using the right keywords)? It would be easier to find the right answer for the b4a search engine using them...
 
Top