2009年7月27日 星期一

MS Access 中的 like 語法

一般 SQL 都是用 '%' 及 '_' 當通配字符, 但 MS Access 中卻要用 '?' 及 '*'

2009年7月19日 星期日

用 php 寫 sqlite3

php 5.3.0 已開始支援 SQLite3:

$db=new sqlite3("my.sql3"); // use a database
if( $db )
{
$table="foo";
header("Content-Type: text/html;charset=big5");
$result=$db->query("Select * from $table");
$col=$result->numColumns();
$str="";
for($i=0;$i<$col;$i++) $str.=$result->columnName($i)."  ";
echo "$str
";

while( $s1=$result->fetchArray() )
{
$str="";
for($i=0;$i<$col;$i++) $str.=$s1[$i]."        ";
echo "$str
";
}
}
$db->close();
?>

如果要 delete, update, insert into, 等 SQL query, 可以執行 $db->exec($sql_query)

2009年7月15日 星期三

用 php 寫一個簡單的 sqlite 資料庫


$newdb ="stock.sqlite";
$table ="ase";
$db=sqlite_open($newdb,0666,$sqlerr);
if( $db )
{
$Q1=sqlite_query($db,"select count(serial) from ase");
if ( !$Q1 )
{
echo "Create $table
";
sqlite_query($db,"create table $table (serial int not null,name char(255),primary key(serial))");
$Q1=sqlite_query($db,"select count(serial) from ase");
}
$record=sqlite_fetch_array($Q1);
$id=intval($record[0]);
if( $id==0 )
{ // no record, add some record
echo "Insert $table
";
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第一筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第二筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第三筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第四筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第五筆')");
$id++;sqlite_query($db,"insert into $table (serial,name) values ('$id','第六筆')");
}
sqlite_close($db);
}
fclose($fin);

?>

AMP for windows

懶得安裝各式各樣 Server, 及繁瑣設定, 又想學 php 語法, 可考慮下載 WAMP Server:
http://www.wampserver.com/en/download.php
Appache2, php , mysql 安裝包一次搞定, 如果 mysql 要使用 utf8
記得修改 my.ini 內 client 及 mysqld 內的設定 default-character-set = utf8
這樣灌完就可在 local 端 (http://127.0.0.1) 執行 server 的 php 程式了.
記得將 php 程式放於 apache2 server 目錄內 htdocs 下面.
如果需要特殊外掛(例如 php_mysql.dll, php_sqlite, php_sockets ...等),
記得修改 php.ini 將對應的 dll 註解移除

在 Ubuntu 安裝 php-curl.dll, php-sqlite.dll

apt-get install php-curl,php-sqlite
就可以在 php script 使用了.

apache2

Appach2 是一隻很好用的 Web 伺服器(WWW Server), 支援 mysql, php, cgi-bin...等等, 又是開原程式碼,架設 httpd 非它莫屬

sqlite

SQLITE 是一個小巧好用的 SQL 資料庫語言.
php 支援它, 只要將 php.in 內 php_sqlite.dll
及 php_pdo.dll 註解 ';' 移除就可. 也可上官方
網站下載其原始檔, 用 Tiny C 就可直接編譯完成,
如果在安裝 sqlodbc driver, 就可使用 MS Access
透過 ODBC 方式,直接讀取或修改其所建立的資料庫.

使用 TCC 當作 cgi-bin

TinyC 是一隻很好用的 C compiler, 也可以當作 cgi-bin 的解譯器, 不會使用 php, 但熟 c 語言的老手, 可以考慮用它來寫伺服端軟體.
只要在 C 原始檔前面增加(假設 Tiny C 放在 c:\tcc 目錄下面)編譯指令碼就可以了:
#! c:/tcc/tcc.exe -run
將此原始碼hello.c放於 appach2 的 cgi-bin 目錄內, 就可執行了,tcc 會將他 compier後執行

#! c:/tcc/tcc.exe -run
#include
main()
{
printf("Content-Type: plain/text;charset=big5\n\n");
printf("Hello\n\");
}

安裝 apache2, TinyC, php, sqlite

1. 下載 apache2 for windows: http://httpd.apache.org/download.cgi
2. 下載 php for windows: http://www.php.net/downloads.php
3. 下載 TINYC compiler: http://bellard.org/tcc/
4. 下載 SQLITE: http://www.sqlite.org/download.html
5. 下載 sqlodbc: http://www.ch-werner.de/sqliteodbc/

2009年7月8日 星期三

octave 呼叫 DLL

/* http://www.gnu.org/software/octave/doc/interpreter/Oct_002dFiles.html#Oct_002dFiles
to compile, type
>mkoctfile calldll.cc
*/
// calldll.cc
#include "octave/oct.h"
#include "windows.h"
#include "stdio.h"
typedef void (*helloDLL)(char *in,char *out,int n);

DEFUN_DLD (calldll, args, nargout, "An example to call DLL\n")
{
HINSTANCE hDLL; // Handle to DLL
helloDLL hello;
octave_value_list OctRet;
static char ret[1024];
hDLL = LoadLibrary((LPCSTR)"Project1.dll"); // load dll
if( hDLL != NULL)
{
hello = (helloDLL) GetProcAddress(hDLL, "hello");
hello("send parameter",ret,0);
OctRet(0)="OK";
FreeLibrary(hDLL);
}
return OctRet;

}

用 Dev C++ 寫簡單的 DLL程式

/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include "windows.h"
#include "stdio.h"
extern "C"
{
DLLIMPORT void hello(char *in,char *out,int n);
}

DllClass::DllClass() { }
DllClass::~DllClass () { }
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}

DLLIMPORT void hello(char *in,char *out,int n)
{
int i;
printf ("DLL Hello %s\n",in);
out[0]='Y';
}

Octave's hello world

今天貼一個在 OCTAVE 上用 C 來寫程式
Octave 是一個數學運匴軟體,相當好用, 功能類似 matlab, 但它是自由軟體.學工程的決不能錯過, 且灌完後內建 C++ compiler, 可以用 C 來寫程式, 用 mkoctfile 來 complie, 之後會產生 .oct 檔, 該檔便可直接在命令列上呼叫, 如果要移除已載入的 .oct 程式, 可以下命令 clear all 或 clear 該oct 檔名即可

/*
Refer to URL: http://www.gnu.org/software/octave/doc/interpreter/Oct_002dFiles.html#Oct_002dFiles
use following syntax to compile under octave command line:
> mkoctfile hello.cc
Then it will generate a OCT file called hello.oct in the same directory
type
> p=hello
will execute the program, and the return message will be stored to variable 'p'
type
> help hello
will display the help message about the hello
In the end, it will return a string "OK" back to octave
type
>clear all
or
>clear hello
will unload the oct file
*/

// hello.cc
#include "octave/oct.h"
#include "windows.h"
#include "stdio.h"
DEFUN_DLD(hello,args,nargout,"Hello world\n")
{
octave_value_list OctRet;
printf("This is OCT file\n");
OctRet(0)="OK";
return OctRet;
}

2009年7月7日 星期二

奇怪, 這個 Blog 會將 tag 移除, 每改一次,就少了一些文字, 真是有夠怪?
想貼 php 原始碼都無法忠於原著, 放棄貼全部原碼, 只能貼部份, 因 function OTCstockmatch($match,$fin) 內含 html 的 tag, 會被該網站濾除, 就不貼了. 簡單來講利用 fgetcsv ($fin) 抓出資料, 再比對字串, 如果符合就將該筆資料的欄位內容抓出來, 利用 html 語法將它顯示到 browser 上,

function OTCstockprint($fin)
{
echo '資料來源:證券櫃檯買賣中心';
OTCstockmatch('名稱',$fin);
OTCstockmatch('聯勝',$fin);
echo '
';
}


if( 0 ) { $filename="emgstk.csv"; $fin=fopen($filename,"w+t");}
else $fin= tmpfile();
if( $fin )
{
$ch = curl_init();
if( $ch )
{
curl_setopt($ch, CURLOPT_URL,"http://nweb.otc.org.tw/emgstk/ch/emgstk.csv");
curl_setopt($ch, CURLOPT_FILE,$fin);
curl_exec($ch);
curl_close($ch);
OTCstockprint($fin);
}
fclose($fin);
}
?>

用 php 分析興櫃股票資訊

興櫃股票最新行情,檔案可以輕易自證券櫃檯買賣中心抓取,
URL 為 http://nweb.otc.org.tw/emgstk/ch/emgstk.csv
利用啟用 php CURL 外掛(在 php.ini 檔內 extension=php_curl.dll 的注解 ';' 移除即可),
稍作分析後只秀出自己關心的股票,程式碼如上