Topic: check if column exist in table

Hi all, How can I check by script if a column exist in a table. and if not how to create it ?

Re: check if column exist in table

Attached is a sample way of doing it through script. The table is named TestTable with one column named Field1. The form has a button to check for Field2 column existence and if it does not exist, it can add it. I am using the form for user interaction, but typically you would probably do this without user interaction. You can do this behind the scenes without showing the form at all.

Post's attachments

Attachment icon Column Exists.zip 336.45 kb, 177 downloads since 2021-08-18 

Re: check if column exist in table

thank you ehwanger.  I am not so familiar with SQL
I do not understand the line  SqlQuery('pragma table_info(TestTable)',ColResults);

as I understand the Test_table is the name of the table and the Field2 is the name of the  record


what is the pragma??
thank you

4 (edited by ehwagner 2021-08-19 03:41:37)

Re: check if column exist in table

Pragma is a sqlite function that can be used to retrieve various technical info about the database. Below is a link for more information about its specific uses.


[https://sqlite.org/pragma.html]


The SqlQuery using the pragma function retrieves all the columns of a table and puts them in a dataset called ColResults. Then using the while statement I go through each row of the dataset which in this case are the columns in the specified table. I check for the col name of Field2. If it finds the column name Field2 it sets the boolean field FieldExists to TRUE otherwise it remains FALSE and can be added to the table in the ALTER statement. Hope this helps.

Re: check if column exist in table

Yes now it's clear. Thank's again