C 如何通过更改SQLite3中的现有SQL表,通过单个SQL语句添加更多列?
不,不可能在一条sql语句中完成,可以在一行多条语句中完成。在许多sql方言中,可以使用C 如何通过更改SQLite3中的现有SQL表,通过单个SQL语句添加更多列?,c,database,sqlite,C,Database,Sqlite,不,不可能在一条sql语句中完成,可以在一行多条语句中完成。在许多sql方言中,可以使用ALTER TABLE name ADD col1 INTEGER、col2 CHAR(10)、col3 DATE作为一条语句。SQLite似乎不允许这样做,SQL标准也不允许这样做(至少在SQL 2003之前)。 #include <stdio.h> /* needed for vsnprintf */ #include <stdlib.h> /* needed for malloc
ALTER TABLE name ADD col1 INTEGER、col2 CHAR(10)、col3 DATE
作为一条语句。SQLite似乎不允许这样做,SQL标准也不允许这样做(至少在SQL 2003之前)。
#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);
}
}
- 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.