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.
2
  • sqlite.org/lang_altertable.html Commented Dec 20, 2019 at 9: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). Commented Dec 20, 2019 at 14:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.