QT操作SQLite
1、安装SQLite on Windows1)进入 SQL 下载页面:http://www.sqlite.org/download.html2)下载 Windows 下的预编译二进制文件包:sqlite-shell-win32-x86-<build#>.zipsqlite-dll-win32-x86-<build#>.zip注意: <build#> 是 sqlite 的编译版本号将 zip 文件解压到你的磁盘,并将解压后的目录添加到系统的 PATH 变量中,以方便在命令行中执行 sqlite 命令。可选: 如果你计划发布基于 sqlite 数据库的应用程序,你还需要下载源码以便编译和利用其 APIsqlite-amalgamation-<build#>.zipSQLite on Linux在 多个 Linux 发行版提供了方便的命令来获取 SQLite:/*ForDebianorUbuntu/* $sudoapt-getinstallsqlite3sqlite3-dev /*ForRedHat,CentOS,orFedora/* $yuminstallSQLite3sqlite3-devSQLite on Mac OS X如果你正在使用 Mac OS 雪豹或者更新版本的系统,那么系统上已经装有 SQLite 了。
2、创建首个 SQLite 数据库现在你已经安装了 SQLite 数据库,接下来我们创建首个墙绅褡孛数据库。在道药苍嗓命令行窗口中输入如下命令来创建一个名为 test.db 的数据库。sqlite3test.db创建表:sqlite>createtablemytable(idintegerprimarykey,valuetext); 2columnswerecreated.该表包含一个名为 id 的主键字段和一个名为 value 的文本字段。注意: 最少必须为新建的数据库创建一个表或者视图,这么才能将数据库保存到磁盘中,否则数据库不会被创建。接下来往表里中写入一些数据:sqlite>insertintomytable(id,value)values(1,'Micheal'); sqlite>insertintomytable(id,value)values(2,'Jenny'); sqlite>insertintomytable(value)values('Francis'); sqlite>insertintomytable(value)values('Kerk');查询数据:sqlite>select*fromtest; 1|Micheal 2|Jenny 3|Francis 4|Kerk设置格式化查询结果:sqlite>.modecolumn; sqlite>.headeron; sqlite>select*fromtest; idvalue ------------------------ 1Micheal 2Jenny 3Francis 4Kerk.mode column 将设置为列显示模式,.header 将显示列名。修改表结构,增加列:sqlite>altertablemytableaddcolumnemailtextnotnull''collatenocase;;创建视图:sqlite>createviewnameviewasselect*frommytable;创建索引:sqlite>createindextest_idxonmytable(value);
3、一些有用的 SQLite 命令显示表结构:sqlite>.schema[table]获取所有表和视图:sqlite>.tables获取指定表的索引列表:sqlite>.indices[table]导出数据库到 SQL 文件:sqlite>.output[filename] sqlite>.dump sqlite>.outputstdout从 SQL 文件导入数据库:sqlite>.read[filename]格式化输出数据到 CSV 格式:sqlite>.output[filename.csv] sqlite>.separator, sqlite>select*fromtest; sqlite>.outputstdout从 CSV 文件导入数据到表中:sqlite>createtablenewtable(idintegerprimarykey,valuetext); sqlite>.import[filename.csv]newtable备份数据库:/*usage:sqlite3[database].dump>[filename]*/ sqlite3mytable.db.dump>backup.sql恢复数据库:/*usage:sqlite3[database]<[filename]*/ sqlite3mytable.db<backup.sql