VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)

VB爱好者乐园(VBGood)

 找回密码
 立即注册
搜索
查看: 48744|回复: 186

Vb6版 SQLite3[3.8.7.2] 源码

  [复制链接]
发表于 2014-11-24 14:30:37 | 显示全部楼层 |阅读模式
本帖最后由 19900603 于 2014-11-24 21:51 编辑

网上找了半天没找到包装器源码啊 自己写了个包装器 包装了下。。
下面 三项 SQLite3 源码
1.SQLite3原版DLL 源码(vb无法直接调用)
2.SQLite3 包装器源码 (用下面API调用)
3.SQLite3+包装器一体源码直接一个DLL搞定 (用下面API调用)

VS2008 + Win7X64 下编译测试成功




游客,如果您要查看本帖隐藏内容请回复



Vb模块
  1. Option Explicit
  2.                                                                  
  3. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
  4. Public Declare Sub sqlite_free_table Lib "SQLite" (ByVal ResultPtr As String)
  5. Public Declare Sub sqlite_initialize Lib "SQLite" ()
  6. Public Declare Sub sqlite_interrupt Lib "SQLite" (ByVal Hdb As Long)
  7. Public Declare Sub sqlite_free Lib "SQLite" (ptr As Any)
  8. Public Declare Function sqlite_open Lib "SQLite" (ByVal pwsFileName As String, ByRef Hdb As Long) As Long ' PtrDb
  9. Public Declare Function sqlite_close Lib "SQLite" (ByVal Hdb As Long) As Long
  10. Public Declare Function sqlite_finalize Lib "SQLite" (ByVal hstmt As Long) As Long
  11. Public Declare Function sqlite_prepare Lib "SQLite" (ByVal Hdb As Long, ByVal pwsSql As String, ByRef hstmt As Long) As Long
  12. Public Declare Function sqlite_sleep Lib "SQLite" (ByVal Times As Long) As Long
  13. Public Declare Function sqlite_column_int Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  14. Public Declare Function sqlite_column_ptext Lib "SQLite" Alias "sqlite_column_text" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  15. Public Declare Function sqlite_step Lib "SQLite" (ByVal hstmt As Long) As Long
  16. Public Declare Function sqlite_exec Lib "SQLite" (ByVal Hdb As Long, ByVal pwsSql As String) As Long
  17. Public Declare Function sqlite_get_table Lib "SQLite" (ByVal db As Long, ByVal zSql As String, ByRef pazResult As String, ByRef pnRow As Long, ByRef pnColumn As Long) As Long
  18. Public Declare Function sqlite_limit Lib "SQLite" (ByVal db As Long, ByVal Id As Long, ByVal NewVal As Long) As Long
  19. Public Declare Function sqlite_libversion Lib "SQLite" () As Long
  20. Public Declare Function sqlite_libversion_number Lib "SQLite" () As Long
  21. Public Declare Function sqlite_reset Lib "SQLite" (ByVal hstmt As Long) As Long
  22. Public Declare Function sqlite_errcode Lib "SQLite" (ByVal db As Long) As Long
  23. Public Declare Function sqlite_changes Lib "SQLite" (ByVal db As Long) As Long
  24. Public Declare Function sqlite_total_changes Lib "SQLite" (ByVal db As Long) As Long
  25. Public Declare Function sqlite_malloc Lib "SQLite" (ByVal nByte As Long) As Long
  26. Public Declare Function sqlite_errmsgchar Lib "SQLite" Alias "sqlite_errmsg" (ByVal db As Long) As Long '字符串指针
  27. Public Declare Function sqlite_column_database_pname Lib "SQLite" Alias "sqlite_column_database_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  28. Public Declare Function sqlite_column_table_pname Lib "SQLite" Alias "sqlite_column_table_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  29. Public Declare Function sqlite_column_origin_pname Lib "SQLite" Alias "sqlite_column_origin_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  30. Public Declare Function sqlite_column_declptype Lib "SQLite" Alias "sqlite_column_decltype" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  31. Public Declare Function sqlite_data_count Lib "SQLite" (ByVal hstmt As Long) As Long
  32. Rem 判断语句是否以分号结尾
  33. Public Declare Function sqlite_complete Lib "SQLite" (ByVal zSql As String) As Long
  34. Public Declare Function sqlite_column_count Lib "SQLite" (ByVal hstmt As Long) As Long
  35. Public Declare Function sqlite_column_ptype Lib "SQLite" Alias "sqlite_column_type" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  36. Public Declare Function sqlite_column_pname Lib "SQLite" Alias "sqlite_column_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  37. Public Declare Function sqlite_column_blob Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  38. Public Declare Function sqlite_column_bytes Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  39. Public Declare Function sqlite_column_double Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Double
  40. Public Declare Function sqlite_column_value Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  41. Public Declare Function sqlite_bind_parameter_count Lib "SQLite" (ByVal hstmt As Long) As Long
  42. Public Declare Function sqlite_bind_parameter_pname Lib "SQLite" Alias "sqlite_bind_parameter_name" (ByVal hstmt As Long, ByVal paramIndex As Long) As Long '字符串指针
  43. Public Declare Function sqlite_bind_parameter_index Lib "SQLite" (ByVal hstmt As Long, ByVal zName As String) As Long
  44. Public Declare Function sqlite_bind_null Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long) As Long
  45. Public Declare Function sqlite_bind_blob Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, Dataptr As Any, ByVal nByte As Long) As Long
  46. Public Declare Function sqlite_bind_zeroblob Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal nByte As Long) As Long
  47. Public Declare Function sqlite_bind_double Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal nByte As Double) As Long
  48. Public Declare Function sqlite_bind_int Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal nByte As Long) As Long
  49. Public Declare Function sqlite_bind_ptext Lib "SQLite" Alias "sqlite_bind_text" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal lpStr As String) As Long
  50. Public Declare Function sqlite_bind_value Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal NewVal As Long) As Long
  51. Public Declare Function sqlite_clear_bindings Lib "SQLite" (ByVal hstmt As Long) As Long
  52. Public Declare Function sqlite_last_insert_rowid Lib "SQLite" (ByVal Hdb As Long) As Long
  53. Public Declare Function sqlite_backup_init Lib "SQLite" (ByVal pDestDC As Long, ByVal zDestName As String, ByVal pSourceDC As Long, ByVal zSourceName As String) As Long
  54. Public Declare Function sqlite_backup_step Lib "SQLite" (ByVal pBackup As Long, ByVal nPage As Long) As Long
  55. Public Declare Function sqlite_backup_finish Lib "SQLite" (ByVal pBackup As Long) As Long
  56. Public Declare Function sqlite_backup_remaining Lib "SQLite" (ByVal pBackup As Long) As Long
  57. Public Declare Function sqlite_backup_pagecount Lib "SQLite" (ByVal pBackup As Long) As Long
  58. Public Declare Function CopyStr Lib "kernel32" Alias "lstrcpynA" (ByVal lpStringDestination As String, ByVal lpStringSource As Long, ByVal lngMaxLength As Long) As Long
  59. Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
  60. Public Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
  61. Public Declare Function lstrcpynW Lib "kernel32" (ByVal pwsDest As Long, ByVal pwsSource As Long, ByVal cchCount As Long) As Long
  62. Public Declare Function lstrcpyW Lib "kernel32" (ByVal pwsDest As Long, ByVal pwsSource As Long) As Long
  63. Public Declare Function lstrlenW Lib "kernel32" (ByVal pwsString As Long) As Long
  64. Private Const CP_UTF8          As Long = 65001
  65. Enum SQLITE_RETURN
  66.     SQLITE_OK = 0
  67.     SQLITE_ERROR = 1
  68.     SQLITE_INTERNAL = 2
  69.     SQLITE_PERM = 3
  70.     SQLITE_ABORT = 4
  71.     SQLITE_BUSY = 5
  72.     SQLITE_LOCKED = 6
  73.     SQLITE_NOMEM = 7
  74.     SQLITE_READONLY = 8                                                         ' Attempt to write a readonly database
  75.     sqlite_interruptD = 9                                                       ' Operation terminated by sqlite3_interrupt()
  76.     SQLITE_IOERR = 10                                                           ' Some kind of disk I/O error occurred
  77.     SQLITE_CORRUPT = 11                                                         ' The database disk image is malformed
  78.     SQLITE_NOTFOUND = 12                                                        ' (Internal Only) Table or record not found
  79.     SQLITE_FULL = 13                                                            ' Insertion failed because database is full
  80.     SQLITE_CANTOPEN = 14                                                        ' Unable to open the database file
  81.     SQLITE_PROTOCOL = 15                                                        ' Database lock protocol error
  82.     SQLITE_EMPTY = 16                                                           ' Database is empty
  83.     SQLITE_SCHEMA = 17                                                          ' The database schema changed
  84.     SQLITE_TOOBIG = 18                                                          ' Too much data for one row of a table
  85.     SQLITE_CONSTRAINT = 19                                                      ' Abort due to contraint violation
  86.     SQLITE_MISMATCH = 20                                                        ' Data type mismatch
  87.     SQLITE_MISUSE = 21                                                          ' Library used incorrectly
  88.     SQLITE_NOLFS = 22                                                           ' Uses OS features not supported on host
  89.     SQLITE_AUTH = 23                                                            ' Authorization denied
  90.     SQLITE_FORMAT = 24                                                          ' Auxiliary database format error
  91.     SQLITE_RANGE = 25                                                           ' 2nd parameter to sqlite3_bind out of range
  92.     SQLITE_NOTADB = 26                                                          ' File opened that is not a database file
  93.     SQLITE_ROW = 100                                                            ' sqlite3_step() has another row ready
  94.     SQLITE_DONE = 101                                                           ' sqlite3_step() has finished executing
  95. End Enum
  96. Public Const SQLITE_TRANSIENT  As Long = 0

  97. Public Function UTF8StringFromPtr(ByVal pUtf8String As Long) As String
  98.     Dim cSize As Long
  99.     UTF8StringFromPtr = ""
  100.     cSize = MultiByteToWideChar(CP_UTF8, 0, pUtf8String, -1, 0, 0)
  101.     If cSize > 1 Then
  102.         UTF8StringFromPtr = Space(cSize - 1)                                    'String(cSize - 1, " ")
  103.         MultiByteToWideChar CP_UTF8, 0, pUtf8String, -1, StrPtr(UTF8StringFromPtr), cSize
  104.     End If
  105. End Function




  106. Public Function sqlite_bind_text(hstmt As Long, iCol As Long, lpStr As String) As Long

  107.     sqlite_bind_text = sqlite_bind_ptext(hstmt, iCol, StrConv(lpStr, vbUnicode))

  108. End Function


  109. Private Function BytesFromPtr(ByVal lAddr As Long, ByVal lSize As Long) As Byte()
  110.     ReDim bvData(lSize - 1) As Byte
  111.     CopyMemory bvData(0), ByVal lAddr, lSize
  112.     BytesFromPtr = bvData
  113. End Function
  114.                                                                     
  115. Public Function SQLite_Vacuum(h_DB As Long) As Boolean
  116.     Dim hstmt As Long, lpReturn As SQLITE_RETURN
  117.     lpReturn = sqlite_exec(h_DB, "VACUUM")
  118.     SQLite_Vacuum = (lpReturn = SQLITE_OK)
  119. End Function
  120.                                                                     
  121. Public Function sqlite3_column_name(hstmt As Long, iCol As Long) As String
  122.     Dim lpReturn As Long
  123.     lpReturn = sqlite_column_name(hstmt, iCol)
  124.     sqlite3_column_name = UTF8StringFromPtr(lpReturn)
  125. End Function
  126.                                                                     
  127. Public Function sqlite_column_text(hstmt As Long, iCol As Long) As String
  128.     Dim lpReturn As Long
  129.     lpReturn = sqlite_column_ptext(hstmt, iCol)
  130.     sqlite_column_text = UTF8StringFromPtr(lpReturn)
  131. End Function
  132.                                                                     
  133. Public Function sqlite_column_database_name(hstmt As Long, iCol As Long) As String
  134.     Dim lpReturn As Long
  135.     lpReturn = sqlite_column_database_pname(hstmt, iCol)
  136.     sqlite_column_database_name = UTF8StringFromPtr(lpReturn)
  137. End Function
  138.                                                                     
  139. Public Function sqlite_column_table_name(hstmt As Long, iCol As Long) As String
  140.     Dim lpReturn As Long
  141.     lpReturn = sqlite_column_table_pname(hstmt, iCol)
  142.     sqlite_column_table_name = UTF8StringFromPtr(lpReturn)
  143. End Function
  144.                                                                     
  145. Public Function sqlite_column_origin_name(hstmt As Long, iCol As Long) As String
  146.     Dim lpReturn As Long
  147.     lpReturn = sqlite_column_origin_pname(hstmt, iCol)
  148.     sqlite_column_origin_name = UTF8StringFromPtr(lpReturn)
  149. End Function
  150.                                                                     
  151. Public Function sqlite_column_decltype(hstmt As Long, iCol As Long) As String
  152.     Dim lpReturn As Long
  153.     lpReturn = sqlite_column_declptype(hstmt, iCol)
  154.     sqlite_column_decltype = UTF8StringFromPtr(lpReturn)
  155. End Function
  156.                                                                     
  157. Public Function sqlite_errmsg(Hdb As Long) As String
  158.     Dim lpReturn As Long
  159.     lpReturn = sqlite_errmsgchar(Hdb)
  160.     sqlite_errmsg = UTF8StringFromPtr(lpReturn)
  161. End Function
  162.                                                                     
  163. Public Function sqlite_column_type(hstmt As Long, iCol As Long) As String
  164.     Dim lpReturn As Long
  165.     lpReturn = sqlite_column_ptype(hstmt, iCol)
  166.     sqlite_column_type = UTF8StringFromPtr(lpReturn)
  167. End Function
  168.                                                                     
  169. Public Function sqlite_column_name(hstmt As Long, iCol As Long) As String
  170.     Dim lpReturn As Long
  171.     lpReturn = sqlite_column_pname(hstmt, iCol)
  172.     sqlite_column_name = UTF8StringFromPtr(lpReturn)
  173. End Function
  174.                                                                     
  175. Public Function sqlite_bind_parameter_name(hstmt As Long, paramIndex As Long) As String
  176.     Dim lpReturn As Long
  177.     lpReturn = sqlite_bind_parameter_pname(hstmt, paramIndex)
  178.     sqlite_bind_parameter_name = UTF8StringFromPtr(lpReturn)
  179. End Function
  180.                                                                     
  181. Public Function sqlite_next(hstmt As Long) As Boolean
  182.     sqlite_next = (sqlite_step(hstmt) = SQLITE_ROW)
  183. End Function
  184.                                                                                                             
复制代码
SQLite.Cpp 下(项目2和项目3都要换)
  1. _declspec(dllexport) int sqlite_exec(sqlite3 * db,                                  /* An open database */
  2.                                                                                         const char *sql                           /* SQL to be evaluated */
  3.                                                                                         )
复制代码
换成
  1. _declspec(dllexport) int WINAPI sqlite_exec(sqlite3 * db,                                  /* An open database */
  2.                                                                                         const char *sql                           /* SQL to be evaluated */
  3.                                                                                         )
复制代码
  1. _declspec(dllexport) int WINAPI sqlite_bind_text(sqlite3_stmt*pStmt, int paramIndex,  const char* lpstr)
  2. {
  3.         return sqlite3_bind_text(pStmt,paramIndex,lpstr,-1,0);
  4. }
复制代码
换成
  1. _declspec(dllexport) int WINAPI sqlite_bind_text(sqlite3_stmt*pStmt, int paramIndex,  const char* lpstr)
  2. {
  3.         return sqlite3_bind_text16(pStmt,paramIndex,lpstr,-1,0);
  4. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

点评

模块更新见11楼  发表于 2014-11-27 18:45
试了 貌似不行 - -!  发表于 2014-11-24 21:50
弱弱地告诉你,在VS工程选项里面可以强制设置调用约定成stdcall  发表于 2014-11-24 19:07

评分

参与人数 4威望 +26 人气 +8 收起 理由
cybman + 1 赞一个
vbguest + 1 + 1 很给力
yangxb771224 + 5 + 1 赞一个
acme_pjz + 20 + 5 赞一个

查看全部评分

本帖被以下淘专辑推荐:

发表于 2017-4-6 18:48:16 | 显示全部楼层
好人啊····

点评

论坛元老  发表于 2017-4-6 18:52
回复 支持 1 反对 0

使用道具 举报

头像被屏蔽
发表于 2014-11-24 16:05:03 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2014-11-24 17:52:36 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

发表于 2014-11-24 20:17:33 | 显示全部楼层

小伙伴都说好,不能错过!
回复 支持 反对

使用道具 举报

发表于 2014-11-25 01:59:56 | 显示全部楼层
支持一个~
回复 支持 反对

使用道具 举报

发表于 2014-11-25 12:26:25 | 显示全部楼层

.net 有
回复 支持 反对

使用道具 举报

发表于 2014-11-25 18:12:24 | 显示全部楼层
henhao ,wo yao kankan
回复 支持 反对

使用道具 举报

发表于 2014-11-25 20:44:34 | 显示全部楼层
帅哥。。。嫁给我把。为啥你喜欢研究的东西,我也同样喜欢。

点评

发现一奇人..  发表于 2014-11-28 11:43
此楼喜欢断臂。。。。  发表于 2014-11-26 13:12
- -! 不好意思了。。 我只喜欢美女  发表于 2014-11-25 22:52
回复 支持 反对

使用道具 举报

发表于 2014-11-26 10:50:40 | 显示全部楼层
谢谢分享。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-27 18:44:24 | 显示全部楼层
本帖最后由 19900603 于 2014-11-27 18:45 编辑

增加了
Rem 备份数据库 table 为main 时 备份整个数据库
sqlite_BackUp_To_File
Rem 从物理文件拷贝一个表  CopyData 是否拷贝数据
sqlite_CopyTable_From_File

hSQLite 模块
测试代码
  1. Private Sub Command2_Click()
  2.     Dim db As Long
  3.     Dim ret As Long
  4.     Dim hstmt As Long
  5.     Dim pReturn As Long
  6.     rem 创建内存数据库(处理速度比物理数据库快很多)
  7.     Call sqlite_open(":memory:", db)
  8.    
  9.     MsgBox sqlite_CopyTable_From_File(db, "Role.SQLite", "Role")
  10.     Dim Script As String
  11.     rem 查询记录
  12.     Script = "select count(*) from Role"
  13.     pReturn = sqlite_prepare(db, Script, hstmt)
  14.     If pReturn = SQLITE_OK And sqlite_next(hstmt) Then
  15.         MsgBox sqlite_column_int(hstmt, 0)
  16.         sqlite_step hstmt
  17.         sqlite_finalize hstmt
  18.     End If
  19.     Rem 保存内存数据库到文件
  20.     MsgBox sqlite_BackUp_To_File(db, "Bak.Sqlite")
  21.     sqlite_close db
  22. End Sub
复制代码
  1. Option Explicit
  2.                                                                  
  3. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
  4. Public Declare Sub sqlite_free_table Lib "SQLite" (ByVal ResultPtr As String)
  5. Public Declare Sub sqlite_initialize Lib "SQLite" ()
  6. Public Declare Sub sqlite_interrupt Lib "SQLite" (ByVal hdb As Long)
  7. Public Declare Sub sqlite_free Lib "SQLite" (ptr As Any)
  8. Public Declare Function sqlite_open Lib "SQLite" (ByVal pwsFileName As String, ByRef hdb As Long) As Long ' PtrDb
  9. Public Declare Function sqlite_close Lib "SQLite" (ByVal hdb As Long) As Long
  10. Public Declare Function sqlite_finalize Lib "SQLite" (ByVal hstmt As Long) As Long
  11. Public Declare Function sqlite_prepare Lib "SQLite" (ByVal hdb As Long, ByVal pwsSql As String, ByRef hstmt As Long) As Long
  12. Public Declare Function sqlite_sleep Lib "SQLite" (ByVal Times As Long) As Long
  13. Public Declare Function sqlite_column_int Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  14. Public Declare Function sqlite_column_ptext Lib "SQLite" Alias "sqlite_column_text" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  15. Public Declare Function sqlite_step Lib "SQLite" (ByVal hstmt As Long) As Long
  16. Public Declare Function sqlite_exec Lib "SQLite" (ByVal hdb As Long, ByVal pwsSql As String) As Long
  17. Public Declare Function sqlite_get_table Lib "SQLite" (ByVal db As Long, ByVal zSql As String, ByRef pazResult As String, ByRef pnRow As Long, ByRef pnColumn As Long) As Long
  18. Public Declare Function sqlite_limit Lib "SQLite" (ByVal db As Long, ByVal Id As Long, ByVal NewVal As Long) As Long
  19. Public Declare Function sqlite_libversion Lib "SQLite" () As Long
  20. Public Declare Function sqlite_libversion_number Lib "SQLite" () As Long
  21. Public Declare Function sqlite_reset Lib "SQLite" (ByVal hstmt As Long) As Long
  22. Public Declare Function sqlite_errcode Lib "SQLite" (ByVal db As Long) As Long
  23. Public Declare Function sqlite_changes Lib "SQLite" (ByVal db As Long) As Long
  24. Public Declare Function sqlite_total_changes Lib "SQLite" (ByVal db As Long) As Long
  25. Public Declare Function sqlite_malloc Lib "SQLite" (ByVal nByte As Long) As Long
  26. Public Declare Function sqlite_errmsgchar Lib "SQLite" Alias "sqlite_errmsg" (ByVal db As Long) As Long '字符串指针
  27. Public Declare Function sqlite_column_database_pname Lib "SQLite" Alias "sqlite_column_database_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  28. Public Declare Function sqlite_column_table_pname Lib "SQLite" Alias "sqlite_column_table_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  29. Public Declare Function sqlite_column_origin_pname Lib "SQLite" Alias "sqlite_column_origin_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  30. Public Declare Function sqlite_column_declptype Lib "SQLite" Alias "sqlite_column_decltype" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  31. Public Declare Function sqlite_data_count Lib "SQLite" (ByVal hstmt As Long) As Long
  32. Rem 判断语句是否以分号结尾
  33. Public Declare Function sqlite_complete Lib "SQLite" (ByVal zSql As String) As Long
  34. Public Declare Function sqlite_column_count Lib "SQLite" (ByVal hstmt As Long) As Long
  35. Public Declare Function sqlite_column_ptype Lib "SQLite" Alias "sqlite_column_type" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  36. Public Declare Function sqlite_column_pname Lib "SQLite" Alias "sqlite_column_name" (ByVal hstmt As Long, ByVal iCol As Long) As Long '字符串指针
  37. Public Declare Function sqlite_column_blob Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  38. Public Declare Function sqlite_column_bytes Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  39. Public Declare Function sqlite_column_double Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Double
  40. Public Declare Function sqlite_column_value Lib "SQLite" (ByVal hstmt As Long, ByVal iCol As Long) As Long
  41. Public Declare Function sqlite_bind_parameter_count Lib "SQLite" (ByVal hstmt As Long) As Long
  42. Public Declare Function sqlite_bind_parameter_pname Lib "SQLite" Alias "sqlite_bind_parameter_name" (ByVal hstmt As Long, ByVal paramIndex As Long) As Long '字符串指针
  43. Public Declare Function sqlite_bind_parameter_index Lib "SQLite" (ByVal hstmt As Long, ByVal zName As String) As Long
  44. Public Declare Function sqlite_bind_null Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long) As Long
  45. Public Declare Function sqlite_bind_blob Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, Dataptr As Any, ByVal nByte As Long) As Long
  46. Public Declare Function sqlite_bind_zeroblob Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal nByte As Long) As Long
  47. Public Declare Function sqlite_bind_double Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal nByte As Double) As Long
  48. Public Declare Function sqlite_bind_int Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal nByte As Long) As Long
  49. Public Declare Function sqlite_bind_ptext Lib "SQLite" Alias "sqlite_bind_text" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal lpStr As String) As Long
  50. Public Declare Function sqlite_bind_value Lib "SQLite" (ByVal hstmt As Long, ByVal paramIndex As Long, ByVal NewVal As Long) As Long
  51. Public Declare Function sqlite_clear_bindings Lib "SQLite" (ByVal hstmt As Long) As Long
  52. Public Declare Function sqlite_last_insert_rowid Lib "SQLite" (ByVal hdb As Long) As Long
  53. Public Declare Function sqlite_backup_init Lib "SQLite" (ByVal pDestDC As Long, ByVal zDestName As String, ByVal pSourceDC As Long, ByVal zSourceName As String) As Long
  54. Public Declare Function sqlite_backup_step Lib "SQLite" (ByVal pBackup As Long, ByVal nPage As Long) As Long
  55. Public Declare Function sqlite_backup_finish Lib "SQLite" (ByVal pBackup As Long) As Long
  56. Public Declare Function sqlite_backup_remaining Lib "SQLite" (ByVal pBackup As Long) As Long
  57. Public Declare Function sqlite_backup_pagecount Lib "SQLite" (ByVal pBackup As Long) As Long
  58. Public Declare Function CopyStr Lib "kernel32" Alias "lstrcpynA" (ByVal lpStringDestination As String, ByVal lpStringSource As Long, ByVal lngMaxLength As Long) As Long
  59. Public Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
  60. Public Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
  61. Public Declare Function lstrcpynW Lib "kernel32" (ByVal pwsDest As Long, ByVal pwsSource As Long, ByVal cchCount As Long) As Long
  62. Public Declare Function lstrcpyW Lib "kernel32" (ByVal pwsDest As Long, ByVal pwsSource As Long) As Long
  63. Public Declare Function lstrlenW Lib "kernel32" (ByVal pwsString As Long) As Long
  64. Private Const CP_UTF8          As Long = 65001
  65. Enum SQLITE_RETURN
  66.     SQLITE_OK = 0
  67.     SQLITE_ERROR = 1
  68.     SQLITE_INTERNAL = 2
  69.     SQLITE_PERM = 3
  70.     SQLITE_ABORT = 4
  71.     SQLITE_BUSY = 5
  72.     SQLITE_LOCKED = 6
  73.     SQLITE_NOMEM = 7
  74.     SQLITE_READONLY = 8                                                         ' Attempt to write a readonly database
  75.     sqlite_interruptD = 9                                                       ' Operation terminated by sqlite3_interrupt()
  76.     SQLITE_IOERR = 10                                                           ' Some kind of disk I/O error occurred
  77.     SQLITE_CORRUPT = 11                                                         ' The database disk image is malformed
  78.     SQLITE_NOTFOUND = 12                                                        ' (Internal Only) Table or record not found
  79.     SQLITE_FULL = 13                                                            ' Insertion failed because database is full
  80.     SQLITE_CANTOPEN = 14                                                        ' Unable to open the database file
  81.     SQLITE_PROTOCOL = 15                                                        ' Database lock protocol error
  82.     SQLITE_EMPTY = 16                                                           ' Database is empty
  83.     SQLITE_SCHEMA = 17                                                          ' The database schema changed
  84.     SQLITE_TOOBIG = 18                                                          ' Too much data for one row of a table
  85.     SQLITE_CONSTRAINT = 19                                                      ' Abort due to contraint violation
  86.     SQLITE_MISMATCH = 20                                                        ' Data type mismatch
  87.     SQLITE_MISUSE = 21                                                          ' Library used incorrectly
  88.     SQLITE_NOLFS = 22                                                           ' Uses OS features not supported on host
  89.     SQLITE_AUTH = 23                                                            ' Authorization denied
  90.     SQLITE_FORMAT = 24                                                          ' Auxiliary database format error
  91.     SQLITE_RANGE = 25                                                           ' 2nd parameter to sqlite3_bind out of range
  92.     SQLITE_NOTADB = 26                                                          ' File opened that is not a database file
  93.     SQLITE_ROW = 100                                                            ' sqlite3_step() has another row ready
  94.     SQLITE_DONE = 101                                                           ' sqlite3_step() has finished executing
  95. End Enum
  96. Public Const SQLITE_TRANSIENT  As Long = 0

  97. Public Function UTF8StringFromPtr(ByVal pUtf8String As Long) As String
  98.     Dim cSize As Long
  99.     UTF8StringFromPtr = ""
  100.     cSize = MultiByteToWideChar(CP_UTF8, 0, pUtf8String, -1, 0, 0)
  101.     If cSize > 1 Then
  102.         UTF8StringFromPtr = Space(cSize - 1)                                    'String(cSize - 1, " ")
  103.         MultiByteToWideChar CP_UTF8, 0, pUtf8String, -1, StrPtr(UTF8StringFromPtr), cSize
  104.     End If
  105. End Function
  106.                                                                     
  107. Public Function sqlite_bind_text(hstmt As Long, iCol As Long, lpStr As String) As Long
  108.     sqlite_bind_text = sqlite_bind_ptext(hstmt, iCol, StrConv(lpStr, vbUnicode))
  109. End Function
  110.                                                                     
  111. Private Function BytesFromPtr(ByVal lAddr As Long, ByVal lSize As Long) As Byte()
  112.     ReDim bvData(lSize - 1) As Byte
  113.     CopyMemory bvData(0), ByVal lAddr, lSize
  114.     BytesFromPtr = bvData
  115. End Function
  116.                                                                     
  117. Public Function SQLite_Vacuum(h_DB As Long) As Boolean
  118.     Dim hstmt As Long, lpReturn As SQLITE_RETURN
  119.     lpReturn = sqlite_exec(h_DB, "VACUUM")
  120.     SQLite_Vacuum = (lpReturn = SQLITE_OK)
  121. End Function
  122.                                                                     
  123. Public Function sqlite3_column_name(hstmt As Long, iCol As Long) As String
  124.     Dim lpReturn As Long
  125.     lpReturn = sqlite_column_name(hstmt, iCol)
  126.     sqlite3_column_name = UTF8StringFromPtr(lpReturn)
  127. End Function
  128.                                                                     
  129. Public Function sqlite_column_text(hstmt As Long, iCol As Long) As String
  130.     Dim lpReturn As Long
  131.     lpReturn = sqlite_column_ptext(hstmt, iCol)
  132.     sqlite_column_text = UTF8StringFromPtr(lpReturn)
  133. End Function
  134.                                                                     
  135. Public Function sqlite_column_database_name(hstmt As Long, iCol As Long) As String
  136.     Dim lpReturn As Long
  137.     lpReturn = sqlite_column_database_pname(hstmt, iCol)
  138.     sqlite_column_database_name = UTF8StringFromPtr(lpReturn)
  139. End Function
  140.                                                                     
  141. Public Function sqlite_column_table_name(hstmt As Long, iCol As Long) As String
  142.     Dim lpReturn As Long
  143.     lpReturn = sqlite_column_table_pname(hstmt, iCol)
  144.     sqlite_column_table_name = UTF8StringFromPtr(lpReturn)
  145. End Function
  146.                                                                     
  147. Public Function sqlite_column_origin_name(hstmt As Long, iCol As Long) As String
  148.     Dim lpReturn As Long
  149.     lpReturn = sqlite_column_origin_pname(hstmt, iCol)
  150.     sqlite_column_origin_name = UTF8StringFromPtr(lpReturn)
  151. End Function
  152.                                                                     
  153. Public Function sqlite_column_decltype(hstmt As Long, iCol As Long) As String
  154.     Dim lpReturn As Long
  155.     lpReturn = sqlite_column_declptype(hstmt, iCol)
  156.     sqlite_column_decltype = UTF8StringFromPtr(lpReturn)
  157. End Function
  158.                                                                     
  159. Public Function sqlite_errmsg(hdb As Long) As String
  160.     Dim lpReturn As Long
  161.     lpReturn = sqlite_errmsgchar(hdb)
  162.     sqlite_errmsg = UTF8StringFromPtr(lpReturn)
  163. End Function
  164.                                                                     
  165. Public Function sqlite_column_type(hstmt As Long, iCol As Long) As String
  166.     Dim lpReturn As Long
  167.     lpReturn = sqlite_column_ptype(hstmt, iCol)
  168.     sqlite_column_type = UTF8StringFromPtr(lpReturn)
  169. End Function
  170.                                                                     
  171. Public Function sqlite_column_name(hstmt As Long, iCol As Long) As String
  172.     Dim lpReturn As Long
  173.     lpReturn = sqlite_column_pname(hstmt, iCol)
  174.     sqlite_column_name = UTF8StringFromPtr(lpReturn)
  175. End Function
  176.                                                                     
  177. Public Function sqlite_bind_parameter_name(hstmt As Long, paramIndex As Long) As String
  178.     Dim lpReturn As Long
  179.     lpReturn = sqlite_bind_parameter_pname(hstmt, paramIndex)
  180.     sqlite_bind_parameter_name = UTF8StringFromPtr(lpReturn)
  181. End Function
  182.                                                                     
  183. Rem 是否跳转到下一条记录
  184. Public Function sqlite_next(hstmt As Long) As Boolean
  185.     sqlite_next = (sqlite_step(hstmt) = SQLITE_ROW)
  186. End Function
  187.                                                                     
  188. Rem 备份数据库 table 为main 时 备份整个数据库
  189. Public Function sqlite_BackUp_To_File(hdb As Long, FileName As String, Optional table As String = "main") As Boolean
  190.     Dim db As Long
  191.     Dim backup As Long
  192.     Call sqlite_open(FileName, db)
  193.     backup = sqlite_backup_init(db, table, hdb, table)
  194.     If backup Then
  195.         sqlite_backup_step backup, -1
  196.         sqlite_backup_finish backup
  197.         sqlite_BackUp_To_File = True
  198.     End If
  199.     sqlite_close db
  200. End Function
  201.                                                                     
  202. Rem 从物理文件拷贝一个表  CopyData 是否拷贝数据
  203. Public Function sqlite_CopyTable_From_File(db As Long, FileName As String, table As String, Optional CopyData As Boolean = True) As Boolean
  204.     Dim pReturn As SQLITE_RETURN
  205.     Dim Script As String
  206.     Rem ATTACH Role.SQLite 到内存 命名为RoleDB
  207.     pReturn = sqlite_exec(db, "ATTACH DATABASE '" & FileName & "' AS CopyDB")
  208.     If pReturn = SQLITE_OK Then
  209.         Rem 创建脚本
  210.         Script = "CREATE TABLE " & table & " AS SELECT * FROM CopyDB." & table
  211.         Rem 是否复制表内数据
  212.         If Not CopyData Then Script = Script & " WHERE 1=0"
  213.         Rem 执行SQL
  214.         pReturn = sqlite_exec(db, Script)
  215.         If pReturn = SQLITE_OK Then
  216.             Rem 分离数据库
  217.             pReturn = sqlite_exec(db, "DETACH DATABASE CopyDB")
  218.             If pReturn = SQLITE_OK Then sqlite_CopyTable_From_File = True
  219.         End If
  220.     End If
  221. End Function
  222.                        
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-5-29 03:04

VB爱好者乐园(VBGood)
快速回复 返回顶部 返回列表