【问题标题】:How to add more columns through a single SQL statement by changing the existing SQL table in SQLite3? [duplicate]如何通过更改 SQLite3 中现有的 SQL 表,通过单个 SQL 语句添加更多列? [复制]
【发布时间】: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)。

标签: c database sqlite


【解决方案1】:

不,你不可能在 1 条 sql 语句中做到这一点,你可以在一行中用多条语句做到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2019-05-24
    • 2018-09-08
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    相关资源
    最近更新 更多