SQLite的简单使用

打开cmd 运行SQLite,进入sqlite

sqlite> .help

.backup ?DB? FILE Backup DB (default "main") to FILE

.bail ON|OFF Stop after hitting an error. Default OFF

.databases List names and files of attached databases

.dump ?TABLE? ... Dump the database in an SQL text format

If TABLE specified, only dump tables matching

LIKE pattern TABLE.

.echo ON|OFF Turn command echo on or off

.exit Exit this program

.explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.

With no args, it turns EXPLAIN on.

.header(s) ON|OFF Turn display of headers on or off

.help Show this message

.import FILE TABLE Import data from FILE into TABLE

.indices ?TABLE? Show names of all indices

If TABLE specified, only show indices for tables

matching LIKE pattern TABLE.

.load FILE ?ENTRY? Load an extension library

.log FILE|off Turn logging on or off. FILE can be stderr/stdout

.mode MODE ?TABLE? Set output mode where MODE is one of:

csv Comma-separated values

column Left-aligned columns. (See .width)

html HTML <table> code

insert SQL insert statements for TABLE

line One value per line

list Values delimited by .separator string

tabs Tab-separated values

tcl TCL list elements

.nullvalue STRING Print STRING in place of NULL values

.output FILENAME Send output to FILENAME

.output stdout Send output to the screen

.prompt MAIN CONTINUE Replace the standard prompts

.quit Exit this program

.read FILENAME Execute SQL in FILENAME

.restore ?DB? FILE Restore content of DB (default "main") from FILE

.schema ?TABLE? Show the CREATE statements

If TABLE specified, only show tables matching

LIKE pattern TABLE.

.separator STRING Change separator used by output mode and .import

.show Show the current values for various settings

.stats ON|OFF Turn stats on or off

.tables ?TABLE? List names of tables

If TABLE specified, only list tables matching

LIKE pattern TABLE.

.timeout MS Try opening locked tables for MS milliseconds

.width NUM1 NUM2 ... Set column widths for "column" mode

.timer ON|OFF Turn the CPU timer measurement on or off

sqlite>.exit

 

C:\>sqlite3 ex1 
SQLite version 3.7.6 
Enter ".help" for instructions 
Enter SQL statements terminated with a ";" 
sqlite> create table tbl1(id smallint,name varchar(10)); 
sqlite> insert into tbl1 values(1,'hello'); 
sqlite> insert into tbl1 values(2,'goodluck'); 
sqlite> select * from tbl1; 
1|hello 
2|goodluck 
sqlite> select name from tbl1 where id=2
goodluck 
sqlite> 
sqlite> .exit 
 
C:\>sqlite3 ex1 "select * from tbl1" 
1|hello 
2|goodluck

//将结果输出到一个文本文件

sqlite> .output text.txt

sqlite> select * from tbl1;

sqlite> .output stdout;

sqlite> .exit

 

G:\>print text.txt

1|hello

2|goodluck

3|nihao

 

=================================================================

 

Write Programs That Use SQLite 

 

Below is a simple TCL program that demonstrates how to use the TCL interface to SQLite. The program executes the SQL statements given as the second argument on the database defined by the first argument. The commands to watch for are the sqlite3 command on line 7 which opens an SQLite database and creates a new TCL command named "db" to access that database, the invocation of the db command on line 8 to execute SQL commands against the database, and the closing of the database connection on the last line of the script. 
 
 #!/usr/bin/tclsh 
 if {$argc!=2} { 
   puts stderr "Usage: %s DATABASE SQL-STATEMENT" 
    exit 1 
  } 
  load /usr/lib/tclsqlite3.so Sqlite307   
  sqlite3 db [lindex $argv 0
  db eval [lindex $argv 1] x { 
  foreach v $x(*) { 
      puts "$v = $x($v)" 
    } 
    puts "" 
  } 
  db close
 
 
Below is a simple C program that demonstrates how to use the C/C++ interface to SQLite. The name of a database is given by the first argument and the second argument is one or more SQL statements to execute against the database. The function calls to pay attention to here are the call to sqlite3_open() on line 22 which opens the database, sqlite3_exec() on line 28 that executes SQL commands against the database, and sqlite3_close() on line 33 that closes the database connection. 
 
See also the Introduction To The SQLite C/C++ Interface for an introductory overview and roadmap to the dozens of SQLite interface functions. 
 
  #include <stdio.h> 
  #include <sqlite3.h> 
   
  static int callback(void *NotUsed, int argc, char **argv, char **azColName){ 
    int i; 
    for(i=0; i<argc; i++){ 
      printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
    } 
    printf("\n"); 
    return 0
  } 
   
  int main(int argc, char **argv){ 
    sqlite3 *db; 
    char *zErrMsg = 0
    int rc; 
   
    if( argc!=3 ){ 
      fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]); 
      exit(1); 
    } 
    rc = sqlite3_open(argv[1], &db); 
    if( rc ){ 
      fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 
      sqlite3_close(db); 
      exit(1); 
    } 
    rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg); 
    if( rc!=SQLITE_OK ){ 
      fprintf(stderr, "SQL error: %s\n", zErrMsg); 
      sqlite3_free(zErrMsg); 
    } 
    sqlite3_close(db); 
    return 0
  }


 

原文链接: https://www.cnblogs.com/luowei010101/archive/2011/04/27/2030787.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    SQLite的简单使用

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/24662

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午2:30
下一篇 2023年2月8日 上午2:32

相关推荐