SQLite

Check-in Differences
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From bf9d1278846dce92 To 4838b888e431f794

2022-03-07
01:28
Fix the .import problem in the CLI reported by forum post 72a024c957. (check-in: d753285b user: drh tags: branch-3.38)
00:14
Disentangle variable use in last checkin (check-in: 4c3a0260 user: larrybr tags: trunk)
2022-03-06
23:41
For CLI .import, revert to importing into temp or main when given table is found there and no -schema option used. And plug an obscure leak. (check-in: bf9d1278 user: larrybr tags: trunk)
20:22
Fix obsolete but harmless comments in btree. No changes to code. (check-in: 4838b888 user: drh tags: trunk)
11:43
The sqlite_offset() function should be non-deterministic. dbsqlfuzz 3df8230bb940870db87ffca2c0fc759c1e7fa356. (check-in: e1a185e6 user: drh tags: trunk)

Changes to src/shell.c.in.

8823
8824
8825
8826
8827
8828
8829
8830

8831
8832
8833
8834
8835
8836
8837
8823
8824
8825
8826
8827
8828
8829

8830
8831
8832
8833
8834
8835
8836
8837







-
+







    }else{
      showHelp(p->out, 0);
    }
  }else

  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
    char *zTable = 0;           /* Insert data into this table */
    char *zSchema = 0;          /* within this schema (may default to "main") */
    char *zSchema = "main";     /* within this schema */
    char *zFile = 0;            /* Name of file to extra content from */
    sqlite3_stmt *pStmt = NULL; /* A statement */
    int nCol;                   /* Number of columns in the table */
    int nByte;                  /* Number of bytes in an SQL string */
    int i, j;                   /* Loop counters */
    int needCommit;             /* True to COMMIT or ROLLBACK at end */
    int nSep;                   /* Number of bytes in p->colSeparator[] */
8959
8960
8961
8962
8963
8964
8965

8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987


8988
8989
8990
8991
8992
8993
8994
8995


8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017

9018
9019
9020
9021
9022
9023
9024
9025
9026
9027

9028



9029
9030
9031
9032
9033

9034
9035
9036
9037



9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049


9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060

9061
9062
9063
9064



9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977

8978
8979
8980







8981
8982
8983
8984
8985
8986
8987
8988
8989

8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007


9008


9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021

9022
9023
9024
9025
9026

9027
9028
9029
9030
9031
9032

9033
9034
9035
9036

9037
9038
9039
9040
9041
9042
9043
9044
9045

9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062

9063
9064
9065
9066


9067
9068
9069
9070
9071
9072
9073







+











-



-
-
-
-
-
-
-
+
+







-
+
+
















-
-

-
-

+










+
-
+
+
+


-


+



-
+
+
+

-









-
+
+











+



-
+
+
+

-
-







    }
    if( sCtx.in==0 ){
      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
      rc = 1;
      import_cleanup(&sCtx);
      goto meta_command_exit;
    }
    /* Below, resources must be freed before exit. */
    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
      char zSep[2];
      zSep[1] = 0;
      zSep[0] = sCtx.cColSep;
      utf8_printf(p->out, "Column separator ");
      output_c_string(p->out, zSep);
      utf8_printf(p->out, ", row separator ");
      zSep[0] = sCtx.cRowSep;
      output_c_string(p->out, zSep);
      utf8_printf(p->out, "\n");
    }
    /* Below, resources must be freed before exit. */
    while( (nSkip--)>0 ){
      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
    }
    if( zSchema!=0 ){
      zSchema = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
    }else{
      zSchema = sqlite3_mprintf("\"%w\"", zTable);
    }
    zSql = sqlite3_mprintf("SELECT * FROM %s", zSchema);
    if( zSql==0 || zSchema==0 ){
    zSql = sqlite3_mprintf("SELECT * FROM \"%w\".\"%w\"", zSchema, zTable);
    if( zSql==0 ){
      import_cleanup(&sCtx);
      shell_out_of_memory();
    }
    nByte = strlen30(zSql);
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
      char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zSchema);
      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
                                      zSchema, zTable);
      sqlite3 *dbCols = 0;
      char *zRenames = 0;
      char *zColDefs;
      while( xRead(&sCtx) ){
        zAutoColumn(sCtx.z, &dbCols, 0);
        if( sCtx.cTerm!=sCtx.cColSep ) break;
      }
      zColDefs = zAutoColumn(0, &dbCols, &zRenames);
      if( zRenames!=0 ){
        utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
                    "Columns renamed during .import %s due to duplicates:\n"
                    "%s\n", sCtx.zFile, zRenames);
        sqlite3_free(zRenames);
      }
      assert(dbCols==0);
      if( zColDefs==0 ){
        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
      import_fail:
        sqlite3_free(zCreate);
        sqlite3_free(zSql);
        sqlite3_free(zSchema);
        import_cleanup(&sCtx);
        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
        rc = 1;
        goto meta_command_exit;
      }
      zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
      if( eVerbose>=1 ){
        utf8_printf(p->out, "%s\n", zCreate);
      }
      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
      if( rc ){
        utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
        sqlite3_free(zCreate);
        goto import_fail;
        import_cleanup(&sCtx);
        rc = 1;
        goto meta_command_exit;
      }
      sqlite3_free(zCreate);
      zCreate = 0;
      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    }
    sqlite3_free(zSql);
    if( rc ){
      if (pStmt) sqlite3_finalize(pStmt);
      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
      goto import_fail;
      import_cleanup(&sCtx);
      rc = 1;
      goto meta_command_exit;
    }
    sqlite3_free(zSql);
    nCol = sqlite3_column_count(pStmt);
    sqlite3_finalize(pStmt);
    pStmt = 0;
    if( nCol==0 ) return 0; /* no columns, no error */
    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
    if( zSql==0 ){
      import_cleanup(&sCtx);
      shell_out_of_memory();
    }
    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zSchema);
    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?",
                     zSchema, zTable);
    j = strlen30(zSql);
    for(i=1; i<nCol; i++){
      zSql[j++] = ',';
      zSql[j++] = '?';
    }
    zSql[j++] = ')';
    zSql[j] = 0;
    if( eVerbose>=2 ){
      utf8_printf(p->out, "Insert using: %s\n", zSql);
    }
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
    if( rc ){
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
      if (pStmt) sqlite3_finalize(pStmt);
      goto import_fail;
      import_cleanup(&sCtx);
      rc = 1;
      goto meta_command_exit;
    }
    sqlite3_free(zSql);
    sqlite3_free(zSchema);
    needCommit = sqlite3_get_autocommit(p->db);
    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
    do{
      int startLine = sCtx.nLine;
      for(i=0; i<nCol; i++){
        char *z = xRead(&sCtx);
        /*

Changes to test/shell5.test.

258
259
260
261
262
263
264
265

266
267
268
269
270
271
272
258
259
260
261
262
263
264

265
266
267
268
269
270
271
272







-
+







  }
  close $in
  set res [catchcmd "test.db" {.mode csv
.import shell5.csv t3
SELECT COUNT(*) FROM t3;}]
} [list 0 $rows]

# Import from a pipe.  (Unix only, as it requires "awk")
# Inport from a pipe.  (Unix only, as it requires "awk")
if {$tcl_platform(platform)=="unix"} {
  do_test shell5-1.8 {
    forcedelete test.db
    catchcmd test.db {.mode csv
.import "|awk 'END{print \"x,y\";for(i=1;i<=5;i++){print i \",this is \" i}}'" t1
SELECT * FROM t1;}
  } {0 {1,"this is 1"
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
472
473
474
475
476
477
478














479
480
481
482
483
484
485







-
-
-
-
-
-
-
-
-
-
-
-
-
-







CREATE TABLE t8(a, b, c);
.import -skip 1 shell5.csv t8
.nullvalue #
  }]
  db eval { SELECT * FROM t8 }
} {1 2 3}

do_test shell5-4.4 {
  forcedelete shell5.csv
  set fd [open shell5.csv w]
  puts $fd "1,2,3"
  close $fd
  catchcmd test.db [string trim {
.mode csv
CREATE TEMP TABLE t8(a, b, c);
.import shell5.csv t8
.nullvalue #
SELECT * FROM temp.t8
  }]
} {0 1,2,3}

#----------------------------------------------------------------------------
# Tests for the shell automatic column rename.
#
db close

# Import columns containing duplicates
do_test shell5-5.1 {