⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlite3inifiledb.pas

📁 定时器for timer for ic chip
💻 PAS
📖 第 1 页 / 共 2 页
字号:
Begin
  If Not Assigned(FStmtInsertSection) Then
    FStmtInsertSection := Prepare(Sql_Insert_Section);
  Try
    Check(SQLite3_Bind_Str16(FStmtInsertSection, 1, ASection));
    Check(Sqlite3_Step(FStmtInsertSection));
    Result := SQLite3_Last_Insert_RowID(FDb);
  Finally
    SQLite3_Reset(FStmtInsertSection);
  End;
End;

Function TSQLite3IniFileDb.KeyExists(Const ASection, AKey: WideString): Boolean;
Begin
  Result := SelectKeyID(SelectSectionID(ASection), AKey) > 0;
End;

Function TSQLite3IniFileDb.Prepare(Const ASql8: Utf8String): TSQLiteStmt;
Var
  NextSQLStatement: PAnsiChar;
Begin
  Check(sqlite3_prepare_v2(FDb, PAnsiChar(ASql8), Length(ASql8), Result, NextSQLStatement));
End;

Procedure TSQLite3IniFileDb.RaiseException(Const AErrorCode: Integer);
Begin
  {$IFDEF COMPILER_5_UP}
  Raise ESQLite3IniFileDb.CreateResFmt(PResStringRec(@SSQLite3IniFileDbError), [AErrorCode, sqlite3_errmsg(FDb)])
  {$ELSE}
  Raise ESQLite3IniFileDb.CreateFmt(SSQLite3IniFileDbError, [AErrorCode, sqlite3_errmsg(FDb)])
  {$ENDIF}
End;

Function TSQLite3IniFileDb.ReadInteger(Const ASection, AKey: WideString; Const ADefault: Integer = 0): Integer;
Begin
  Result := ADefault;
  If Not Assigned(FStmtSelectValue) Then
    FStmtSelectValue := Prepare(Sql_Select_Value);
  Try
    Check(SQLite3_Bind_Str16(FStmtSelectValue, 1, ASection));
    Check(SQLite3_Bind_Str16(FStmtSelectValue, 2, AKey));
    If Check(Sqlite3_Step(FStmtSelectValue)) = SQLITE_ROW Then
      Result := Sqlite3_Column_Int(FStmtSelectValue, 0);
  Finally
    SQLite3_Reset(FStmtSelectValue);
  End;
End;

Procedure TSQLite3IniFileDb.ReadSection(Const ASection: WideString; AStrings: TStrings);
Const
  SQL = 'SELECT KeyName FROM Keys NATURAL JOIN Sections WHERE SectionName=?;';
Begin
  AStrings.BeginUpdate;
  Try
    If Not Assigned(FStmtReadSection) Then
      FStmtReadSection := Prepare(SQL);
    Try
      Check(SQLite3_Bind_Str16(FStmtReadSection, 1, ASection));
      While Check(Sqlite3_Step(FStmtReadSection)) = SQLITE_ROW Do
        AStrings.Add(Sqlite3_Column_Str16(FStmtReadSection, 0));
    Finally
      SQLite3_Reset(FStmtReadSection);
    End;
  Finally
    AStrings.EndUpdate;
  End;
End;

Procedure TSQLite3IniFileDb.ReadSections(Const AStrings: TStrings);
Const
  SQL = 'SELECT SectionName FROM Sections;';
Begin
  AStrings.BeginUpdate;
  Try
    If Not Assigned(FStmtReadSections) Then
      FStmtReadSections := Prepare(SQL);
    Try
      While Check(Sqlite3_Step(FStmtReadSections)) = SQLITE_ROW Do
        AStrings.Add(Sqlite3_Column_Str16(FStmtReadSections, 0));
    Finally
      SQLite3_Reset(FStmtReadSections);
    End;
  Finally
    AStrings.EndUpdate;
  End;
End;

Procedure TSQLite3IniFileDb.ReadSectionValues(Const ASection: WideString; AStrings: TStrings);
Const
  SQL = 'SELECT KeyName, KeyValue FROM Keys NATURAL JOIN Sections WHERE SectionName=?;';
Begin
  AStrings.BeginUpdate;
  Try
    If Not Assigned(FStmtReadSectionValues) Then
      FStmtReadSectionValues := Prepare(SQL);
    Try
      Check(SQLite3_Bind_Str16(FStmtReadSectionValues, 1, ASection));
      While Check(Sqlite3_Step(FStmtReadSectionValues)) = SQLITE_ROW Do
        AStrings.Add(Sqlite3_Column_Str16(FStmtReadSectionValues, 0) + '=' + Sqlite3_Column_Str16(FStmtReadSectionValues, 1));
    Finally
      SQLite3_Reset(FStmtReadSectionValues);
    End;
  Finally
    AStrings.EndUpdate;
  End;
End;

Function TSQLite3IniFileDb.ReadString(Const ASection, AKey, ADefault: WideString): WideString;
Begin
  Result := ADefault;
  If Not Assigned(FStmtSelectValue) Then
    FStmtSelectValue := Prepare(Sql_Select_Value);
  Try
    Check(SQLite3_Bind_Str16(FStmtSelectValue, 1, ASection));
    Check(SQLite3_Bind_Str16(FStmtSelectValue, 2, AKey));
    If Check(Sqlite3_Step(FStmtSelectValue)) = SQLITE_ROW Then
      Result := Sqlite3_Column_Str16(FStmtSelectValue, 0);
  Finally
    SQLite3_Reset(FStmtSelectValue);
  End;
End;

Function TSQLite3IniFileDb.ReadStringList(
  Const ASection: WideString;
  Const AStringList: TStrings;
  Const AClearFirst: Boolean = True;
  Const AItemName: WideString = DEFAULT_LIST_ITEM_NAME): Integer;
Var
  i: Integer;
Begin
  AStringList.BeginUpdate;
  Try
    If AClearFirst Then
      AStringList.Clear;
    Result := ReadInteger(ASection, AItemName + LIST_COUNT_SUFFIX, 0);
    For i := 0 To Result - 1 Do
      AStringList.Add(ReadString(ASection, AItemName + IntToStr(i)));
  Finally
    AStringList.EndUpdate;
  End;
End;

Function TSQLite3IniFileDb.SectionExists(Const ASection: WideString): Boolean;
Begin
  Result := SelectSectionID(ASection) > 0;
End;

Function TSQLite3IniFileDb.SelectKeyID(Const ASectionID: Int64; Const AKey: WideString): Int64;
Const
  Sql_Select_KeyID = 'SELECT KeyID FROM Keys WHERE SectionID=? AND KeyName=?;';
Begin
  If Not Assigned(FStmtSelectKeyID) Then
    FStmtSelectKeyID := Prepare(Sql_Select_KeyID);
  Try
    Check(SQLite3_Bind_Int64(FStmtSelectKeyID, 1, ASectionID));
    Check(SQLite3_Bind_Str16(FStmtSelectKeyID, 2, AKey));
    If Check(Sqlite3_Step(FStmtSelectKeyID)) = SQLITE_ROW Then
      Result := Sqlite3_Column_Int64(FStmtSelectKeyID, 0)
    Else
      Result := -1;
  Finally
    SQLite3_Reset(FStmtSelectKeyID);
  End;
End;

Function TSQLite3IniFileDb.SelectSectionID(Const ASection: WideString): Int64;
Const
  Sql_Select_SectionID = 'SELECT SectionID FROM Sections WHERE SectionName=?;';
Begin
  If Not Assigned(FStmtSelectSectionID) Then
    FStmtSelectSectionID := Prepare(Sql_Select_SectionID);
  Try
    Check(SQLite3_Bind_Str16(FStmtSelectSectionID, 1, ASection));
    If Check(Sqlite3_Step(FStmtSelectSectionID)) = SQLITE_ROW Then
      Result := Sqlite3_Column_Int64(FStmtSelectSectionID, 0)
    Else
      Result := -1;
  Finally
    SQLite3_Reset(FStmtSelectSectionID);
  End;
End;

Procedure TSQLite3IniFileDb.WriteInteger(Const ASection, AKey: WideString; Const AValue: Integer);
Var
  KeyID: Int64;
  SectionID: Int64;
Begin
  SectionID := SelectSectionID(ASection);
  If SectionID < 0 Then
    SectionID := InsertSection(ASection);
  If SectionID >= 0 Then
  Begin
    KeyID := SelectKeyID(SectionID, AKey);
    If KeyID >= 0 Then
    Begin

      If Not Assigned(FStmtUpdateKey) Then
        FStmtUpdateKey := Prepare(Sql_Update_Key);
      Try
        Check(SQLite3_Bind_Int(FStmtUpdateKey, 1, AValue));
        Check(SQLite3_Bind_Int64(FStmtUpdateKey, 2, KeyID));
        Check(Sqlite3_Step(FStmtUpdateKey))
      Finally
        SQLite3_Reset(FStmtUpdateKey);
      End;
    End
    Else
    Begin

      If Not Assigned(FStmtInsertKey) Then
        FStmtInsertKey := Prepare(Sql_Insert_Key);
      Try
        Check(SQLite3_Bind_Int64(FStmtInsertKey, 1, SectionID));
        Check(SQLite3_Bind_Str16(FStmtInsertKey, 2, AKey));
        Check(SQLite3_Bind_Int(FStmtInsertKey, 3, AValue));
        Check(Sqlite3_Step(FStmtInsertKey))
      Finally
        SQLite3_Reset(FStmtInsertKey);
      End;
    End;
  End;
End;

Procedure TSQLite3IniFileDb.WriteString(Const ASection, AKey, AValue: WideString);
Var
  KeyID: Int64;
  SectionID: Int64;
Begin
  SectionID := SelectSectionID(ASection);
  If SectionID < 0 Then
    SectionID := InsertSection(ASection);
  If SectionID >= 0 Then
  Begin
    KeyID := SelectKeyID(SectionID, AKey);
    If KeyID >= 0 Then
    Begin

      If Not Assigned(FStmtUpdateKey) Then
        FStmtUpdateKey := Prepare(Sql_Update_Key);
      Try
        Check(SQLite3_Bind_Str16(FStmtUpdateKey, 1, AValue));
        Check(SQLite3_Bind_Int64(FStmtUpdateKey, 2, KeyID));
        Check(Sqlite3_Step(FStmtUpdateKey))
      Finally
        SQLite3_Reset(FStmtUpdateKey);
      End;
    End
    Else
    Begin

      If Not Assigned(FStmtInsertKey) Then
        FStmtInsertKey := Prepare(Sql_Insert_Key);
      Try
        Check(SQLite3_Bind_Int64(FStmtInsertKey, 1, SectionID));
        Check(SQLite3_Bind_Str16(FStmtInsertKey, 2, AKey));
        Check(SQLite3_Bind_Str16(FStmtInsertKey, 3, AValue));
        Check(Sqlite3_Step(FStmtInsertKey))
      Finally
        SQLite3_Reset(FStmtInsertKey);
      End;
    End;
  End;
End;

Procedure TSQLite3IniFileDb.WriteStringList(
  Const ASection: WideString;
  Const AStringList: TStrings;
  Const AItemName: WideString = DEFAULT_LIST_ITEM_NAME);
Var
  i: Integer;
  PrevListCount: Integer;
Begin
  PrevListCount := ReadInteger(ASection, AItemName + LIST_COUNT_SUFFIX, 0);
  WriteInteger(ASection, AItemName + LIST_COUNT_SUFFIX, AStringList.Count);
  For i := 0 To AStringList.Count - 1 Do
    WriteString(ASection, AItemName + IntToStr(i), AStringList[i]);
  For i := AStringList.Count To PrevListCount - 1 Do
    DeleteKey(ASection, AItemName + IntToStr(i));
End;

End.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -