您的位置:首页 > 数据库

how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language

2011-01-05 22:45 1336 查看
 

This article describes how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language.

SQLite
I first heard of SQLite from Tim Anderson's website (www.itwriting.com) and then, when I read that Google and Adobe were using it in their applications, I thought I should investigate and try it for myself.

You will find on the SQLite website (www.sqlite.org) a number of advantages to using SQLite. Here are some points that I thought were useful:-

No database server is needed; the whole database is in a single file.

The filesize is relatively small. I converted one small database from Access to SQLite with a reduction in filesize from 104 kb to 6 kb.

SQLite comes as part of the PHP5 package and PHP5 has good support for this database.

I am also interested in seeing whether I could use a simple database with PHP5 in one of my Linux-hosted websites, but here I thought it would be useful to see whether SQLite could replace Access or Ability in HTML database programs.

What you need
SQLite to be installed on your computer. Go to the SQLite download page and download the latest precompile binary as a .zip file (currently 178kB) and follow the installation instructions.

A means of setting up your database. If you are feeling macho, you can set up a SQLite database from a command line interface. However a GUI-based program is easier. Two freeware programs for Windows that I have tried and found very useful are SQLite Administrator and SQLite2007 Pro Enterprise Manager. Both allow conversion of a database from MS Access to SQLite.

An ODBC Driver for SQLite which is available from Christian Werner's website. This is only a 1.43 MB download.

Cost of all this: Nothing
Quality: Excellent!

You now have the materials for producing compact databases with compact programs.

Connecting to the database
I am assuming that you can follow the instructions to set up a database with one of the recommended programs. I converted a MS Access database to SQLite3 with SQLite2007 Pro Enterprise Manager and found it straightforward with the wizard supplied. I recommend that you save the file formed with the ending ".db3".

You can set up a DSN connection in Windows as follows:

Go to the Control Panel and select "ODBC Data Sources". (in XP it is under "Performance and Maintenance" and then "Administrative Tools")

On the interface form, select the tab "User DSN".

Click on "Add" and select "SQLite3 ODBC Driver" and click on "Finish". This driver should be there if you have installed Christian Werner's ODBC Driver.

Enter a name for the Data Source, e.g. Clientsqlite.

Under "Database Name", click on "Browse" and select the database you created.

Click on OK and you are finished.

The connection strings for connecting through a DSN and without a DSN are shown below. For a DSN connection, you need the name of the Data Source that you created. For a DSN-less connection, you need the path of the database.

Connection through DSN:

Set objConn = CreateObject("ADODB.Connection")
objconn.Open "DSN=mydsnsource"

Connection without DSN:

Set objConn = CreateObject("ADODB.Connection")
objConn.Open "DRIVER=SQLite3 ODBC Driver;Database=C:/myfolder/mydb.db3;"


The Interface
Now we know how to get an ADO connection, the rest is relatively straightforward as we can use the same interface as described in a previous article, "Operating a database". The code can be exactly the same.

We will make one useful addition, however - a selector based on a dropdown menu:-

 
--Select a client--Client 1Client 2Client 3
 

The menu will be populated by key information from the database, e.g, a client's name based on the fields Firstname + LastName. Frequently, you find a button to the right of the menu to activate the selection. However, it is simpler if you can just click on the option to activate the process of finding the record. This we can do if we use the event onChange which activates a subroutine called findrecord which we will discuss shortly.

document.write "<form name = 'firstform'>"
document.write "<select name = 'selectid' onchange = 'vbscript:findrecord'>"
document.write "<option value = '0'>--Select a client--</option>"
objrs.movefirst
while not objrs.eof
document.write "<option value = " & objrs.fields("id").value & ">" & _
objrs.fields("FirstName").value & " " & objrs.fields("LastName").value & "</option>"
objrs.movenext
wend
document.write "</select><br /><br />"
document.write "</ form>


Each option in the menu has a value which is the primary key in the database, in this case an auto-incrementing integer called 'id'. The text for the menu option is obtained by joining together the field FirstName, a space and the field LastName. The code iterates through the records in a while .. wend loop adding names until the end of the recordset is reached. The first option "--Select a client--" is just to tell the user what to do and this has been given a value of 0.

The subroutine findrecord

sub findrecord
dim rsnumber
rsnumber = document.firstform.selectid.value
if rsnumber >0 then
objrs.movefirst
objrs.find("id='" & rsnumber & "'")
call fillform
end if
end sub


First we need to find the value of the selected item, which wiil give the index id of the record we must retrieve. Using the Document Object Model, we assign a variable, Rsnumber to document.firstform.selectid.value. You will remember that we need to exclude the case that someone clicks on the heading "--Select a client--" which has a value of 0. We make the operation conditional that rsnumber is greater than 0. If it is 0, nothing happens. To find the record, we use the method find which requires us first to go to the beginning of the recordset. Having found the correct record, the subroutine fillform is used to populate the textboxes on the form

The full program
The code for the full program can be obtained by clicking here. Internet Explorer will attempt to run the program without success, but if you goto "View Source", the full code can be copied and adapted for your use.

Conclusion
SQLite produces fast compact databases. The largest of the three that I tried was 48 kb and all were much smaller than their MS Access equivalents.

These SQLite databases can be set up or tested with quality free software and, by using an ODBC driver, they can be programmed with VBScript in an HTML program giving a compact interface.

 

 

 

all you need is an OLEDB or ODBC wrapper for sqlite, and there are some around, check /
the wiki. once you install one, your sqlite is accesible from ADO, just like any /
other database (almost)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐