Looping over column names in SQLite and running a command for each column name

sqlite

I'm trying to run the following command

UPDATE attachments SET colname=TRIM(colname);

on every column name in a SQLite table.

I found the following approach to getting a list of column names in
an answer to "How to get a list of column names on sqlite3 / iPhone?", namely

SELECT name FROM PRAGMA_TABLE_INFO('your_table');

which seems like a nice, clean, simple way to do it. But this method is specific to SQLite, per PRAGMA Statements.

In any case, I'd like a solution that runs entirely in SQLite. I'm actually calling SQLite from Lua, using LuaSQL. So I could run the loop in Lua if necessary. But doing it inside SQLite just seems cleaner.

It would be even better if it's a general method that works for any RDBMS. But that may not be possible, and if it is, it would probably be more complicated

Best Answer

You can't do it in pure sqlite; column names have to be hardcoded in the query (also why they can't be bound to parameters), even if there was a way to use UPDATE in a loop, which there isn't.

Related Question