SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至2015年已经有15个年头,SQLite也迎来了一个版本 SQLite 3已经发布。个人非常喜欢!
编译时遇到的问题:
编译sqlite3数据库C语言程序时出现fatal error: sqlite3.h: No such file or directory,找不到头文件的问题。原来是系统没有安装函数库。
执行下面语句解决:
sudo apt-get install libsqlite3-dev
补充:
当用交叉编译器编译的时候,也会出现找不到sqlite3.h头文件的情况,解决方法是把sqlite3.h这个头文件放到交叉编译工具目录的 include目录下。
数据库导出乱码解决(转码函数):
//UTF-8转Unicode
std::wstring Utf82Unicode(const std::string& utf8string)
{
int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[0]);
}
//unicode 转为 ascii
string WideByte2Acsi(wstring& wstrcode)
{
int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (asciisize == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(asciisize);
int convresult =::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
if (convresult != asciisize)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[0]);
}
//utf-8 转 ascii
string UTF_82ASCII(string& strUtf8Code)
{
string strRet("");
//先把 utf8 转为 unicode
wstring wstr = Utf82Unicode(strUtf8Code);
//最后把 unicode 转为 ascii
strRet = WideByte2Acsi(wstr);
return strRet;
}
///////////////////////////////////////////////////////////////////////
//ascii 转 Unicode
wstring Acsi2WideByte(string& strascii)
{
int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
throw std::exception("Invalid UTF-8 sequence.");
}
if (widesize == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<wchar_t> resultstring(widesize);
int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);
if (convresult != widesize)
{
throw std::exception("La falla!");
}
return std::wstring(&resultstring[0]);
}
//Unicode 转 Utf8
std::string Unicode2Utf8(const std::wstring& widestring)
{
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
if (utf8size == 0)
{
throw std::exception("Error in conversion.");
}
std::vector<char> resultstring(utf8size);
int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);
if (convresult != utf8size)
{
throw std::exception("La falla!");
}
return std::string(&resultstring[0]);
}
//ascii 转 Utf8
string ASCII2UTF_8(string& strAsciiCode)
{
string strRet("");
//先把 ascii 转为 unicode
wstring wstr = Acsi2WideByte(strAsciiCode);
//最后把 unicode 转为 utf8
strRet = Unicode2Utf8(wstr);
return strRet;
}
数据库导入导出:
sqlite的数据导入 导出
数据导入的来源可以是其他应用程序的输出,也可以是指定的文本文件,这里采用指定的文本文件。
1. 首先,确定导入的数据源,这里是待导入的,按固定格式的文本文件。
2. 然后,依照导入的文件格式,确定想导入的目标数据表,这个数据表如果没有,可以依照待导入的文本文件格式,创建一个相对应的数据表。
3. 最后,执行.import命令,将文本文件中数据导入数据表中。
1. 数据源
在/home/ywx/yu/sqlite/下,创建一个名为data.txt的文本文件,并输入以下数据,数据之间采用逗号隔开
- id,name,age,address,hobby
- 1,tom,24,beijing,football
- 2,liu,27,heibei,fotball
- 3,jim,26,shandong,football
- 4,han,28,beijing,football
- 5,meng,25,beijing,tennis
2. 目标数据表
这里创建一张目标数据表,通过分析文本格式,这里需要3个字段,分别是id,name,age。但在数据类型选择时存在一个问题,id和age在文本文件中是按字符型存储的,而其实际在数据表中,最好要表示成整型,因此这里要涉及到一个字符型数据类型向整型数据类型转换的问题。
在创建表时,将id和age的类型定义为整型,进行强制转换,如果在数据导入时,发现转换失败,可以将id和age类型改为文本型。
- ywx@ywx:~/yu/sqlite$ sqlite3 test.db
- SQLite version 3.7.7.1 2011-06-28 17:39:05
- Enter ".help" for instructions
- Enter SQL statements terminated with a ";"
- sqlite> create table data_txt_table(id char(10),name char(10),age char(10),address varchar(15),hobby varchar (15));
- sqlite>
3. 导入命令
- sqlite> .separator ","
- sqlite> .import data.txt data_txt_table
- sqlite> select * from data_txt_table;
- id,name,age,address,hobby
- 1,tom,24,beijing,football
- 2,liu,27,heibei,fotball
- 3,jim,26,shandong,football
- 4,han,28,beijing,football
- 5,meng,25,beijing,tennis
- sqlite>
这里需要注意一点,在数据导入之前,先要根据数据的具体分的格式,设置数据导入的间隔符,例如在文本数据中采用的是‘,’来间隔数据,因此应先调用.seperator 设置‘,’ 为间隔符。
2. 查看命令
.schema 命令来查看指定的数据表的结构
- sqlite> .schema data_txt_table
- CREATE TABLE data_txt_table(id char(10),name char(10),age char(10),address varchar(15),hobby varchar (15));
- sqlite>
2. .tables 命令用来查看当前数据库的所有数据表
- sqlite> .tables
- data_txt_table
- sqlite>
3. databases 命令用来查看当前所有数据库
- sqlite> .databases
- seq name file
- --- --------------- ----------------------------------------------------------
- 0 main /home/ywx/yu/sqlite/test.db
- 1 temp
3. 数据导出
数据导出也是一个常用到的操作,可以将指定表中的数据导出成SQL脚本,供其他数据库使用,还可以将指定的数据表中的数据完整定位到标准输出,也可以将指定数据库中的数据完整的导入到另一个指定数据库等,
1. 导出成指定的SQL脚本
将sqlite中指定的数据表以SQL创建脚本的形式导出,具体命令
- ywx@ywx:~/yu/sqlite$ sqlite3 test.db
- SQLite version 3.7.7.1 2011-06-28 17:39:05
- Enter ".help" for instructions
- Enter SQL statements terminated with a ";"
- sqlite> .output data.sql
- sqlite> .dump
- sqlite>
- ywx@ywx:~/yu/sqlite$ ll
- 总计 16
- drwxr-xr-x 2 ywx ywx 4096 2011-08-13 23:15 ./
- drwxr-xr-x 7 ywx ywx 4096 2011-08-13 20:53 ../
- -rw-r--r-- 1 ywx ywx 602 2011-08-13 23:17 data.sql
- -rw-r--r-- 1 ywx ywx 2048 2011-08-13 22:44 test.db
2. 数据库导出
- data.sql test.db
- ywx@ywx:~/yu/sqlite$ sqlite3 test.db ".dump" | sqlite3 test2.db
- ywx@ywx:~/yu/sqlite$ ll
- 总计 20
- drwxr-xr-x 2 ywx ywx 4096 2011-08-13 23:20 ./
- drwxr-xr-x 7 ywx ywx 4096 2011-08-13 20:53 ../
- -rw-r--r-- 1 ywx ywx 602 2011-08-13 23:17 data.sql
- -rw-r--r-- 1 ywx ywx 2048 2011-08-13 23:20 test2.db
- -rw-r--r-- 1 ywx ywx 2048 2011-08-13 22:44 test.db
3. 其他格式,如:htm格式输出
- ywx@ywx:~/yu/sqlite$ sqlite3 -html test.db "select * from data_txt_table" > liu.htm
- ywx@ywx:~/yu/sqlite$ ls
- data.sql liu.htm test2.db test.db
- http://blog.chinaunix.net/uid-22666248-id-2182334.html