⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 sqlite-3.4.1,嵌入式数据库.是一个功能强大的开源数据库,给学习和研发以及小型公司的发展带来了全所未有的好处.
💻 C
📖 第 1 页 / 共 4 页
字号:
               w = 10;            }            fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"                   "----------------------------------------------------------",                    i==nArg-1 ? "\n": "  ");          }        }      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        int w;        if( i<ArraySize(p->actualWidth) ){           w = p->actualWidth[i];        }else{           w = 10;        }        fprintf(p->out,"%-*.*s%s",w,w,            azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");      }      break;    }    case MODE_Semi:    case MODE_List: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator);        }      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        char *z = azArg[i];        if( z==0 ) z = p->nullvalue;        fprintf(p->out, "%s", z);        if( i<nArg-1 ){          fprintf(p->out, "%s", p->separator);        }else if( p->mode==MODE_Semi ){          fprintf(p->out, ";\n");        }else{          fprintf(p->out, "\n");        }      }      break;    }    case MODE_Html: {      if( p->cnt++==0 && p->showHeader ){        fprintf(p->out,"<TR>");        for(i=0; i<nArg; i++){          fprintf(p->out,"<TH>%s</TH>",azCol[i]);        }        fprintf(p->out,"</TR>\n");      }      if( azArg==0 ) break;      fprintf(p->out,"<TR>");      for(i=0; i<nArg; i++){        fprintf(p->out,"<TD>");        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);        fprintf(p->out,"</TD>\n");      }      fprintf(p->out,"</TR>\n");      break;    }    case MODE_Tcl: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          output_c_string(p->out,azCol[i] ? azCol[i] : "");          fprintf(p->out, "%s", p->separator);        }        fprintf(p->out,"\n");      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue);        fprintf(p->out, "%s", p->separator);      }      fprintf(p->out,"\n");      break;    }    case MODE_Csv: {      if( p->cnt++==0 && p->showHeader ){        for(i=0; i<nArg; i++){          output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);        }        fprintf(p->out,"\n");      }      if( azArg==0 ) break;      for(i=0; i<nArg; i++){        output_csv(p, azArg[i], i<nArg-1);      }      fprintf(p->out,"\n");      break;    }    case MODE_Insert: {      if( azArg==0 ) break;      fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable);      for(i=0; i<nArg; i++){        char *zSep = i>0 ? ",": "";        if( azArg[i]==0 ){          fprintf(p->out,"%sNULL",zSep);        }else if( isNumber(azArg[i], 0) ){          fprintf(p->out,"%s%s",zSep, azArg[i]);        }else{          if( zSep[0] ) fprintf(p->out,"%s",zSep);          output_quoted_string(p->out, azArg[i]);        }      }      fprintf(p->out,");\n");      break;    }  }  return 0;}/*** Set the destination table field of the callback_data structure to** the name of the table given.  Escape any quote characters in the** table name.*/static void set_table_name(struct callback_data *p, const char *zName){  int i, n;  int needQuote;  char *z;  if( p->zDestTable ){    free(p->zDestTable);    p->zDestTable = 0;  }  if( zName==0 ) return;  needQuote = !isalpha((unsigned char)*zName) && *zName!='_';  for(i=n=0; zName[i]; i++, n++){    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){      needQuote = 1;      if( zName[i]=='\'' ) n++;    }  }  if( needQuote ) n += 2;  z = p->zDestTable = malloc( n+1 );  if( z==0 ){    fprintf(stderr,"Out of memory!\n");    exit(1);  }  n = 0;  if( needQuote ) z[n++] = '\'';  for(i=0; zName[i]; i++){    z[n++] = zName[i];    if( zName[i]=='\'' ) z[n++] = '\'';  }  if( needQuote ) z[n++] = '\'';  z[n] = 0;}/* zIn is either a pointer to a NULL-terminated string in memory obtained** from malloc(), or a NULL pointer. The string pointed to by zAppend is** added to zIn, and the result returned in memory obtained from malloc().** zIn, if it was not NULL, is freed.**** If the third argument, quote, is not '\0', then it is used as a ** quote character for zAppend.*/static char *appendText(char *zIn, char const *zAppend, char quote){  int len;  int i;  int nAppend = strlen(zAppend);  int nIn = (zIn?strlen(zIn):0);  len = nAppend+nIn+1;  if( quote ){    len += 2;    for(i=0; i<nAppend; i++){      if( zAppend[i]==quote ) len++;    }  }  zIn = (char *)realloc(zIn, len);  if( !zIn ){    return 0;  }  if( quote ){    char *zCsr = &zIn[nIn];    *zCsr++ = quote;    for(i=0; i<nAppend; i++){      *zCsr++ = zAppend[i];      if( zAppend[i]==quote ) *zCsr++ = quote;    }    *zCsr++ = quote;    *zCsr++ = '\0';    assert( (zCsr-zIn)==len );  }else{    memcpy(&zIn[nIn], zAppend, nAppend);    zIn[len-1] = '\0';  }  return zIn;}/*** Execute a query statement that has a single result column.  Print** that result column on a line by itself with a semicolon terminator.**** This is used, for example, to show the schema of the database by** querying the SQLITE_MASTER table.*/static int run_table_dump_query(FILE *out, sqlite3 *db, const char *zSelect){  sqlite3_stmt *pSelect;  int rc;  rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);  if( rc!=SQLITE_OK || !pSelect ){    return rc;  }  rc = sqlite3_step(pSelect);  while( rc==SQLITE_ROW ){    fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));    rc = sqlite3_step(pSelect);  }  return sqlite3_finalize(pSelect);}/*** This is a different callback routine used for dumping the database.** Each row received by this callback consists of a table name,** the table type ("index" or "table") and SQL to create the table.** This routine should print text sufficient to recreate the table.*/static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){  int rc;  const char *zTable;  const char *zType;  const char *zSql;  struct callback_data *p = (struct callback_data *)pArg;  if( nArg!=3 ) return 1;  zTable = azArg[0];  zType = azArg[1];  zSql = azArg[2];    if( strcmp(zTable, "sqlite_sequence")==0 ){    fprintf(p->out, "DELETE FROM sqlite_sequence;\n");  }else if( strcmp(zTable, "sqlite_stat1")==0 ){    fprintf(p->out, "ANALYZE sqlite_master;\n");  }else if( strncmp(zTable, "sqlite_", 7)==0 ){    return 0;  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){    char *zIns;    if( !p->writableSchema ){      fprintf(p->out, "PRAGMA writable_schema=ON;\n");      p->writableSchema = 1;    }    zIns = sqlite3_mprintf(       "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"       "VALUES('table','%q','%q',0,'%q');",       zTable, zTable, zSql);    fprintf(p->out, "%s\n", zIns);    sqlite3_free(zIns);    return 0;  }else{    fprintf(p->out, "%s;\n", zSql);  }  if( strcmp(zType, "table")==0 ){    sqlite3_stmt *pTableInfo = 0;    char *zSelect = 0;    char *zTableInfo = 0;    char *zTmp = 0;       zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);    zTableInfo = appendText(zTableInfo, zTable, '"');    zTableInfo = appendText(zTableInfo, ");", 0);    rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);    if( zTableInfo ) free(zTableInfo);    if( rc!=SQLITE_OK || !pTableInfo ){      return 1;    }    zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);    zTmp = appendText(zTmp, zTable, '"');    if( zTmp ){      zSelect = appendText(zSelect, zTmp, '\'');    }    zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);    rc = sqlite3_step(pTableInfo);    while( rc==SQLITE_ROW ){      const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);      zSelect = appendText(zSelect, "quote(", 0);      zSelect = appendText(zSelect, zText, '"');      rc = sqlite3_step(pTableInfo);      if( rc==SQLITE_ROW ){        zSelect = appendText(zSelect, ") || ',' || ", 0);      }else{        zSelect = appendText(zSelect, ") ", 0);      }    }    rc = sqlite3_finalize(pTableInfo);    if( rc!=SQLITE_OK ){      if( zSelect ) free(zSelect);      return 1;    }    zSelect = appendText(zSelect, "|| ')' FROM  ", 0);    zSelect = appendText(zSelect, zTable, '"');    rc = run_table_dump_query(p->out, p->db, zSelect);    if( rc==SQLITE_CORRUPT ){      zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);      rc = run_table_dump_query(p->out, p->db, zSelect);    }    if( zSelect ) free(zSelect);  }  return 0;}/*** Run zQuery.  Use dump_callback() as the callback routine so that** the contents of the query are output as SQL statements.**** If we get a SQLITE_CORRUPT error, rerun the query after appending** "ORDER BY rowid DESC" to the end.*/static int run_schema_dump_query(  struct callback_data *p,   const char *zQuery,  char **pzErrMsg){  int rc;  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);  if( rc==SQLITE_CORRUPT ){    char *zQ2;    int len = strlen(zQuery);    if( pzErrMsg ) sqlite3_free(*pzErrMsg);    zQ2 = malloc( len+100 );    if( zQ2==0 ) return rc;    sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);    free(zQ2);  }  return rc;}/*** Text of a help message*/static char zHelp[] =  ".bail ON|OFF           Stop after hitting an error.  Default OFF\n"  ".databases             List names and files of attached databases\n"  ".dump ?TABLE? ...      Dump the database in an SQL text format\n"  ".echo ON|OFF           Turn command echo on or off\n"  ".exit                  Exit this program\n"  ".explain ON|OFF        Turn output mode suitable for EXPLAIN on or off.\n"  ".header(s) ON|OFF      Turn display of headers on or off\n"  ".help                  Show this message\n"  ".import FILE TABLE     Import data from FILE into TABLE\n"  ".indices TABLE         Show names of all indices on TABLE\n"#ifdef SQLITE_ENABLE_IOTRACE  ".iotrace FILE          Enable I/O diagnostic logging to FILE\n"#endif#ifndef SQLITE_OMIT_LOAD_EXTENSION  ".load FILE ?ENTRY?     Load an extension library\n"#endif  ".mode MODE ?TABLE?     Set output mode where MODE is one of:\n"  "                         csv      Comma-separated values\n"  "                         column   Left-aligned columns.  (See .width)\n"  "                         html     HTML <table> code\n"  "                         insert   SQL insert statements for TABLE\n"  "                         line     One value per line\n"  "                         list     Values delimited by .separator string\n"  "                         tabs     Tab-separated values\n"  "                         tcl      TCL list elements\n"  ".nullvalue STRING      Print STRING in place of NULL values\n"  ".output FILENAME       Send output to FILENAME\n"  ".output stdout         Send output to the screen\n"  ".prompt MAIN CONTINUE  Replace the standard prompts\n"  ".quit                  Exit this program\n"  ".read FILENAME         Execute SQL in FILENAME\n"  ".schema ?TABLE?        Show the CREATE statements\n"  ".separator STRING      Change separator used by output mode and .import\n"  ".show                  Show the current values for various settings\n"  ".tables ?PATTERN?      List names of tables matching a LIKE pattern\n"  ".timeout MS            Try opening locked tables for MS milliseconds\n"  ".width NUM NUM ...     Set column widths for \"column\" mode\n";/* Forward reference */static int process_input(struct callback_data *p, FILE *in);/*** Make sure the database is open.  If it is not, then open it.  If** the database fails to open, print an error message and exit.*/static void open_db(struct callback_data *p){  if( p->db==0 ){    sqlite3_open(p->zDbFilename, &p->db);    db = p->db;    sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,        shellstaticFunc, 0, 0);    if( SQLITE_OK!=sqlite3_errcode(db) ){      fprintf(stderr,"Unable to open database \"%s\": %s\n",           p->zDbFilename, sqlite3_errmsg(db));      exit(1);    }#ifndef SQLITE_OMIT_LOAD_EXTENSION    sqlite3_enable_load_extension(p->db, 1);#endif  }}/*** Do C-language style dequoting.****    \t    -> tab**    \n    -> newline**    \r    -> carriage return**    \NNN  -> ascii character NNN in octal**    \\    -> backslash*/static void resolve_backslashes(char *z){  int i, j, c;  for(i=j=0; (c = z[i])!=0; i++, j++){    if( c=='\\' ){      c = z[++i];      if( c=='n' ){        c = '\n';      }else if( c=='t' ){        c = '\t';      }else if( c=='r' ){        c = '\r';      }else if( c>='0' && c<='7' ){        c -= '0';        if( z[i+1]>='0' && z[i+1]<='7' ){          i++;          c = (c<<3) + z[i] - '0';          if( z[i+1]>='0' && z[i+1]<='7' ){            i++;            c = (c<<3) + z[i] - '0';          }        }      }    }    z[j] = c;  }  z[j] = 0;}/*** Interpret zArg as a boolean value.  Return either 0 or 1.*/static int booleanValue(char *zArg){  int val = atoi(zArg);  int j;  for(j=0; zArg[j]; j++){    zArg[j] = tolower(zArg[j]);  }  if( strcmp(zArg,"on")==0 ){    val = 1;  }else if( strcmp(zArg,"yes")==0 ){    val = 1;  }  return val;}/*** If an input line begins with "." then invoke this routine to** process that line.**** Return 1 on error, 2 to exit, and 0 otherwise.*/static int do_meta_command(char *zLine, struct callback_data *p){  int i = 1;  int nArg = 0;  int n, c;  int rc = 0;  char *azArg[50];  /* Parse the input line into tokens.  */  while( zLine[i] && nArg<ArraySize(azArg) ){    while( isspace((unsigned char)zLine[i]) ){ i++; }    if( zLine[i]==0 ) break;    if( zLine[i]=='\'' || zLine[i]=='"' ){      int delim = zLine[i++];      azArg[nArg++] = &zLine[i];      while( zLine[i] && zLine[i]!=delim ){ i++; }      if( zLine[i]==delim ){        zLine[i++] = 0;      }      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);    }else{      azArg[nArg++] = &zLine[i];      while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }      if( zLine[i] ) zLine[i++] = 0;      resolve_backslashes(azArg[nArg-1]);    }  }  /* Process the input line.  */  if( nArg==0 ) return rc;  n = strlen(azArg[0]);  c = azArg[0][0];  if( c=='b' && n>1 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){    bail_on_error = booleanValue(azArg[1]);  }else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -