【发布时间】:2019-12-20 06:29:59
【问题描述】:
#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);
}
}
- 在上面添加“3”列的代码中(例如 John、Peter、Nolan)我重新运行 SQL 语句“3”次。
- 是否可以通过单个 SQL 语句添加以上“3”列(或)任何其他可用的 API?
注意:我不想做以下事情
- 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.
【问题讨论】:
-
在许多 SQL 方言中,您可以使用
ALTER TABLE name ADD col1 INTEGER, col2 CHAR(10), col3 DATE作为单个语句。似乎 SQLite 不允许这样做——SQL 标准也不允许(无论如何,直到 SQL 2003)。