HTML数据库编程、JavaScript数据库编程
<html>
<head>
<!--
>>HTML database application demo
*--------------------------------------------------------------------------------*
Author: Sdiwen Date:2003-12-29 email:sdiwencn@yahoo.com.cn
>>Introduce
This program demo the operations of database(generate/init/list).
>>Structure
Access FileName: avatar.mdb
TableName: user
Fields: ID--AUTOINCREMENT, username--TEXT(50), userid--TEXT(50)
*--------------------------------------------------------------------------------*
>>HTML数据库操作演示程序
*--------------------------------------------------------------------------------*
作者:鼎文 日期:2003年12月29日 电子邮件:sdiwencn@yahoo.com.cn
>>介绍
本程序可以直接生成和初始化数据库,并遍历数据库中提取需要的数据显示出来
>>数据库结构
Access 文件名: avatar.mdb
表名: user
字段: ID--自动编号, username--文本(50), userid--文本(50)
*--------------------------------------------------------------------------------*
-->
<script language="javascript">
//创建数据表
var tablesqls=new Array(
"CREATE TABLE user([id] AUTOINCREMENT,[username] TEXT(50),[userid] TEXT(50) NOT NULL,PRIMARY KEY ([id]))"
);
//初始化数据
var initsqls=new Array(
"insert into user(userid) values('i687')",
"insert into user(userid) values('dudufay')",
"insert into user(userid) values('daisyfairy')",
"insert into user(userid) values('lifeng9552056')",
"insert into user(userid) values('du00')",
"insert into user(userid) values('joycle')",
"insert into user(userid) values('beuta')",
"insert into user(userid) values('xiaolanfangxiao')",
"insert into user(userid) values('yinruxue_-')",
"insert into user(userid) values('qixiaofei00')",
"insert into user(userid) values('skirty')",
"insert into user(userid) values('eveningprimrose')",
"insert into user(userid) values('riotgrrrl')",
"insert into user(userid) values('ghee')",
"insert into user(userid) values('coolzee')",
"insert into user(userid) values('anglo520')",
"insert into user(userid) values('bianguai')",
"insert into user(userid) values('bethhubing')",
"insert into user(userid) values('weixin622')",
"insert into user(userid) values('qiofei')",
"insert into user(userid) values('marksman1019361')",
"insert into user(userid) values('helb520')",
"insert into user(userid) values('zhaoying8225')",
"insert into user(userid) values('ticigi')",
"insert into user(userid) values('lemontea_520')"
);
//获取当前目录位置
function geturlpath(myurl){
var pos1;
var tempstr;
myurl=myurl.replace("file:///","");
myurl=myurl.replace("\\","/");
myurl=myurl.replace(/%20/g," ");
pos1=myurl.lastIndexOf("/");
tempstr=myurl.substring(0,pos1+1);
return tempstr;
}
var basepath=geturlpath(window.location.href);
var dbpath=basepath+"avatar.mdb";
var baseconn2k="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
var baseconn97="Provider=Microsoft.Jet.OLEDB.3.51;Data Source=";
var baseconn="driver={Microsoft Access Driver (*.mdb)};dbq=";
var connstr,conn,sql;
//document.write(dbpath);
//创建Access数据库文件
function createdb(filename,ver){
var CreateAccess=new ActiveXObject("ADOX.Catalog");
if (ver=="97"){
//创建Access97格式的数据库
CreateAccess.Create(baseconn97+filename+";");
}else{
//创建Access2000格式的数据库
CreateAccess.Create(baseconn2k+filename+";");
}
}
//运行SQL列表
function runsqls(sqlarray){
var sql="";
for (var i=0;i<sqlarray.length;i++){
sql=sqlarray[i];
if ((sql!="")&&(sql!=null)){
//alert(sql);
conn.execute(sql);
}
}
}
//判断文件是否存在
function FileExists(strFile){
var objTextStream = new ActiveXObject("ADODB.Stream");
objTextStream.open;
try{
objTextStream.LoadFromFile(strFile);
return true;
}
catch(Exception)
{
return false;
}
objTextStream.close;
}
//初始化数据库连接
function createconn(filename){
var tempresult=true;
connstr=baseconn+filename+";";
try{
conn = new ActiveXObject("ADODB.Connection");
//如果不存在先创建数据库
if (!(FileExists(dbpath))){
createdb(dbpath,"2k");
//alert("创建数据库成功 !");
conn.open(connstr);
runsqls(tablesqls);
//alert("创建数据表成功!");
runsqls(initsqls);
//alert("数据初始化完毕");
}else{
conn.open(connstr);
//alert("打开数据库成功!");
}
}
catch(Exception)
{
tempresult=false;
}
return tempresult;
}
//提取数据库数据合成函数
baseurl="http://avatar.etang.com/info/avatarhead.asp";
function genurls(){
var names,narray,i;
sql="select userid from user";
var rs = new ActiveXObject("ADODB.RecordSet");
rs.open(sql,conn,1);
names="";
while (!rs.EOF){
if (names!=""){
names=names+" "+rs("userid").value;
}else{
names=rs("userid").value;
}
rs.moveNext();
}
rs.Close();
conn.Close();
narray=names.split(" ")
for (var i=1;i<=narray.length;i++){
document.write("<iframe frameborder=0 src='"+baseurl+"?name="+narray[i]+"&type=0"+"' width=126 height=156></iframe>");
}
}
</script>
</head>
<body>
<script language="javascript">
var treturn=createconn(dbpath);
if (treturn){
genurls();
}else{
alert("数据库初始化失败!");
}
</script>
</body>
</html>
<head>
<!--
>>HTML database application demo
*--------------------------------------------------------------------------------*
Author: Sdiwen Date:2003-12-29 email:sdiwencn@yahoo.com.cn
>>Introduce
This program demo the operations of database(generate/init/list).
>>Structure
Access FileName: avatar.mdb
TableName: user
Fields: ID--AUTOINCREMENT, username--TEXT(50), userid--TEXT(50)
*--------------------------------------------------------------------------------*
>>HTML数据库操作演示程序
*--------------------------------------------------------------------------------*
作者:鼎文 日期:2003年12月29日 电子邮件:sdiwencn@yahoo.com.cn
>>介绍
本程序可以直接生成和初始化数据库,并遍历数据库中提取需要的数据显示出来
>>数据库结构
Access 文件名: avatar.mdb
表名: user
字段: ID--自动编号, username--文本(50), userid--文本(50)
*--------------------------------------------------------------------------------*
-->
<script language="javascript">
//创建数据表
var tablesqls=new Array(
"CREATE TABLE user([id] AUTOINCREMENT,[username] TEXT(50),[userid] TEXT(50) NOT NULL,PRIMARY KEY ([id]))"
);
//初始化数据
var initsqls=new Array(
"insert into user(userid) values('i687')",
"insert into user(userid) values('dudufay')",
"insert into user(userid) values('daisyfairy')",
"insert into user(userid) values('lifeng9552056')",
"insert into user(userid) values('du00')",
"insert into user(userid) values('joycle')",
"insert into user(userid) values('beuta')",
"insert into user(userid) values('xiaolanfangxiao')",
"insert into user(userid) values('yinruxue_-')",
"insert into user(userid) values('qixiaofei00')",
"insert into user(userid) values('skirty')",
"insert into user(userid) values('eveningprimrose')",
"insert into user(userid) values('riotgrrrl')",
"insert into user(userid) values('ghee')",
"insert into user(userid) values('coolzee')",
"insert into user(userid) values('anglo520')",
"insert into user(userid) values('bianguai')",
"insert into user(userid) values('bethhubing')",
"insert into user(userid) values('weixin622')",
"insert into user(userid) values('qiofei')",
"insert into user(userid) values('marksman1019361')",
"insert into user(userid) values('helb520')",
"insert into user(userid) values('zhaoying8225')",
"insert into user(userid) values('ticigi')",
"insert into user(userid) values('lemontea_520')"
);
//获取当前目录位置
function geturlpath(myurl){
var pos1;
var tempstr;
myurl=myurl.replace("file:///","");
myurl=myurl.replace("\\","/");
myurl=myurl.replace(/%20/g," ");
pos1=myurl.lastIndexOf("/");
tempstr=myurl.substring(0,pos1+1);
return tempstr;
}
var basepath=geturlpath(window.location.href);
var dbpath=basepath+"avatar.mdb";
var baseconn2k="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
var baseconn97="Provider=Microsoft.Jet.OLEDB.3.51;Data Source=";
var baseconn="driver={Microsoft Access Driver (*.mdb)};dbq=";
var connstr,conn,sql;
//document.write(dbpath);
//创建Access数据库文件
function createdb(filename,ver){
var CreateAccess=new ActiveXObject("ADOX.Catalog");
if (ver=="97"){
//创建Access97格式的数据库
CreateAccess.Create(baseconn97+filename+";");
}else{
//创建Access2000格式的数据库
CreateAccess.Create(baseconn2k+filename+";");
}
}
//运行SQL列表
function runsqls(sqlarray){
var sql="";
for (var i=0;i<sqlarray.length;i++){
sql=sqlarray[i];
if ((sql!="")&&(sql!=null)){
//alert(sql);
conn.execute(sql);
}
}
}
//判断文件是否存在
function FileExists(strFile){
var objTextStream = new ActiveXObject("ADODB.Stream");
objTextStream.open;
try{
objTextStream.LoadFromFile(strFile);
return true;
}
catch(Exception)
{
return false;
}
objTextStream.close;
}
//初始化数据库连接
function createconn(filename){
var tempresult=true;
connstr=baseconn+filename+";";
try{
conn = new ActiveXObject("ADODB.Connection");
//如果不存在先创建数据库
if (!(FileExists(dbpath))){
createdb(dbpath,"2k");
//alert("创建数据库
conn.open(connstr);
runsqls(tablesqls);
//alert("创建数据表成功!");
runsqls(initsqls);
//alert("数据初始化完毕");
}else{
conn.open(connstr);
//alert("打开数据库成功!");
}
}
catch(Exception)
{
tempresult=false;
}
return tempresult;
}
//提取数据库数据合成函数
baseurl="http://avatar.etang.com/info/avatarhead.asp";
function genurls(){
var names,narray,i;
sql="select userid from user";
var rs = new ActiveXObject("ADODB.RecordSet");
rs.open(sql,conn,1);
names="";
while (!rs.EOF){
if (names!=""){
names=names+" "+rs("userid").value;
}else{
names=rs("userid").value;
}
rs.moveNext();
}
rs.Close();
conn.Close();
narray=names.split(" ")
for (var i=1;i<=narray.length;i++){
document.write("<iframe frameborder=0 src='"+baseurl+"?name="+narray[i]+"&type=0"+"' width=126 height=156></iframe>");
}
}
</script>
</head>
<body>
<script language="javascript">
var treturn=createconn(dbpath);
if (treturn){
genurls();
}else{
alert("数据库初始化失败!");
}
</script>
</body>
</html>