0
#include <stdio.h> /* needed for vsnprintf */
#include <stdlib.h> /* needed for malloc-free */
#include <string.h>
#include <sqlite3.h>

 char *table[10]    = { "John", "peter", "Nolan" };
 int   i ;
 for(i=0; i<3; i++)
 {
           char *sql1 = "ALTER TABLE names ADD column '%s'",table[i];    
           rc = sqlite3_exec(db, sql1, callback, 0, &zErrMsg);
           if (rc != SQLITE_OK )
           {
                printf("Error: %s:Unable to ALTER the table\n", zErrMsg);
           }
 }
  • In the above code to add '3' columns(such as John, Peter, Nolan) I re-run SQL statement for '3' times.
  • Is it possible to add above '3' columns through a single SQL statement (or) any other API is available?

NOTE: I don't want to do the following things

       - Using a loop statement to add the columns.

       - Executing many SQL statements to add the columns.

       - Backup of the existing database and renaming it after adding the columns.
  • https://www.sqlite.org/lang_altertable.html – qrdl Dec 20 '19 at 09:04
  • In many dialects of SQL, you could use `ALTER TABLE name ADD col1 INTEGER, col2 CHAR(10), col3 DATE` as a single statement. It appears that SQLite doesn't allow that — and neither does the SQL standard (up to SQL 2003, at any rate). – Jonathan Leffler Dec 20 '19 at 14:27

1 Answers1

1

No, there is no way you can do it in 1 single sql statement, you could do it in one line with mutiple statements.

BobRun
  • 756
  • 5
  • 12