搜尋
首頁資料庫mysql教程SQLite数据库操作实践

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

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语句添加数据

显示数据库所有表名称

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
MySQL如何處理數據複製?MySQL如何處理數據複製?Apr 28, 2025 am 12:25 AM

MySQL通過異步、半同步和組複製三種模式處理數據複製。 1)異步複製性能高但可能丟失數據。 2)半同步複製提高數據安全性但增加延遲。 3)組複製支持多主複製和故障轉移,適用於高可用性需求。

您如何使用解釋性語句分析查詢性能?您如何使用解釋性語句分析查詢性能?Apr 28, 2025 am 12:24 AM

EXPLAIN語句可用於分析和提升SQL查詢性能。 1.執行EXPLAIN語句查看查詢計劃。 2.分析輸出結果,關注訪問類型、索引使用情況和JOIN順序。 3.根據分析結果,創建或調整索引,優化JOIN操作,避免全表掃描,以提升查詢效率。

您如何備份並還原MySQL數據庫?您如何備份並還原MySQL數據庫?Apr 28, 2025 am 12:23 AM

使用mysqldump進行邏輯備份和MySQLEnterpriseBackup進行熱備份是備份MySQL數據庫的有效方法。 1.使用mysqldump備份數據庫:mysqldump-uroot-pmydatabase>mydatabase_backup.sql。 2.使用MySQLEnterpriseBackup進行熱備份:mysqlbackup--user=root--password=password--backup-dir=/path/to/backupbackup。恢復時,使用相應的命

MySQL中慢速查詢的常見原因是什麼?MySQL中慢速查詢的常見原因是什麼?Apr 28, 2025 am 12:18 AM

MySQL慢查詢的主要原因包括索引缺失或不當使用、查詢複雜度、數據量過大和硬件資源不足。優化建議包括:1.創建合適的索引;2.優化查詢語句;3.使用分錶分區技術;4.適當升級硬件。

MySQL中有什麼看法?MySQL中有什麼看法?Apr 28, 2025 am 12:04 AM

MySQL視圖是基於SQL查詢結果的虛擬表,不存儲數據。 1)視圖簡化複雜查詢,2)增強數據安全性,3)維護數據一致性。視圖是數據庫中的存儲查詢,可像表一樣使用,但數據動態生成。

MySQL和其他SQL方言之間的語法有什麼區別?MySQL和其他SQL方言之間的語法有什麼區別?Apr 27, 2025 am 12:26 AM

mysqldiffersfromothersqldialectsinsyntaxforlimit,自動啟動,弦樂範圍,子征服和表面上分析。 1)MySqluessLipslimit,whilesqlserverusestopopandoraclesrontersrontsrontsrontsronnum.2)

什麼是mysql分區?什麼是mysql分區?Apr 27, 2025 am 12:23 AM

MySQL分區能提升性能和簡化維護。 1)通過按特定標準(如日期範圍)將大表分成小塊,2)物理上將數據分成獨立文件,3)查詢時MySQL可專注於相關分區,4)查詢優化器可跳過不相關分區,5)選擇合適的分區策略並定期維護是關鍵。

您如何在MySQL中授予和撤銷特權?您如何在MySQL中授予和撤銷特權?Apr 27, 2025 am 12:21 AM

在MySQL中,如何授予和撤銷權限? 1.使用GRANT語句授予權限,如GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host';2.使用REVOKE語句撤銷權限,如REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host',確保及時溝通權限變更。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版