归属地快速查询SQLite_Ahk

#NoEnv
;初始化连接数据库 以便反复查询
DBFileName := A_ScriptDir . "areacode.db"
DB := new SQLiteDB
If !DB.OpenDB(DBFileName) {
   MsgBox, 16, SQLite错误, % "消息:`t" . DB.ErrorMsg . "`n代码:`t" . DB.ErrorCode
   ExitApp
}

;示例
start := A_TickCount
MsgBox % "号码:01012345678 归属地:"get_area("01012345678") "查询耗时:" A_TickCount - start "毫秒"
MsgBox % get_area("95533")
MsgBox % get_area("02340820000")
MsgBox % c := get_area("13800138000")
ExitApp

get_area(phone){
global DB ;全局
;去国家代码
phone := RegExReplace(phone,"^+86","")
phone := RegExReplace(phone,"^0086","")
;去长途代码
phone := RegExReplace(phone,"^0","")
;组织查询语句
SQL := "SELECT * FROM area where code='" SubStr(phone,1,7) "' or code='" SubStr(phone,1,6) "' or code='" SubStr(phone,1,5) "' or code='" SubStr(phone,1,4) "' or code='" SubStr(phone,1,3) "' or code='" SubStr(phone,1,2) "'"
If !DB.GetTable(SQL, Result)
   MsgBox, 16, SQLite错误: 获取结果, % "消息:`t" . DB.ErrorMsg . "`n代码:`t" . DB.ErrorCode
Result.Next(Row)
If (Result.HasRows) {
   Return Row[2] "-" Row[3]
}
}

Class SQLiteDB Extends SQLiteDB.BaseClass {
   ;http://ahkscript.org/boards/viewtopic.php?f=6&t=1064&sid=91f4222620265f42c43b55625450943d
   ;https://github.com/AHK-just-me/Class_SQLiteDB/tree/master/Sources_v1.1
   Class BaseClass {
      Static Version := ""
      Static _SQLiteDLL := A_ScriptDir . "SQLite3.dll"
      Static _RefCount := 0
      Static _MinVersion := 36
   }
   Class _Table {
      __New() {
          This.ColumnCount := 0
          This.RowCount := 0
          This.ColumnNames := []
          This.Rows := []
          This.HasNames := False
          This.HasRows := False
          This._CurrentRow := 0
      }
      GetRow(RowIndex, ByRef Row) {
         Row := ""
         If (RowIndex  This.RowCount)
            Return False
         If !This.Rows.HasKey(RowIndex)
            Return False
         Row := This.Rows[RowIndex]
         This._CurrentRow := RowIndex
         Return True
      }
      Next(ByRef Row) {
         Row := ""
         If (This._CurrentRow >= This.RowCount)
            Return -1
         This._CurrentRow += 1
         If !This.Rows.HasKey(This._CurrentRow)
            Return False
         Row := This.Rows[This._CurrentRow]
         Return True
      }
      Reset() {
         This._CurrentRow := 0
         Return True
      }
   }
   Class _RecordSet {
      __New() {
          This.ColumnCount := 0
          This.ColumnNames := []
          This.HasNames := False
          This.HasRows := False
          This.CurrentRow := 0
          This.ErrorMsg := ""
          This.ErrorCode := 0
          This._Handle := 0
          This._DB := {}
      }
      Next(ByRef Row) {
         Static SQLITE_NULL := 5
         Static SQLITE_BLOB := 4
         Static EOR := -1
         Row := ""
         This.ErrorMsg := ""
         This.ErrorCode := 0
         If !(This._Handle) {
            This.ErrorMsg := "Invalid query handle!"
            Return False
         }
         RC := DllCall("SQlite3.dllsqlite3_step", "Ptr", This._Handle, "Cdecl Int")
         If (ErrorLevel) {
            This.ErrorMsg := "DLLCall sqlite3_step failed!"
            This.ErrorCode := ErrorLevel
            Return False
         }
         If (RC  This._DB._ReturnCode("SQLITE_ROW")) {
            If (RC = This._DB._ReturnCode("SQLITE_DONE")) {
               This.ErrorMsg := "EOR"
               This.ErrorCode := RC
               Return EOR
            }
            This.ErrorMsg := This._DB.ErrMsg()
            This.ErrorCode := RC
            Return False
         }
         RC := DllCall("SQlite3.dllsqlite3_data_count", "Ptr", This._Handle, "Cdecl Int")
         If (ErrorLevel) {
            This.ErrorMsg := "DLLCall sqlite3_data_count failed!"
            This.ErrorCode := ErrorLevel
            Return False
         }
         If (RC  "W") && (Access  "R")
         Access := "R"
      Flags := SQLITE_OPEN_READONLY
      If (Access = "W") {
         Flags := SQLITE_OPEN_READWRITE
         If (Create)
            Flags |= SQLITE_OPEN_CREATE
      }
      This._Path := DBPath
      UTF8 := This._StrToUTF8(DBPath)
      RC := DllCall("SQlite3.dllsqlite3_open_v2", "Ptr", &UTF8, "PtrP", HDB, "Int", Flags, "Ptr", 0, "Cdecl Int")
      If (ErrorLevel) {
         This._Path := ""
         This.ErrorMsg := "DLLCall sqlite3_open_v2 failed!"
         This.ErrorCode := ErrorLevel
         Return False
      }
      If (RC) {
         This._Path := ""
         This.ErrorMsg := This._ErrMsg()
         This.ErrorCode := RC
         Return False
      }
      This._Handle := HDB
      Return True
   }
   CloseDB() {
      This.ErrorMsg := ""
      This.ErrorCode := 0
      This.SQL := ""
      If !(This._Handle)
         Return True
      For Each, Query in This._Queries
         DllCall("SQlite3.dllsqlite3_finalize", "Ptr", Query, "Cdecl Int")
      RC := DllCall("SQlite3.dllsqlite3_close", "Ptr", This._Handle, "Cdecl Int")
      If (ErrorLevel) {
         This.ErrorMsg := "DLLCall sqlite3_close failed!"
         This.ErrorCode := ErrorLevel
         Return False
      }
      If (RC) {
         This.ErrorMsg := This._ErrMsg()
         This.ErrorCode := RC
         Return False
      }
      This._Path := ""
      This._Handle := ""
      This._Queries := []
      Return True
   }
   AttachDB(DBPath, DBAlias) {
      Return This.Exec("ATTACH DATABASE '" . DBPath . "' As " . DBAlias . ";")
   }
   DetachDB(DBAlias) {
      Return This.Exec("DETACH DATABASE " . DBAlias . ";")
   }

   Exec(SQL, Callback = "") {
      This.ErrorMsg := ""
      This.ErrorCode := 0
      This.SQL := SQL
      If !(This._Handle) {
         This.ErrorMsg := "Invalid dadabase handle!"
         Return False
      }
      CBPtr := 0
      Err := 0
      If (FO := Func(Callback)) && (FO.MinParams = 4)
         CBPtr := RegisterCallback(Callback, "F C", 4, &SQL)
      UTF8 := This._StrToUTF8(SQL)
      RC := DllCall("SQlite3.dllsqlite3_exec", "Ptr", This._Handle, "Ptr", &UTF8, "Int", CBPtr, "Ptr", Object(This)
                  , "PtrP", Err, "Cdecl Int")
      CallError := ErrorLevel
      If (CBPtr)
         DllCall("Kernel32.dllGlobalFree", "Ptr", CBPtr)
      If (CallError) {
         This.ErrorMsg := "DLLCall sqlite3_exec failed!"
         This.ErrorCode := CallError
         Return False
      }
      If (RC) {
         This.ErrorMsg := StrGet(Err, "UTF-8")
         This.ErrorCode := RC
         DllCall("SQLite3.dllsqlite3_free", "Ptr", Err, "Cdecl")
         Return False
      }
      This.Changes := This._Changes()
      Return True
   }
   GetTable(SQL, ByRef TB, MaxResult = 0) {
      TB := ""
      This.ErrorMsg := ""
      This.ErrorCode := 0
      This.SQL := SQL
      If !(This._Handle) {
         This.ErrorMsg := "Invalid dadabase handle!"
         Return False
      }
      If !RegExMatch(SQL, "i)^s*SELECTs") {
         This.ErrorMsg := "Method " . A_ThisFunc . " requires a SELECT-Statement!"
         Return False
      }
      Names := ""
      Err := 0, RC := 0, GetRows := 0
      I := 0, Rows := Cols := 0
      Table := 0
      If MaxResult Is Not Integer
         MaxResult := 0
      If (MaxResult  0) && (MaxResult  This._ReturnCode("SQLITE_DONE")) {
         This.ErrorMsg := A_ThisFunc . ": " . This._ErrMsg()
         This.ErrorCode := RC
         Return False
      }
      RC := DllCall("SQlite3.dllsqlite3_finalize", "Ptr", Query, "Cdecl Int")
      If (ErrorLevel) {
         This.ErrorMsg := A_ThisFunc . ": DLLCall sqlite3_finalize failed!"
         This.ErrorCode := ErrorLevel
         Return False
      }
      If (RC) {
         This.ErrorMsg := A_ThisFunc . ": " . This._ErrMsg()
         This.ErrorCode := RC
         Return False
      }
      Return True
   }
}

给TA捐赠
共{{data.count}}人
人已捐赠
其他

开机时间

2020-3-9 2:27:44

其他

当前窗口的路径

2020-3-9 2:29:44

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索