本帖最后由 ceoguang 于 2010-8-25 20:32 编辑
SQLite是一款轻型的数据库,
关于它的特点,可以参考网上的相关文章。
令人兴奋的是,AutoIT UDFs 支持对 SQLite 的操作。
遗憾的是到今天为止,论坛上没有相关的文章和资料可供参考,
我根据自己学习的心得体会,写了“SQLite 数据库操作实例”,
希望能抛砖引玉,帮助大家在写 AutoIT + SQLite 程序的过程中有所帮助。
源代码中用到的主要语句,大家可以参考AutoIT的帮助文档:
_SQLite_Startup
_SQLite_Open
_SQLite_Exec
_SQLite_Query
_SQLite_QuerySingleRow
_SQLite_Close
_SQLite_Shutdown
_GUICtrlListView_AddColumn
_GUICtrlListView_DeleteAllItems
_GUICtrlListView_AddItem
_GUICtrlListView_AddSubItem
_GUICtrlListView_FindInText
#NoTrayIcon
#include <GUIListBox.au3>
#Include <GuiListView.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
Opt("TrayIconHide", 1)
Opt('MustDeclareVars', 1)
Opt("GUICloseOnESC", 0)
Global $GUI_Form, $Title, $SQLite_Data_Path, $GUI_ListBox
Global $GUI_Input1, $GUI_Input2, $GUI_Input3, $GUI_Input4, $GUI_Input5
Global $GUI_Button1, $GUI_Button2, $GUI_Button3, $GUI_Button4
Global $Msg, $hQuery, $aRow
Global $Temp, $a, $b, $c
$Title = "SQLite操作实例..."
$SQLite_Data_Path = "SQLite.db"
_SQLite_Startup () ;加载 SQLite.dll
If Not FileExists($SQLite_Data_Path) Then
SQLCreate()
EndIf
$GUI_Form = GUICreate($Title, 300, 435, -1, -1)
GUISetBkColor(0xECE9D8) ; will change background color
$GUI_ListBox = GUICtrlCreateListView("", 2, 2, 296, 309, 0x0010)
_GUICtrlListView_AddColumn($GUI_ListBox, "编号", 100, 0)
_GUICtrlListView_AddColumn($GUI_ListBox, "姓名", 80, 1)
_GUICtrlListView_AddColumn($GUI_ListBox, "年龄", 80, 1)
GUICtrlCreateLabel("编号 姓名 年龄", 34, 325, 240, 15)
$GUI_Input1 = GUICtrlCreateInput("", 10, 341, 73, 20)
$GUI_Input2 = GUICtrlCreateInput("", 88, 341, 73, 20)
$GUI_Input3 = GUICtrlCreateInput("", 166, 341, 73, 20)
$GUI_Input4 = GUICtrlCreateInput("", 88, 366, 73, 20)
$GUI_Input5 = GUICtrlCreateInput("", 88, 391, 73, 20)
$GUI_Button1 = GUICtrlCreateButton("读取", 246, 315, 48, 22, 0)
$GUI_Button2 = GUICtrlCreateButton("写入", 246, 340, 48, 22, 0)
$GUI_Button3 = GUICtrlCreateButton("查找", 246, 365, 48, 22, 0)
$GUI_Button4 = GUICtrlCreateButton("删除", 246, 390, 48, 22, 0)
GUICtrlCreateGroup("", 2, 307, 296, 107)
GUICtrlCreateLabel("「江西樟树·仅此一仙·QQ:43848058」制作", 32, 419, 240, 15)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()
While 1
$Msg = GUIGetMsg()
Select
Case $Msg = $GUI_EVENT_CLOSE
_SQLite_Shutdown () ;卸载 SQLite.dll
ExitLoop
Case $Msg = $GUI_Button1
SQLiteRead()
Case $Msg = $GUI_Button2
SQLiteInsert(GUICtrlRead ($GUI_Input1), GUICtrlRead ($GUI_Input2), GUICtrlRead ($GUI_Input3))
SQLiteRead()
Case $Msg = $GUI_Button3
SQLiteSelect(GUICtrlRead ($GUI_Input4))
Case $Msg = $GUI_Button4
SQLiteDelete(GUICtrlRead ($GUI_Input5))
SQLiteRead()
EndSelect
WEnd
Func SQLCreate()
_SQLite_Open ($SQLite_Data_Path)
_SQLite_Exec(-1, "Create Table IF NOT Exists TestTable (IDs Text PRIMARY KEY, Name Text, Age Text);")
_SQLite_Close ()
EndFunc
Func SQLiteRead()
_GUICtrlListView_DeleteAllItems ( GUICtrlGetHandle ($GUI_ListBox) )
_SQLite_Open ($SQLite_Data_Path)
_SQLite_Query(-1, "SELECT * FROM TestTable ORDER BY IDs DESC;",$hQuery)
While _SQLite_FetchData ($hQuery, $aRow) = $SQLITE_OK
_GUICtrlListView_AddItem($GUI_ListBox, $aRow[0])
_GUICtrlListView_AddSubItem($GUI_ListBox, _GUICtrlListView_FindInText($GUI_ListBox, $aRow[0]), $aRow[1], 1)
_GUICtrlListView_AddSubItem($GUI_ListBox, _GUICtrlListView_FindInText($GUI_ListBox, $aRow[0]), $aRow[2], 2)
WEnd
_SQLite_Close ()
EndFunc
Func SQLiteInsert($a, $b, $c)
_SQLite_Open ($SQLite_Data_Path)
_SQLite_QuerySingleRow(-1, "SELECT IDs FROM TestTable WHERE IDs = '" & $a & "';", $aRow)
$Temp = $aRow[0]
If $Temp = "" Then
_SQLite_Exec(-1, "Insert into TestTable (IDs) values ('" & $a & "');")
EndIf
_SQLite_Exec(-1, "UPDATE TestTable SET Name = '" & $b & "' WHERE IDs = '" & $a & "';")
_SQLite_Exec(-1, "UPDATE TestTable SET Age = '" & $c & "' WHERE IDs = '" & $a & "';")
_SQLite_Close ()
EndFunc
Func SQLiteSelect($a)
_SQLite_Open ($SQLite_Data_Path)
_SQLite_QuerySingleRow(-1, "SELECT * FROM TestTable WHERE Name = '" & $a & "';", $aRow)
$Temp = $aRow[0]
If $Temp = "" Then
MsgBox(262208, "查找结果...", "数据库中姓名为 [" & $a & "] 的员工记录不存在!")
Else
_GUICtrlListView_DeleteAllItems ( GUICtrlGetHandle ($GUI_ListBox) )
_GUICtrlListView_AddItem($GUI_ListBox, $aRow[0])
_GUICtrlListView_AddSubItem($GUI_ListBox, _GUICtrlListView_FindInText($GUI_ListBox, $aRow[0]), $aRow[1], 1)
_GUICtrlListView_AddSubItem($GUI_ListBox, _GUICtrlListView_FindInText($GUI_ListBox, $aRow[0]), $aRow[2], 2)
MsgBox(262208, "查找结果...", "找到记录: 编号[" & $aRow[0] & "] 姓名[" & $aRow[1] & "] 年龄[" & $aRow[2] & "] !")
EndIf
_SQLite_Close ()
EndFunc
Func SQLiteDelete($a)
_SQLite_Open ($SQLite_Data_Path)
_SQLite_Exec(-1, "DELETE FROM TestTable WHERE Name = '" & $a & "';")
_SQLite_Close ()
MsgBox(262208, "删除记录...", "删除数据库中姓名为 [" & $a & "] 的员工记录!")
EndFunc
================黄金分割线==================
BY HOOK
借位置一用
Sqlite是支持加密的.参考开源项目
http://sourceforge.net/projects/ ... ilename&sortdir=asc
操作方法:
打开数据库 sqlite3_open,然后在操作数据库之前执行 sqlite3_key 后就可进行数据库操作,否则会返回错误。
sqlite3_key是输入密钥,如果数据库已加密必须先执行此函数并输入正确密钥才能进行操作,如果数据库没有加密,执行此函数后进行数据库操作反而会出现“此数据库已加密或不是一个数据库文件”的错误。
int sqlite3_key( sqlite3 *db, const void *pKey, int nKey),db 是指定数据库,pKey 是密钥,nKey 是密钥长度。例:sqlite3_key( db, "abc", 3);
sqlite3_rekey是变更密钥或给没有加密的数据库添加密钥或清空密钥,变更密钥或清空密钥前必须先正确执行 sqlite3_key。在正确执行 sqlite3_rekey 之后在 sqlite3_close 关闭数据库之前可以正常操作数据库,不需要再执行 sqlite3_key。
int sqlite3_rekey( sqlite3 *db, const void *pKey, int nKey),参数同上。
清空密钥为 sqlite3_rekey( db, NULL, 0)
================黄金分割线完毕================== |