Advertisement
hyerob

sqlite3_class_sqlitedb_example.ahk

Apr 2nd, 2019
1,577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;**************
  2. ; REQUIREMENTS*
  3. ;**************
  4.  
  5. ; AutoHotkey Version 2 (AutoHotkey_2.0-a100-52515e2)
  6. ; https://www.autohotkey.com/download/2.0/AutoHotkey_2.0-a100-52515e2.zip
  7. ; sqlite3.dll located in A_ScriptDir (i.e., the directory that contains this .ahk file)
  8. ; https://sqlite.org/2019/sqlite-dll-win32-x86-3270200.zip (32-bit)
  9. ; Or
  10. ; https://sqlite.org/2019/sqlite-dll-win64-x64-3270200.zip (64-bit)
  11. ; Class_SQLiteDB.ahk located in A_ScriptDir
  12. ; https://pastebin.com/mJ4gFVCv
  13. ; Chinese-English glossary file (excerpt from CC-CEDICT) in A_ScriptDir
  14. ; https://pastebin.com/BS3KnzRL
  15. ; Official link to complete CC-CEDICT
  16. ; https://cc-cedict.org/editor/editor_export_cedict.php?c=zip
  17.  
  18. ;*************
  19. ; DISCLAIMER:*
  20. ;*************
  21.  
  22. ; This software is provided 'as-is', without any express or implied warranty.
  23. ; In no event will the authors be held liable for any damages arising from the
  24. ; use of this software.
  25.  
  26. ;************************
  27. ; INCLUDE SQLITE WRAPPER*
  28. ;************************
  29.  
  30. #Include Class_SQLiteDB.ahk
  31.  
  32. ;*******************************************
  33. ; SPECIFY NAME FOR DATABASE AND DATA SOURCE*
  34. ;*******************************************
  35.  
  36. db_file := A_ScriptDir . "\ce_glossary.sqlite"
  37. ;source_files := A_ScriptDir . "\chinese_english\*.txt"
  38. source_files := A_ScriptDir . "\CC-CEDICT.txt"
  39.  
  40. ;***********************************************************************************
  41. ; READ IN DATA (COULD BE MEMORY INTENSIVE STORING ALL ENTRIES IN ASSOCIATIVE ARRAY)*
  42. ;***********************************************************************************
  43.  
  44. dict := {} ; use associative array to remove duplicates
  45. FileEncoding("UTF-8")
  46. Loop Files, source_files
  47. {
  48.     Loop Read, A_LoopFileFullPath
  49.     {
  50.         dict[A_LoopReadLine] := 1
  51.     }
  52. }
  53.  
  54. ;***********************************
  55. ; OPEN DATABASE OR CREATE A NEW ONE*
  56. ;***********************************
  57.  
  58. db := New SQLiteDb
  59. If !db.OpenDb(db_file)
  60. {
  61.     MsgBox("SQLite Error OpenDb`n`nMessage: " . db.ErrorMsg . "`nCode: " . db.ErrorCode . "`nFile: " . db_file)
  62.     ExitApp()
  63. }
  64.  
  65. ;***********************
  66. ; CREATE DATABASE TABLE*
  67. ;***********************
  68.  
  69. sql_cmd := "CREATE TABLE glossary (id INTEGER PRIMARY KEY, chinese TEXT, english TEXT)"
  70. If !db.Exec(sql_cmd)
  71. {
  72.     MsgBox("SQLite CREATE Error`n`nMessage: " . db.ErrorMsg . "`nCode: " . db.ErrorCode . "`nQuery: " . sql_cmd)
  73.     ExitApp()
  74. }
  75.  
  76. ;**********************************************************
  77. ; CREATE INSERT STATEMENT (COULD ALSO BE MEMORY INTENSIVE)*
  78. ;**********************************************************
  79.  
  80. insert_query := "INSERT INTO glossary (chinese, english) VALUES"
  81. For key, value In dict
  82. {
  83.     c_e := StrSplit(key, A_Tab,, 2)
  84.     insert_query .= '("' . c_e[1] . '", "' . c_e[2] . '"),'
  85. }
  86. insert_query := RegExReplace(insert_query, ',$', ';') ; replace trailing comma with semicolon
  87.  
  88. ;**************************
  89. ; EXECUTE INSERT STATEMENT*
  90. ;**************************
  91.  
  92. sql_cmd := insert_query
  93. result := db.Exec(sql_cmd)
  94. If (result)
  95. {
  96.     MsgBox("Inserted " . db.Changes . " rows.")
  97. }
  98. Else
  99. {
  100.     MsgBox("SQLite Insert Error`n`nMessage: " . db.ErrorMsg . "`nCode: " . db.ErrorCode . "`nQuery: " . sql_cmd)
  101.     ExitApp()
  102. }
  103.  
  104. ;*****************************
  105. ; MAKE A QUERY (FUZZY SEARCH)*
  106. ;*****************************
  107.  
  108. ;search_query := Clipboard ; could set up a hotkey and then grab search query from clipboard
  109. search_query := '職業'
  110. query := "SELECT chinese, english FROM glossary WHERE chinese LIKE '%" . search_query . "%'"
  111. sql_cmd := query
  112. If !db.Query(sql_cmd, query_result)
  113. {
  114.     MsgBox("SQLite QUERY Error`n`nMessage: " . db.ErrorMsg . "`nCode: " . db.ErrorCode . "`nFile: " . db_file . "`nQuery: " . sql_cmd)
  115.     ExitApp
  116. }
  117. search_result := ''
  118. Loop
  119. {
  120.     result := query_result.Next(row)
  121.     If !result
  122.     {
  123.         MsgBox("SQLite NEXT Error`n`nMessage: " . db.ErrorMsg . "`nCode: " . db.ErrorCode)
  124.         ExitApp
  125.     }
  126.     If result = -1
  127.     {
  128.         Break
  129.     }
  130.     Loop query_result.ColumnCount
  131.     {
  132.         search_result .= row[A_Index] . A_Tab
  133.     }
  134.     search_result .= '`n'
  135. }
  136. query_result.Free()
  137. MsgBox(search_result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement