找回密码
 加入
搜索
查看: 148693|回复: 277

[原创] SQLite 数据库操作实例(读取、写入、查找、删除)!

[复制链接]
发表于 2008-7-29 13:11:25 | 显示全部楼层 |阅读模式
本帖最后由 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)

================黄金分割线完毕==================

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 2金钱 +14 贡献 +5 收起 理由
rho123 + 2 精品文章
sanhen + 12 + 5 原创内容,感谢你对论坛的支持;希望继续发 ...

查看全部评分

发表于 2008-7-29 15:03:45 | 显示全部楼层
其实数据库方面,语法都是差不多的。
发表于 2008-8-5 15:24:59 | 显示全部楼层
多谢分享了,正需要呢
发表于 2008-8-8 08:30:42 | 显示全部楼层
能支持本地就不错了,脚本你还指望做网络服务器?
发表于 2008-8-8 11:00:27 | 显示全部楼层
多个同姓名记录呢?楼主这个只支持查找并显示一个记录。。
希望楼主指点~~~谢谢
 楼主| 发表于 2008-8-13 09:46:22 | 显示全部楼层

回复 5# boyhong 的帖子

同名同姓的记录其实道理是一样的,用_SQLite_Query,相关例子可以参照帮助里面的例子。

[ 本帖最后由 zplinux 于 2008-8-13 10:28 编辑 ]
发表于 2008-8-29 17:50:38 | 显示全部楼层
谢谢楼主的提供这么全的代码。
发表于 2008-9-5 13:42:38 | 显示全部楼层
多谢分享,昨天就想找了,今天终于找到了
发表于 2008-9-5 15:07:31 | 显示全部楼层
我还是直接用TXT吧
发表于 2008-9-22 14:40:12 | 显示全部楼层
更新数据呢
发表于 2008-9-22 19:55:27 | 显示全部楼层
公式计算能做吗
发表于 2008-9-23 17:12:41 | 显示全部楼层
楼主可真是强人
学习了 这是好东西啊
发表于 2008-9-23 18:54:24 | 显示全部楼层
学习一下。多谢!
发表于 2008-10-23 14:01:56 | 显示全部楼层
感谢LZ分享
 楼主| 发表于 2008-10-27 15:01:11 | 显示全部楼层
原帖由 netegg 于 2008-9-22 19:55 发表
公式计算能做吗


公式计算当然可以做到。可以用参数传递、函数结合起来,实现很简单的。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-28 23:20 , Processed in 0.083743 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表