diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/shell.c.in | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/src/shell.c.in b/src/shell.c.in index 354c9a849..90445c9cf 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -8827,7 +8827,7 @@ static int do_meta_command(char *zLine, ShellState *p){ if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ char *zTable = 0; /* Insert data into this table */ - char *zSchema = "main"; /* within this schema */ + char *zSchema = 0; /* within this schema (may default to "main") */ char *zFile = 0; /* Name of file to extra content from */ sqlite3_stmt *pStmt = NULL; /* A statement */ int nCol; /* Number of columns in the table */ @@ -8963,7 +8963,6 @@ static int do_meta_command(char *zLine, ShellState *p){ 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; @@ -8975,11 +8974,17 @@ static int do_meta_command(char *zLine, ShellState *p){ 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 ){} } - zSql = sqlite3_mprintf("SELECT * FROM \"%w\".\"%w\"", zSchema, zTable); - if( zSql==0 ){ + 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 ){ import_cleanup(&sCtx); shell_out_of_memory(); } @@ -8987,8 +8992,7 @@ static int do_meta_command(char *zLine, ShellState *p){ 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 \"%w\".\"%w\"", - zSchema, zTable); + char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zSchema); sqlite3 *dbCols = 0; char *zRenames = 0; char *zColDefs; @@ -9005,9 +9009,12 @@ static int do_meta_command(char *zLine, ShellState *p){ } 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; } @@ -9018,22 +9025,18 @@ static int do_meta_command(char *zLine, ShellState *p){ 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); - import_cleanup(&sCtx); - rc = 1; - goto meta_command_exit; + goto import_fail; } 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)); - import_cleanup(&sCtx); - rc = 1; - goto meta_command_exit; + goto import_fail; } + sqlite3_free(zSql); nCol = sqlite3_column_count(pStmt); sqlite3_finalize(pStmt); pStmt = 0; @@ -9043,8 +9046,7 @@ static int do_meta_command(char *zLine, ShellState *p){ import_cleanup(&sCtx); shell_out_of_memory(); } - sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?", - zSchema, zTable); + sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zSchema); j = strlen30(zSql); for(i=1; i<nCol; i++){ zSql[j++] = ','; @@ -9056,14 +9058,13 @@ static int do_meta_command(char *zLine, ShellState *p){ 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); - import_cleanup(&sCtx); - rc = 1; - goto meta_command_exit; + goto import_fail; } + sqlite3_free(zSql); + sqlite3_free(zSchema); needCommit = sqlite3_get_autocommit(p->db); if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); do{ |