一、sqlite-3.3.8编译安装 请阅读在安装包里的 INSTALL或者README 文件。SQLite已经内置了,你不需要安装任何附加的软件(additional software)。 Windows users可以下载SQLite扩展DLL。 这里简单介绍一下: 假设你得到的是源代码sqlite-3.3.8.tar.gz,这里将告诉你怎么编译它。 解压sqlite-3.3.8.tar.gz 到 /home目录下 For example: tar zxvf sqlite-3.3.8.tar.gz -C /home cd /home mkdir sqlite-3.3.8-ix86 cd /home/sqlite-3.3.8-ix86/ ../sqlite-3.3.8/configure --prefix=/home/sqlite-3.3.8-ix86 编译并安装,然后生成帮助文档 make && make install && make doc
如果出现下列错误 ../sqlite-3.3.8/src/tclsqlite.c: In function `DbUpdateHandler': ../sqlite-3.3.8/src/tclsqlite.c:333: warning: passing arg 3 of `Tcl_ListObjAppendElement' makes pointer from integer without a cast ../sqlite-3.3.8/src/tclsqlite.c: In function `tclSqlFunc': ../sqlite-3.3.8/src/tclsqlite.c:419: warning: passing arg 1 of `Tcl_NewByteArrayObj' discards qualifiers from pointer target type ...
这个都是tcl相关的错误,可以先安装ActiveTcl以解决.假如你不需要tcl支持,那么这个错误可以这样避免: cd /home/sqlite-3.3.8-ix86/ ../sqlite-3.3.8/configure --disable-tcl --prefix=/home/sqlite-3.3.8-ix86 编译并安装,然后生成帮助文档 make && make install && make doc 不出意外,将不会出现错误,那么 Libraries have been installed in: /home/sqlite-3.3.8-ix86/lib
库文件已经生成在 /home/sqlite-3.3.8-ix86/lib 目录下 可执行文件sqlite3已经生成在 /home/sqlite-3.3.8-ix86/bin 目录下 下面创建一个新的数据库文件名叫"zieckey.db" (当然你可以使用不同的名字) 来测试数据库. 直接输入: /home/sqlite-3.3.8-ix86/bin/sqlite3 test.db 如果出现下面字样表明编译安装已经成功了. SQLite version 3.3.8 Enter ".help" for instructions sqlite>
sqlite> .help .databases List names and files of attached databases .dump ?TABLE? ... Dump the database in an SQL text format .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. .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 on TABLE .mode MODE ?TABLE? Set output mode where MODE is on 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 .schema ?TABLE? Show the CREATE statements .separator STRING Change separator used by output mode and .import .show Show the current values for various settings .tables ?PATTERN? List names of tables matching a LIKE pattern .timeout MS Try opening locked tables for MS milliseconds .width NUM NUM ... Set column widths for "column" mode sqlite>
[root@localhost temp]# vi opendbsqlite.c 按下 i 键切换到输入模式,输入下列代码:
// name: opendbsqlite.c // This prog is used to test C/C++ API for sqlite3.It is very simple,ha! // Author : zieckey All rights reserved. // date : 2006/11/13
#include #include
int main( void ) { sqlite3 *db=NULL; char *zErrMsg = 0; int rc;
//打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 rc = sqlite3_open("zieckey.db", &db); if( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 0; } else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
sqlite3_close(db); //关闭数据库 return 0; }
退出,保存。(代码输入完成后,按下 Esc 键,然后输入: :wq ,回车就好拉)
好拉,现在编译:[root@localhost temp]# gcc opendbsqlite.c -o db.out 或者遇到这样的问题: [root@localhost temp]# gcc opendbsqlite.c -o db.out opendbsqlite.c:11:21: sqlite3.h: 没有那个文件或目录 opendbsqlite.c: In function `main': opendbsqlite.c:19: `sqlite3' undeclared (first use in this function) opendbsqlite.c:19: (Each undeclared identifier is reported only once opendbsqlite.c:19: for each function it appears in.) opendbsqlite.c:19: `db' undeclared (first use in this function)
这是由于没有找到头文件的原因。
也许会碰到类似这样的问题: [root@localhost temp]# gcc opendbsqlite.c -o db.out /tmp/ccTkItnN.o(.text+0x2b): In function `main': : undefined reference to `sqlite3_open' /tmp/ccTkItnN.o(.text+0x45): In function `main': : undefined reference to `sqlite3_errmsg' /tmp/ccTkItnN.o(.text+0x67): In function `main': : undefined reference to `sqlite3_close' /tmp/ccTkItnN.o(.text+0x8f): In function `main': : undefined reference to `sqlite3_close' collect2: ld returned 1 exit status
这样编译应该就可以了 ,运行: [root@localhost temp]# ./db.out ./db.out: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file or directory
// name: insert.c // This prog is used to test C/C++ API for sqlite3 .It is very simple,ha ! // Author : zieckey All rights reserved. // date : 2006/11/18
int main( void ) { sqlite3 *db=NULL; char *zErrMsg = 0; int rc;
rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 if( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 0; } else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中 char *sql = " CREATE TABLE SensorData( ID INTEGER PRIMARY KEY, SensorID INTEGER, SiteNum INTEGER, Time VARCHAR(12), SensorParameter REAL );" ; sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sqlite3_exec的函数原型说明如下: int sqlite3_exec( sqlite3*, /* An open database */ const char *sql, /* SQL to be executed */ sqlite_callback, /* Callback function */ void *, /* 1st argument to callback function */ char **errmsg /* Error msg written here */ );
编译: [root@localhost temp]# gcc insert.c -lsqlite3 -L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include insert.c:28:21: warning: multi-line string literals are deprecated [root@localhost temp]# 执行 [root@localhost temp]# ./a.out ./a.out: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file or directory [root@localhost temp]# 同样的情况,如上文处理方法: [root@localhost temp]# export LD_LIBRARY_PATH=/usr/local/sqlite3/lib:$LD_LIBRARY_PATH [root@localhost temp]# ./a.out You have opened a sqlite3 database named zieckey.db successfully! Congratulations! Have fun ! ^-^ (null) (null) (null) [root@localhost temp]# 运行成功了,好了,现在我们来看看是否插入了数据 [root@localhost temp]# /usr/local/sqlite3/bin/sqlite3 zieckey.db SQLite version 3.3.8 Enter ".help" for instructions sqlite> select * from SensorData; 1|1|1|200605011206|18.9 2|1|1|200605011306|16.4 sqlite>
// name: query.c // This prog is used to test C/C++ API for sqlite3 .It is very simple,ha ! // Author : zieckey All rights reserved. // date : 2006/11/18
rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 if( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 0; } else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中 char *sql = " CREATE TABLE SensorData( ID INTEGER PRIMARY KEY, SensorID INTEGER, SiteNum INTEGER, Time VARCHAR(12), SensorParameter REAL );" ; sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
[root@localhost temp]# export LD_LIBRARY_PATH=/usr/local/sqlite3/lib:$LD_LIBRARY_PATH [root@localhost temp]# gcc query.c -lsqlite3 -L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include query.c:29:21: warning: multi-line string literals are deprecated [root@localhost temp]# ./a.out You have opened a sqlite3 database named zieckey.db successfully! Congratulations! Have fun ! ^-^ zErrMsg = (null) row=2 column=5
The result of querying is : azResult[0] = ID azResult[1] = SensorID azResult[2] = SiteNum azResult[3] = Time azResult[4] = SensorParameter azResult[5] = 1 azResult[6] = 1 azResult[7] = 1 azResult[8] = 200605011206 azResult[9] = 18.9 azResult[10] = 2 azResult[11] = 1 azResult[12] = 1 azResult[13] = 200605011306 azResult[14] = 16.4 zErrMsg = (null)
// name: delete.c // This prog is used to test C/C++ API for sqlite3 .It is very simple,ha ! // Author : zieckey All rights reserved. // date : 2006/11/18
rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件 if( rc ) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 0; } else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中 char *sql = " CREATE TABLE SensorData( ID INTEGER PRIMARY KEY, SensorID INTEGER, SiteNum INTEGER, Time VARCHAR(12), SensorParameter REAL );" ; sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
[root@localhost temp]# export LD_LIBRARY_PATH=/usr/local/sqlite3/lib:$LD_LIBRARY_PATH [root@localhost temp]# gcc delete.c -lsqlite3 -L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include delete.c:29:21: warning: multi-line string literals are deprecated [root@localhost temp]# ./a.out You have opened a sqlite3 database named zieckey.db successfully! Congratulations! Have fun ! ^-^ zErrMsg = (null) row=3 column=5
用原型函数(prototype)可以定义一些很方便的自定义函数,实现各种自定义功能。本次主要是实现了Array的去重、获取最大值和最小值。
实现代码如下:
<script type="text/javascript">
Array.prototype.unique = function() {
var a = {};
var le
ORACLE
下面这个效率很低
SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM IPAY_RCD_FS_RETURN order by id desc) A ) WHERE RN <20;
下面这个效率很高
SELECT A.*, ROWNUM RN FROM (SELECT * FROM IPAY_RCD_
Reverse a singly linked list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
p