2023年6月21日发(作者:)
SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情
SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下 - 只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。如果您正在寻找一个嵌入式数据库项目或解决方案,SQLite是绝对值得考虑。
1. 安装SQLite on Windows
进入 SQL 下载页面:/
下载 Windows 下的预编译二进制文件包:
sqlite-shell-win32-x86-
sqlite-dll-win32-x86-
注意:
将 zip 文件解压到你的磁盘,并将解压后的目录添加到系统的 PATH 变量中,以方便在命令行中执行 sqlite 命令。
可选: 如果你计划发布基于 sqlite 数据库的应用程序,你还需要下载源码以便编译和利用其 API
sqlite-amalgamation-
SQLite on Linux在 多个 Linux 发行版提供了方便的命令来获取 SQLite: /* For Debian or Ubuntu /*
$ sudo apt-get install sqlite3 sqlite3-dev
/* For RedHat, CentOS, or Fedora/*
$ yum install SQLite3 sqlite3-dev
SQLite on Mac OS X如果你正在使用 Mac OS 雪豹或者更新版本的系统,那么系统上已经装有 SQLite 了。
2. 创建首个 SQLite 数据库
现在你已经安装了 SQLite 数据库,接下来我们创建首个数据库。在命令行窗口中输入如下命令来创建一个名为 的数据库。
sqlite3
创建表:
sqlite> create table mytable(id integer primary key, value text);
2 columns were created.
该表包含一个名为 id 的主键字段和一个名为 value 的文本字段。注意: 最少必须为新建的数据库创建一个表或者视图,这么才能将数据库保存到磁盘中,否则数据库不会被创建。接下来往表里中写入一些数据:
sqlite> insert into mytable(id, value) values(1, 'Micheal');
sqlite> insert into mytable(id, value) values(2, 'Jenny'); sqlite> insert into mytable(value) values('Francis');
sqlite> insert into mytable(value) values('Kerk');
查询数据:
sqlite> select * from test;
1|Micheal
2|Jenny
3|Francis
4|Kerk
设置格式化查询结果:
sqlite> .mode column;
sqlite> .header on;
sqlite> select * from test;
id value
----------- -------------
1 Micheal
2 Jenny
3 Francis
4 Kerk
.mode column 将设置为列显示模式,.header 将显示列名。修改表结构,增加列: sqlite> alter table mytable add column email text not null '' collate nocase;;
创建视图:
sqlite> create view nameview as select * from mytable;
创建索引:
sqlite> create index test_idx on mytable(value);
3. 一些有用的 SQLite 命令
显示表结构:sqlite> .schema [table]
获取所有表和视图:sqlite > .tables
获取指定表的索引列表:sqlite > .indices [table ]
导出数据库到 SQL 文件:
sqlite > .output [filename ]
sqlite > .dump
sqlite > .output stdout
从 SQL 文件导入数据库:sqlite > .read [filename ]
格式化输出数据到 CSV 格式:
sqlite >.output [ ]
sqlite >.separator , sqlite > select * from test;
sqlite >.output stdout
从 CSV 文件导入数据到表中:
sqlite >create table newtable ( id integer primary key, value text );
sqlite >.import [ ] newtable
备份数据库:
/* usage: sqlite3 [database] .dump > [filename] */
sqlite3 .dump >
恢复数据库:
/* usage: sqlite3 [database ] < [filename ] */
sqlite3 <
1 建立数据库
C:sqlite>
后面跟数据库文件名
2 创建数据表
sqlite> create table users(userid varchar(20) PRIMARY KEY,
...> age int,
...> birthday datetime);
3 添加记录
insert into users values('wang',20,'1989-5-4');
insert into users values('li',22,'1987-11-16');
4 查询记录
select * from users order by birthday;
5 删除记录
delete from users where userid='wang';
6 退出sqlite
sqlite> .exit
SQLite数据库的数据结构是存贮在 "sqlite_master" 表中
具体命令可以输入 .help查看或参考帮助文档
sqlite 表结构和数据的导出
全部导出
sqlite3
>.output
>.dump
导出表结构
.output
.schema
全部导入
sqlite3
>.read
平时使用官方提供的工具来操作 sqlite的数据库
进入管理:
d: //假设数据是 d:
>.databases //显示所有数据库 和 mysql的 show databases;
>.tables //显示当前数据库的表格 和 mysql 的show tables;
>.schema tablename; //显示表格结构 和mysql的 SHOW Create TABLE tbl_name >.output c: //导出当前数据库的 sql语句 和mysql的 mysqldump
>.dump
>.import c: //导入 //mysql 用source
===================
导入
命令: .import
sqlite> .import 文件名 表名
注1: 不要忘了开头的点
注2: 这条语句不能用分号结束. 非SQL不需要分号结束.
注3: 需要查看默认的分隔符separator. 必须一致. 如果不一致可能导致sqlite字段分割错误.
查看分隔符使用命令 .show , 如果不一致可直接修改, 比如:
sqlite>.separator ","
将分隔符转为逗号.
举例1:
将文件中的数据导入表 tab_xx. (中字段以逗号分割)
sqlite> .separator ","
sqlite> .import tab_xx
sqlite>
导入结束.
导出
实现方式: 将输出重定向至文件.
命令: .output
sqlite> .output
然后输入sql语句, 查询出要导的数据. 查询后,数据不会显示在屏幕上,而直接写入文件.
结束后,输入
sqlite> .output stdout
将输出重定向至屏幕.
举例2:
将 tab_xx 中的数据导出到文件
sqlite> .output
sqlite> select * from tab_xx;
sqlite> .output stdout
导出完毕.
(1)创建数据库
在命令行中切换到所在的文件夹
在命令中键入sqlite3 ;即可创建了一个名为的数据库
由于此时的数据库中没有任何表及数据存在,这时候是看不到的,必须往里面插入一张表即可看到数据库
(2)创建表
create table Test(Id Integer primary key, value text); 此时即可完成表的创建,当把主键设为Integer时,则该主键为自动增长,插入数据时,可直接使用如下语句:
insert into Test values(null,'Acuzio');
(3)获取最后一次插入的主键: select last_insert_rowid();
(4)sqlite>.mode col
sqlite>.headers on
在数据库查询的时候,显示行数和头!
(5)在DOS中,键入Ctrl+C,退出数据库,Unix中,使用Ctrl+D
(6)SQLite Master Table Schema
-----------------------------------------------------------------
Name Description
-----------------------------------------------------------------
type The object’s type (table, index, view, trigger)
name The object’s name
tbl_name The table the object is associated with
rootpage The object’s root page index in the database (where it begins)
sql The object’s SQL definition (DDL)
eg.
sqlite> .mode col
sqlite> .headers on
sqlite> select type, name, tbl_name, sql from sqlite_master order by type;
这样就能看到所有数据库中的信息,表、索引、视图等等
(7)导出数据
.output [filename],导出到文件中,如果该文件不存在,则自动创建
.dump 导出数据命令
.output stdout 返回输出到屏幕(进行其他操作)
eg.
sqlite>.output
sqlite>.dump
sqlite>.output stdout
这样就可以把数据导入到中
(8)导入数据
导入数据使用.read命令
eg.
如导入(7)中的数据
sqlite>.read (9)备份数据库
在切换到Sqlite文件夹
sqlite3 .dump >
如果在数据库中
sqlite> .output
sqlite> .dump
sqlite> .exit
(10)导入数据库
在切换到Sqlite文件夹
sqlite3 <
(11)备份二进制格式数据库,vacuum:释放掉已经被删除的空间(数据和表等被删除,不会被清空空间)
sqlite3 VACUUM
cp
(12)获取数据库信息
如果想获得物理数据库结构的信息,可以去SQLite网站上下载SQLite Analyzer工具
使用: sqlite3_analyzer
(13)其他的SQLite工具
SQLite Database Browser ()
SQLite Control Center (/)
SQLiteManager (/)
(13)SQLite 与其他数据库不同,它是以(;)来执行语句,而不是(go).
(14)SQLite注释(--)或(/* */)
eg.
-- This is a comment on one line
/* This is a comment spanning
two lines */
(15)创建表结构
CREATE [TEMP|TEMPORARY] TABLE table_name (column_definitions [, constraints]);
关键字TEMP、TEMPORARY表示创建的是临时表
(16)在SQLite中有5种基本类型:
Integer/Real/Text/Blob/Null
(17)确保唯一性可以用关键字UNIQUE
eg.
CREATE TABLE contacts ( id INTEGER PRIMARY KEY,
name TEXT NOT NULL COLLATE NOCASE,
phone TEXT NOT NULL DEFAULT 'UNKNOWN',
UNIQUE (name,phone) );
(18)修改表
ALTER TABLE table { RENAME TO name | ADD COLUMN column_def }
eg.
sqlite> ALTER TABLE contacts
ADD COLUMN email TEXT NOT NULL DEFAULT '' COLLATE NOCASE;
sqlite> .schema contacts
CREATE TABLE contacts ( id INTEGER PRIMARY KEY,
name TEXT NOT NULL COLLATE NOCASE,
phone TEXT NOT NULL DEFAULT 'UNKNOWN',
email TEXT NOT NULL DEFAULT '' COLLATE NOCASE,
UNIQUE (name,phone) );
(19)查询
SELECT DISTINCT heading FROM tables WHERE predicate
GROUP BY columns HAVING predicate
ORDER BY columns LIMIT count,offset;
(20)Limit和Offset关键字
Limit 指返回记录的最大行数
Offset 指跳过 #include "dbpoi.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//dbPOI w;
//QCoreApplication a(argc, argv);
// QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
QVector
QVector
QSet
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
abaseName("NEW_AR_"); // 数据库名与路径, 此时是放在同目录下
// abaseName(""); // 数据库名与路径, 此时是放在同目录下
bool ok = (); // 连接数据库, 然后就可以使用了.
if(ok){
QSqlQuery query;
if (("select * from LinkNode_Table_bark ")) //尝试列出 表的所有记录
{ //本次查询成功
int numRows = 0; //询问数据库驱动,是否驱动含有某种特性
if (()->hasFeature(QSqlDriver::QuerySize))
{
numRows = (); //如果支持结果影响的行数,那么直接记录下来
}
else {
(); //否则定位到结果最后,qt 文档说,这个方法非常慢
numRows = () + 1;
(-1);
}
}
//尝试列出 表的所有记录
while (()) {
int num =0;
/*QString FNode_ID = (4).toString();
QString TNode_ID = (5).toString();*/
int FNode_ID = (3).toInt();
int TNode_ID = (4).toInt();
(FNode_ID);
(TNode_ID);
}
}
else{
//qDebug() << "cannot open database.";
printf( "cannot open database.");
}
QFile file( "" );
if ( (QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream( &file );
for ( int index =0; index < (); index ++ ){
stream << (index);
((index));
((index));
stream<<"t"<< (index) < } (); } // qSort((),()); //(tempSort); QFile filetTempSort( "" ); if ( (QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream( &filetTempSort ); } } } foreach (const int &value, tempSort) { stream << value< (); QFile fileTp( "" ); if ( (QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream( &fileTp); foreach (const int &value, tempSort) { stream << value; } for ( int index =0; index < (); index ++ ){ if ((index) == value) stream << "t"<<(index); } for ( int index =0; index < (); index ++ ){ if ((index) == value) stream << "t"<<(index); } stream << endl; } (); //(); //return (); return 0; 多少行数据 2023年6月21日发(作者:) SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情 SQLite 是一个开源的嵌入式关系数据库,实现自包容、零配置、支持事务的SQL数据库引擎。 其特点是高度便携、使用方便、结构紧凑、高效、可靠。 与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下 - 只要确保SQLite的二进制文件存在即可开始创建、连接和使用数据库。如果您正在寻找一个嵌入式数据库项目或解决方案,SQLite是绝对值得考虑。 1. 安装SQLite on Windows 进入 SQL 下载页面:/ 下载 Windows 下的预编译二进制文件包: sqlite-shell-win32-x86- sqlite-dll-win32-x86- 注意: 将 zip 文件解压到你的磁盘,并将解压后的目录添加到系统的 PATH 变量中,以方便在命令行中执行 sqlite 命令。 可选: 如果你计划发布基于 sqlite 数据库的应用程序,你还需要下载源码以便编译和利用其 API sqlite-amalgamation- SQLite on Linux在 多个 Linux 发行版提供了方便的命令来获取 SQLite: /* For Debian or Ubuntu /* $ sudo apt-get install sqlite3 sqlite3-dev /* For RedHat, CentOS, or Fedora/* $ yum install SQLite3 sqlite3-dev SQLite on Mac OS X如果你正在使用 Mac OS 雪豹或者更新版本的系统,那么系统上已经装有 SQLite 了。 2. 创建首个 SQLite 数据库 现在你已经安装了 SQLite 数据库,接下来我们创建首个数据库。在命令行窗口中输入如下命令来创建一个名为 的数据库。 sqlite3 创建表: sqlite> create table mytable(id integer primary key, value text); 2 columns were created. 该表包含一个名为 id 的主键字段和一个名为 value 的文本字段。注意: 最少必须为新建的数据库创建一个表或者视图,这么才能将数据库保存到磁盘中,否则数据库不会被创建。接下来往表里中写入一些数据: sqlite> insert into mytable(id, value) values(1, 'Micheal'); sqlite> insert into mytable(id, value) values(2, 'Jenny'); sqlite> insert into mytable(value) values('Francis'); sqlite> insert into mytable(value) values('Kerk'); 查询数据: sqlite> select * from test; 1|Micheal 2|Jenny 3|Francis 4|Kerk 设置格式化查询结果: sqlite> .mode column; sqlite> .header on; sqlite> select * from test; id value ----------- ------------- 1 Micheal 2 Jenny 3 Francis 4 Kerk .mode column 将设置为列显示模式,.header 将显示列名。修改表结构,增加列: sqlite> alter table mytable add column email text not null '' collate nocase;; 创建视图: sqlite> create view nameview as select * from mytable; 创建索引: sqlite> create index test_idx on mytable(value); 3. 一些有用的 SQLite 命令 显示表结构:sqlite> .schema [table] 获取所有表和视图:sqlite > .tables 获取指定表的索引列表:sqlite > .indices [table ] 导出数据库到 SQL 文件: sqlite > .output [filename ] sqlite > .dump sqlite > .output stdout 从 SQL 文件导入数据库:sqlite > .read [filename ] 格式化输出数据到 CSV 格式: sqlite >.output [ ] sqlite >.separator , sqlite > select * from test; sqlite >.output stdout 从 CSV 文件导入数据到表中: sqlite >create table newtable ( id integer primary key, value text ); sqlite >.import [ ] newtable 备份数据库: /* usage: sqlite3 [database] .dump > [filename] */ sqlite3 .dump > 恢复数据库: /* usage: sqlite3 [database ] < [filename ] */ sqlite3 < 1 建立数据库 C:sqlite> 后面跟数据库文件名 2 创建数据表 sqlite> create table users(userid varchar(20) PRIMARY KEY, ...> age int, ...> birthday datetime); 3 添加记录 insert into users values('wang',20,'1989-5-4'); insert into users values('li',22,'1987-11-16'); 4 查询记录 select * from users order by birthday; 5 删除记录 delete from users where userid='wang'; 6 退出sqlite sqlite> .exit SQLite数据库的数据结构是存贮在 "sqlite_master" 表中 具体命令可以输入 .help查看或参考帮助文档 sqlite 表结构和数据的导出 全部导出 sqlite3 >.output >.dump 导出表结构 .output .schema 全部导入 sqlite3 >.read 平时使用官方提供的工具来操作 sqlite的数据库 进入管理: d: //假设数据是 d: >.databases //显示所有数据库 和 mysql的 show databases; >.tables //显示当前数据库的表格 和 mysql 的show tables; >.schema tablename; //显示表格结构 和mysql的 SHOW Create TABLE tbl_name >.output c: //导出当前数据库的 sql语句 和mysql的 mysqldump >.dump >.import c: //导入 //mysql 用source =================== 导入 命令: .import sqlite> .import 文件名 表名 注1: 不要忘了开头的点 注2: 这条语句不能用分号结束. 非SQL不需要分号结束. 注3: 需要查看默认的分隔符separator. 必须一致. 如果不一致可能导致sqlite字段分割错误. 查看分隔符使用命令 .show , 如果不一致可直接修改, 比如: sqlite>.separator "," 将分隔符转为逗号. 举例1: 将文件中的数据导入表 tab_xx. (中字段以逗号分割) sqlite> .separator "," sqlite> .import tab_xx sqlite> 导入结束. 导出 实现方式: 将输出重定向至文件. 命令: .output sqlite> .output 然后输入sql语句, 查询出要导的数据. 查询后,数据不会显示在屏幕上,而直接写入文件. 结束后,输入 sqlite> .output stdout 将输出重定向至屏幕. 举例2: 将 tab_xx 中的数据导出到文件 sqlite> .output sqlite> select * from tab_xx; sqlite> .output stdout 导出完毕. (1)创建数据库 在命令行中切换到所在的文件夹 在命令中键入sqlite3 ;即可创建了一个名为的数据库 由于此时的数据库中没有任何表及数据存在,这时候是看不到的,必须往里面插入一张表即可看到数据库 (2)创建表 create table Test(Id Integer primary key, value text); 此时即可完成表的创建,当把主键设为Integer时,则该主键为自动增长,插入数据时,可直接使用如下语句: insert into Test values(null,'Acuzio'); (3)获取最后一次插入的主键: select last_insert_rowid(); (4)sqlite>.mode col sqlite>.headers on 在数据库查询的时候,显示行数和头! (5)在DOS中,键入Ctrl+C,退出数据库,Unix中,使用Ctrl+D (6)SQLite Master Table Schema ----------------------------------------------------------------- Name Description ----------------------------------------------------------------- type The object’s type (table, index, view, trigger) name The object’s name tbl_name The table the object is associated with rootpage The object’s root page index in the database (where it begins) sql The object’s SQL definition (DDL) eg. sqlite> .mode col sqlite> .headers on sqlite> select type, name, tbl_name, sql from sqlite_master order by type; 这样就能看到所有数据库中的信息,表、索引、视图等等 (7)导出数据 .output [filename],导出到文件中,如果该文件不存在,则自动创建 .dump 导出数据命令 .output stdout 返回输出到屏幕(进行其他操作) eg. sqlite>.output sqlite>.dump sqlite>.output stdout 这样就可以把数据导入到中 (8)导入数据 导入数据使用.read命令 eg. 如导入(7)中的数据 sqlite>.read (9)备份数据库 在切换到Sqlite文件夹 sqlite3 .dump > 如果在数据库中 sqlite> .output sqlite> .dump sqlite> .exit (10)导入数据库 在切换到Sqlite文件夹 sqlite3 < (11)备份二进制格式数据库,vacuum:释放掉已经被删除的空间(数据和表等被删除,不会被清空空间) sqlite3 VACUUM cp (12)获取数据库信息 如果想获得物理数据库结构的信息,可以去SQLite网站上下载SQLite Analyzer工具 使用: sqlite3_analyzer (13)其他的SQLite工具 SQLite Database Browser () SQLite Control Center (/) SQLiteManager (/) (13)SQLite 与其他数据库不同,它是以(;)来执行语句,而不是(go). (14)SQLite注释(--)或(/* */) eg. -- This is a comment on one line /* This is a comment spanning two lines */ (15)创建表结构 CREATE [TEMP|TEMPORARY] TABLE table_name (column_definitions [, constraints]); 关键字TEMP、TEMPORARY表示创建的是临时表 (16)在SQLite中有5种基本类型: Integer/Real/Text/Blob/Null (17)确保唯一性可以用关键字UNIQUE eg. CREATE TABLE contacts ( id INTEGER PRIMARY KEY, name TEXT NOT NULL COLLATE NOCASE, phone TEXT NOT NULL DEFAULT 'UNKNOWN', UNIQUE (name,phone) ); (18)修改表 ALTER TABLE table { RENAME TO name | ADD COLUMN column_def } eg. sqlite> ALTER TABLE contacts ADD COLUMN email TEXT NOT NULL DEFAULT '' COLLATE NOCASE; sqlite> .schema contacts CREATE TABLE contacts ( id INTEGER PRIMARY KEY, name TEXT NOT NULL COLLATE NOCASE, phone TEXT NOT NULL DEFAULT 'UNKNOWN', email TEXT NOT NULL DEFAULT '' COLLATE NOCASE, UNIQUE (name,phone) ); (19)查询 SELECT DISTINCT heading FROM tables WHERE predicate GROUP BY columns HAVING predicate ORDER BY columns LIMIT count,offset; (20)Limit和Offset关键字 Limit 指返回记录的最大行数 Offset 指跳过 #include "dbpoi.h" #include #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { QApplication a(argc, argv); //dbPOI w; //QCoreApplication a(argc, argv); // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8")); QVector QVector QSet QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); abaseName("NEW_AR_"); // 数据库名与路径, 此时是放在同目录下 // abaseName(""); // 数据库名与路径, 此时是放在同目录下 bool ok = (); // 连接数据库, 然后就可以使用了. if(ok){ QSqlQuery query; if (("select * from LinkNode_Table_bark ")) //尝试列出 表的所有记录 { //本次查询成功 int numRows = 0; //询问数据库驱动,是否驱动含有某种特性 if (()->hasFeature(QSqlDriver::QuerySize)) { numRows = (); //如果支持结果影响的行数,那么直接记录下来 } else { (); //否则定位到结果最后,qt 文档说,这个方法非常慢 numRows = () + 1; (-1); } } //尝试列出 表的所有记录 while (()) { int num =0; /*QString FNode_ID = (4).toString(); QString TNode_ID = (5).toString();*/ int FNode_ID = (3).toInt(); int TNode_ID = (4).toInt(); (FNode_ID); (TNode_ID); } } else{ //qDebug() << "cannot open database."; printf( "cannot open database."); } QFile file( "" ); if ( (QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream( &file ); for ( int index =0; index < (); index ++ ){ stream << (index); ((index)); ((index)); stream<<"t"<< (index) < } (); } // qSort((),()); //(tempSort); QFile filetTempSort( "" ); if ( (QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream( &filetTempSort ); } } } foreach (const int &value, tempSort) { stream << value< (); QFile fileTp( "" ); if ( (QIODevice::WriteOnly | QIODevice::Text)) { QTextStream stream( &fileTp); foreach (const int &value, tempSort) { stream << value; } for ( int index =0; index < (); index ++ ){ if ((index) == value) stream << "t"<<(index); } for ( int index =0; index < (); index ++ ){ if ((index) == value) stream << "t"<<(index); } stream << endl; } (); //(); //return (); return 0; 多少行数据
发布评论