鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 数据库 > mssql > >

SQLite数据库操作实践

来源:互联网 作者:佚名 时间:2015-03-13 11:34
SQLite数据库操作实践 SQLite存储类型 null - 空值 integer - 有符号整数,自增列定义 id integer primary key autoincrement real - 浮点数,存储为8-byte IEEE浮点数 text - 文本字符串 使用数据库编码(UTF-8, UTF-16BE 或 UTF-16LE)进行存储 blob - 二进

 SQLite数据库操作实践
SQLite存储类型
null - 空值 
integer - 有符号整数,自增列定义 id integer primary key autoincrement
real - 浮点数,存储为8-byte IEEE浮点数 
text - 文本字符串 使用数据库编码(UTF-8, UTF-16BE 或 UTF-16LE)进行存储
blob - 二进制数据,如图片、声音等等 
也可以接受其它数据类型例如datetime,varchar,nvarchar,decimal,float,double,bigint 等等

Last login: Tue Jan 7 19:05:33 2014 from 192.168.10.1
[myth@host10 ~]$ su
Password: 
[root@host10 myth]# pwd //显示当前工作目录

/home/myth

进入SQLite命令行创建数据库并且打开数据库

[root@host10 myth]# sqlite3 bookstore.db 
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file 
--- --------------- ----------------------------------------------------------
0 main /home/myth/bookstore.db 
sqlite> 
sqlite> .quit //退出SQLite命令行环境

[root@host10 myth]#

打开数据库

[root@host10 myth]# sqlite3 bookstore.db //如果数据库已经存在了,则打开数据库
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
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.
.genfkey ?OPTIONS? Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source 
distribution for further information.
.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
.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
.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 NUM NUM ... Set column widths for "column" mode
.timer ON|OFF Turn the CPU timer measurement on or off

sqlite>

创建表

sqlite> create table books(id integer primary key, title nvarchar(50),pubdate datetime); 
sqlite> create table publishers(id integer primary key, name varchar(20)); 
sqlite> .tables //查看数据库中存在的表
books publishers
sqlite> .schema books //显示表结构
CREATE TABLE books(id integer primary key, title nvarchar(50),pubdate datetime);
sqlite> 
sqlite> alter table books add column publisher text not null default '' collate nocase; //增加表字段
sqlite> .schema books //显示表结构
CREATE TABLE books(id integer primary key, title nvarchar(50),pubdate datetime, publisher text not null default '' collate nocase);
sqlite> 
sqlite> alter table publishers rename to authors; //修改表名
sqlite> .tables //查看数据库中存在的表
authors books 

sqlite>

修改字段名称

//修改字段名称和数据类型(删除字段也类似),更简单的做法:如果没有重要数据,可以直接删除表重新创建

sqlite> alter table authors rename to authorsOld;
sqlite> create table authors (id integer primary key, author nvarchar(20));
sqlite> insert into authors select id, name from authorsOld;
sqlite> drop table authorsOld;
sqlite> .schema authors
CREATE TABLE authors (id integer primary key, author nvarchar(20));

//还可以把数据导出成insert语句,然后删除表,重新创建表,执行所有insert语句添加数据

显示数据库所有表名称

网友评论
<