Google

星期五, 六月 29, 2007

SQLite研究——内置函数

Core Functions

The core functions shown below are available by default. Additional functions may be written in C and added to the database engine using the sqlite3_create_function() API.

abs(X)Return the absolute value of argument X.
coalesce(X,Y,...)Return a copy of the first non-NULL argument. If all arguments are NULL then NULL is returned. There must be at least 2 arguments.
glob(X,Y)This function is used to implement the "X GLOB Y" syntax of SQLite. The sqlite3_create_function() interface can be used to override this function and thereby change the operation of the GLOB operator.
ifnull(X,Y)Return a copy of the first non-NULL argument. If both arguments are NULL then NULL is returned. This behaves the same as coalesce() above.
last_insert_rowid()Return the ROWID of the last row insert from this connection to the database. This is the same value that would be returned from the sqlite_last_insert_rowid() API function.
length(X)Return the string length of X in characters. If SQLite is configured to support UTF-8, then the number of UTF-8 characters is returned, not the number of bytes.
like(X,Y [,Z])This function is used to implement the "X LIKE Y [ESCAPE Z]" syntax of SQL. If the optional ESCAPE clause is present, then the user-function is invoked with three arguments. Otherwise, it is invoked with two arguments only. The sqlite_create_function() interface can be used to override this function and thereby change the operation of the LIKE operator. When doing this, it may be important to override both the two and three argument versions of the like() function. Otherwise, different code may be called to implement the LIKE operator depending on whether or not an ESCAPE clause was specified.
load_extension(X)
load_extension(X,Y)
Load SQLite extensions out of the shared library file named X using the entry point Y. The result is a NULL. If Y is omitted then the default entry point of sqlite3_extension_init is used. This function raises an exception if the extension fails to load or initialize correctly.
lower(X)Return a copy of string X will all characters converted to lower case. The C library tolower() routine is used for the conversion, which means that this function might not work correctly on UTF-8 characters.
max(X,Y,...)Return the argument with the maximum value. Arguments may be strings in addition to numbers. The maximum value is determined by the usual sort order. Note that max() is a simple function when it has 2 or more arguments but converts to an aggregate function if given only a single argument.
min(X,Y,...)Return the argument with the minimum value. Arguments may be strings in addition to numbers. The minimum value is determined by the usual sort order. Note that min() is a simple function when it has 2 or more arguments but converts to an aggregate function if given only a single argument.
nullif(X,Y)Return the first argument if the arguments are different, otherwise return NULL.
quote(X)This routine returns a string which is the value of its argument suitable for inclusion into another SQL statement. Strings are surrounded by single-quotes with escapes on interior quotes as needed. BLOBs are encoded as hexadecimal literals. The current implementation of VACUUM uses this function. The function is also useful when writing triggers to implement undo/redo functionality.
random(*)Return a pseudo-random integer between -9223372036854775808 and +9223372036854775807.
round(X)
round(X,Y)
Round off the number X to Y digits to the right of the decimal point. If the Y argument is omitted, 0 is assumed.
soundex(X)Compute the soundex encoding of the string X. The string "?000" is returned if the argument is NULL. This function is omitted from SQLite by default. It is only available the -DSQLITE_SOUNDEX=1 compiler option is used when SQLite is built.
sqlite_version(*)Return the version string for the SQLite library that is running. Example: "2.8.0"
substr(X,Y,Z)Return a substring of input string X that begins with the Y-th character and which is Z characters long. The left-most character of X is number 1. If Y is negative the the first character of the substring is found by counting from the right rather than the left. If SQLite is configured to support UTF-8, then characters indices refer to actual UTF-8 characters, not bytes.
typeof(X)Return the type of the expression X. The only return values are "null", "integer", "real", "text", and "blob". SQLite's type handling is explained in Datatypes in SQLite Version 3.
upper(X)Return a copy of input string X converted to all upper-case letters. The implementation of this function uses the C library routine toupper() which means it may not work correctly on UTF-8 strings.

Aggregate Functions

The aggregate functions shown below are available by default. Additional aggregate functions written in C may be added using the sqlite3_create_function() API.

In any aggregate function that takes a single argument, that argument can be preceeded by the keyword DISTINCT. In such cases, duplicate elements are filtered before being passed into the aggregate function. For example, the function "count(distinct X)" will return the number of distinct values of column X instead of the total number of non-null values in column X.

avg(X)Return the average value of all non-NULL X within a group. String and BLOB values that do not look like numbers are interpreted as 0. The result of avg() is always a floating point value even if all inputs are integers.
count(X)
count(*)
The first form return a count of the number of times that X is not NULL in a group. The second form (with no argument) returns the total number of rows in the group.
max(X)Return the maximum value of all values in the group. The usual sort order is used to determine the maximum.
min(X)Return the minimum non-NULL value of all values in the group. The usual sort order is used to determine the minimum. NULL is only returned if all values in the group are NULL.
sum(X)
total(X)
Return the numeric sum of all non-NULL values in the group. If there are no non-NULL input rows then sum() returns NULL but total() returns 0.0. NULL is not normally a helpful result for the sum of no rows but the SQL standard requires it and most other SQL database engines implement sum() that way so SQLite does it in the same way in order to be compatible. The non-standard total() function is provided as a convenient way to work around this design problem in the SQL language.

The result of total() is always a floating point value. The result of sum() is an integer value if all non-NULL inputs are integers. If any input to sum() is neither an integer or a NULL then sum() returns a floating point value which might be an approximation to the true sum.

Sum() will throw an "integer overflow" exception if all inputs are integers or NULL and an integer overflow occurs at any point during the computation. Total() never throws an exception.

Date and Time Functions Overview

Five date and time functions are available, as follows:

  1. date( timestring, modifier, modifier, ...)
  2. time( timestring, modifier, modifier, ...)
  3. datetime( timestring, modifier, modifier, ...)
  4. julianday( timestring, modifier, modifier, ...)
  5. strftime( format, timestring, modifier, modifier, ...)

All five functions take a time string as an argument. This time string may be followed by zero or more modifiers. The strftime() function also takes a format string as its first argument.

The date() function returns the date in this format: YYYY-MM-DD. The time() function returns the time as HH:MM:SS. The datetime() function returns "YYYY-MM-DD HH:MM:SS". The julianday() function returns the number of days since noon in Greenwich on November 24, 4714 B.C. The julian day number is the preferred internal representation of dates. The strftime() routine returns the date formatted according to the format string specified as the first argument. The format string supports most, but not all, of the more common substitutions found in the strftime() function from the standard C library:

   %d  day of month
%f ** fractional seconds SS.SSS
%H hour 00-24
%j day of year 001-366
%J ** Julian day number
%m month 01-12
%M minute 00-59
%s seconds since 1970-01-01
%S seconds 00-59
%w day of week 0-6 sunday==0
%W week of year 00-53
%Y year 0000-9999
%% %

The %f and %J conversions are new. Notice that all of the other four functions could be expressed in terms of strftime().

   date(...)      ->  strftime("%Y-%m-%d", ...)
time(...) -> strftime("%H:%M:%S", ...)
datetime(...) -> strftime("%Y-%m-%d %H:%M:%S", ...)
julianday(...) -> strftime("%J", ...)

The only reasons for providing functions other than strftime() is for convenience and for efficiency.

Time Strings

A time string can be in any of the following formats:

  1. YYYY-MM-DD
  2. YYYY-MM-DD HH:MM
  3. YYYY-MM-DD HH:MM:SS
  4. YYYY-MM-DD HH:MM:SS.SSS
  5. YYYY-MM-DDTHH:MM
  6. YYYY-MM-DDTHH:MM:SS
  7. YYYY-MM-DDTHH:MM:SS.SSS
  8. HH:MM
  9. HH:MM:SS
  10. HH:MM:SS.SSS
  11. now
  12. DDDD.DDDD

In formats 5 through 7, the "T" is a literal character separating the date and the time, as required by the ISO-8601 standard. These formats are supported in SQLite 3.2.0 and later. Formats 8 through 10 that specify only a time assume a date of 2000-01-01. Format 11, the string 'now', is converted into the current date and time. Universal Coordinated Time (UTC) is used. Format 12 is the julian day number expressed as a floating point value.

Modifiers

The time string can be followed by zero or more modifiers that alter the date or alter the interpretation of the date. The available modifiers are as follows.

  1. NNN days
  2. NNN hours
  3. NNN minutes
  4. NNN.NNNN seconds
  5. NNN months (see #551 and [1163])
  6. NNN years (see #551 and [1163])
  7. start of month
  8. start of year
  9. start of week (withdrawn -- will not be implemented)
  10. start of day
  11. weekday N (see #551 and [1163])
  12. unixepoch
  13. localtime
  14. utc

The first six modifiers (1 through 6) simply add the specified amount of time to the date specified by the preceding timestring.

The "start of" modifiers (7 through 10) shift the date backwards to the beginning of the current month, year or day.

The "weekday" modifier advances the date forward to the next date where the weekday number is N. Sunday is 0, Monday is 1, and so forth.

The "unixepoch" modifier (12) only works if it immediately follows a timestring in the DDDDDDDDDD format. This modifier causes the DDDDDDDDDD to be interpreted not as a julian day number as it normally would be, but as the number of seconds since 1970. This modifier allows unix-based times to be converted to julian day numbers easily.

The "localtime" modifier (13) adjusts the previous time string so that it displays the correct local time. "utc" undoes this.

Examples

Compute the current date.

  SELECT date('now');

Compute the last day of the current month.

  SELECT date('now','start of month','+1 month','-1 day');

Compute the date and time given a unix timestamp 1092941466.

  SELECT datetime(1092941466, 'unixepoch');

Compute the date and time given a unix timestamp 1092941466, and compensate for your local timezone.

  SELECT datetime(1092941466, 'unixepoch', 'localtime');

Compute the current unix timestamp.

  SELECT strftime('%s','now');

Compute the number of days since the battle of Hastings.

  SELECT julianday('now') - julianday('1066-10-14','gregorian');

Compute the number of seconds between two dates:

  SELECT julianday('now')*86400 - julianday('2004-01-01 02:34:56')*86400;

Compute the date of the first Tuesday in October (January + 9) for the current year.

  SELECT date('now','start of year','+9 months','weekday 2');

Caveats And Bugs

The computation of local time depends heavily on the whim of local politicians and is thus difficult to get correct for all locales. In this implementation, the standard C library function localtime() is used to assist in the calculation of local time. Note that localtime() is not threadsafe, so use of the "localtime" modifier is not threadsafe. Also, the localtime() C function normally only works for years between 1970 and 2037. For dates outside this range, SQLite attempts to map the year into an equivalent year within this range, do the calculation, then map the year back.

Please surround uses of localtime() with sqliteOsEnterMutex() and sqliteOsLeaveMutex() so threads using SQLite are protected, at least! -- e It is so. --drh

[Consider instead, using localtime_r which is reentrant and may be used *without* expensive mutex locking. Although non-standard it's available on most Unixes --hauk] But it is not available on windows, as far as I am aware. --drh On windows localtime() is thread-safe if the MT C runtime is used. The MT runtime uses thread-local storage for the static variables, the kind functions use.--gr [What about using localtime_r, and on systems where it is unavailable defining it as sqliteOsEnterMutext() ; locatime() ; sqliteOsLeaveMutex() so that non-windows systems get the maximum advantage, with almost zero code impact?] The autoconfigury and patch for localtime_r is here: ¤http://www.sqlite.org/cvstrac/tktview?tn=1906 . I'm curious why this obvious fix is not applied. gmtime() also suffers from this same threadsafety problem.

Date computations do not give correct results for dates before Julian day number 0 (-4713-11-24 12:00:00).

All internal computations assume the Gregorian calendar system.


An anonymous user adds:
For my use I added new functions and functionalities to the date functions that come with the sqlite 3.3.0 (can be used in older versions as well with small effort).

In main lines they are as follows:

  1. NNN days
  2. NNN hours
  3. NNN minutes
  4. NNN.NNNN seconds
  5. NNN months (see #551 and [1163])
  6. NNN years (see #551 and [1163])
  7. start of month
  8. start of year
  9. start of week (!!! implemented)
  10. start of day
  11. weekday N (see #551 and [1163])
  12. unixepoch
  13. localtime
  14. utc
  15. julian (not implemented as of 2004-01-05)
  16. gregorian (not implemented as of 2004-01-05)
  17. start of minute
  18. start of hour
  19. end of minute
  20. end of hour
  21. end of day
  22. end of week
  23. end of month
  24. end of year
  25. group seconds by
  26. group minutes by
  27. group hours by
  28. group days by
  29. group weeks by
  30. group months by
  31. group years by

The "start of" modifiers (7 through 10 and 17 through 18) shift the date backwards to the beginning of the current minute, hour, week, month, year or day.

The "end of" modifiers (19 through 24) shift the date forwards to the end of the current minute, hour, week, month, year or day.

The "group * by" modifiers (25 through 31) round the date to the closest backward multiple supplied, with some limitations, to the current seconds (1 through 30), minutes (1 through 30), hours (1 through 12), days (1 through 15), weeks (1 through 26), months (1 through 6), years (1 through 100), these limitations are due to dont complicate the calculations when a multiple can span beyound the unit modified.

Ex:

SELECT datetime('2006-02-04 20:09:23','group hours by 3'); => '2006-02-04 18:00:00'

SELECT datetime('2006-02-05 20:09:23','group days by 3'); => '2006-02-04 00:00:00'

New functions "week_number(date)" returns the week number of the year on the supplied date parameter, "datetime2seconds(datetime)" return the number of seconds from the supplied datetime parameter.

The diff file ready to be applied to the file "date.c" in the src directory of sqlite 3.3.0 is at ¤http://dad-it.com:8080/date.c.diff, I hope it's considered valuable to be merged in the official distribution.


原贴:http://www.jimmydong.com/blog/post/1/113

标签: ,

星期四, 六月 21, 2007

Ubuntu安装PDO

pdo在ubuntu的apt里头似乎还找不到安装源,所以通过pecl来安装这个扩展,非常简单 —- 如果海底光纤能连通的话:

pecl install pdo

增加一行:

extension=pdo.so

到文件:

/etc/php/apache2/php.ini
/etc/php/cli/php.ini

接下来安装pdo_mysql碰到一些问题, 直接跑pecl install pecl_mysql会出现一些错误,搜索了一下发现是pecl本身的问题,下面是个比较简单的解决办法:

wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
tar xzvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2

注释掉configure里头判断是否已经安装pdo扩展的代码片段,继续跑:

phpize
./configure
make
make install

然后再次添加下面一行到前面提到的两个php.ini

extension=pdo_mysql.so

重启apache之后, php5 + pdo_mysql就在ubuntu上安装好了, documentroot是/var/www

后记
更简单的解决办法是运行:

PHP_PDO_SHARED=1 pecl install pdo_mysql

标签: , ,

Prado国际出名的PHP框架

PRADO是一个基于组件和事件驱动的PHP5开发框架。它以组件,事件,属性来代替传统web开发中的程序,URLs和查询参数。一个 PRADO组件是由一个说明文件(XML),一个HTML模板和一个PHP类组件。一个个PRADO组件组合在一起形成大型组件或完整的PRADO页面。 PRADO具有如下特点:

复用性 - 符合PRADO组件标准的代码是高度可重用的。
易于使用 - 使用PRADO组件非常简单,通常只需要配置它们的属性,编写相关的事件处理函数。 健壮性 - PRADO让开发者不再被那些令人厌烦的有很多错误的的代码所困扰。它的代码都是有对象,方法和属性组成的;而不是Url地址和各种query参数。PHP5最新的Exception处理机制也被引入进来,让开发人员能够精确定位到错误代码的所在行数。
高效 - PRADO开发的Web应用运行很快。PRADO实现了一种缓存机制,使用其开发的应用的运行效率能够和那些通常使用的模版引擎开发的应用的运行效率相媲美。
团队开发 - PRADO允许内容和表现方式分离。组件,尤其是页面,将内容(逻辑)和表现方式分别保存在不同的文件中

http://www.xisc.com/

标签: , , ,

星期二, 六月 05, 2007

100步笑50步,江恩股市铁律摘抄

  1.在所有的办法当中,最糟糕的一个办法就是过度交易(频繁或过量交易),其次是不下止损单,第三位的致命错误就是摊薄亏损。只要不犯这三种错误,就会取得成功。
  2.一定要顺势而动,不可逆势而上,千万不要追赶大潮退去时的回头浪。那些犹豫不决,在最后时刻才买进卖出的人必定都会遭受损失。
  3.所有影响都是某种原因产生的结果,而在公众看到这种影响之前这种原因就早已存在了。等到人们亲眼看到所造成的影响时,股市上损失也就不可避免了。
  4.如果股市不顺应人的意愿,人就要顺应股市去操作。聪明人会改变主意,傻瓜则永远不会。
  5.如果一时无法判定股市的趋势,那就暂时退出直到能够判定为止。趋势确定之后,总是能够赚到很多钱。
  6.安全总比后悔强......

转个贴,来自天涯

标签:

做事用手,诚信用心




刊于《中国证券报》

http://www.tianya.cn/new/publicforum/Content.asp?idWriter=11547547&Key=21607004&strItem=stocks&idArticle=315213&flag=1

标签: ,

伊湖水写给王小丫的情诗

《我爱上了王小丫》

  小丫是个可爱的女孩子
  要是不去主持电视节目多好
  最好和我一起在山坡上放牛
  我放牛的时候,她打猪草
  没人的时候就亲一亲
  当然不用避开牛
  有一次在山涧边放牛
  我边想着小丫边朝前走
  忘记了前面有山涧
  被牛咬住我的衣襟不放
  我回头训斥牛说
  快走开
  你这笨牛
  这辈子是不会懂人的事了!
  我想我没有说错
  不想我的牛却说话了
  它瓮声地回答我:
  这就像你这辈子也
  不会得到小丫一样!
  ——嗨!
  它也没说错!

  《给王小丫的情诗:一起燃烧》

  小丫,你知道吗
  今天有一位法师
  他说要来度湖水
  要湖水和他一起去
  恒河沙里的世界
  他说湖水是有缘人
  湖水的心真颤栗了
  那一瞬间
  有五千万尊恒河沙
  在湖水面前放出光芒
  奏出神圣的音乐
  湖水的身体摇晃了几晃才稳住
  就要抓起背包——不
  连背包也不要
  就跟他走了
  那是湖水心底
  最高的和终极的呼唤了
  也是湖水之心
  挣不脱的铁锁链
  ……
  小丫,你知道吗?
  湖水要看到他红色的小丫
  丫头的头上出现
  第一缕白羽毛
  湖水要看到他红色的小丫
  满头飞雪
  洗尽铅华
  到那个时候,湖水将默默地
 向他的小丫说声珍重
  然后拧熄电视机屏幕
  背上他的背包——不
  连背包都不背
  去找那位要度他的法师
  湖水知道他说过
  湖水是有缘人
  恒河沙世界的胸怀
  应该是可以容纳一个
  没有背包的
  流浪诗人


有人说是性骚扰,那以后别追女孩,也别写情书了。

标签:

辽ICP备05003652号
流风洄雪听天籁,轻云蔽日看落花

Powered by Blogger