【问题标题】:access vba get cyrillic text from ini file into database访问vba从ini文件中获取西里尔文字到数据库
【发布时间】:2012-08-03 07:04:48
【问题描述】:

2012/08/31 : 更新了我的帖子

在整个网络上搜索它,找到了一些片段,但没有任何帮助,所以我求助于你。


环境信息:

编程语言是 VBA / Access 2003

将从现有的“.ini”文件中读取数据

数据应插入Access数据库


现在我的问题:

我有一个 ini 文件,其中包含 ini 文件中的信息。该文件如下所示:

[product_details]
product_description=my product description
product_name=my product
product_price=11.0
product_sku=myproduct2012

这些信息保存在“products.ini”中,当在记事本或记事本++中打开时,它会正确显示并可以插入到我的访问数据库中,我可以在我的表单中显示这些信息

但现在有人想要这样的东西:

[product_details]
product_description=мое описание продукта
product_name=мой продукт
product_price=11.0
product_sku=произведение2012

当通过 GetINIValue 加载这些信息时,值将作为不可读的文本保存到数据库中。

编辑:在记事本/记事本++中也显示正确,因此西里尔字符被正确传输到ini文件中

我确实尝试了很多东西(使用 UNICODE 版本的 GetINIValue,获取字符代码等,检查西里尔文文本)没有任何帮助。

它应该做什么: 无论是什么语言,我都需要帮助才能从这个 ini 条目中获取值(在这种情况下,英语、德语、法语、俄语就足够了)

希望有人可以帮助我。

编辑:我已经尝试使用此代码和平的 Remou 的测试,通过以下方式打开它:

Dim SQL As String
Dim strValue As String
strValue = GetValueOf("product_details","product_description","C:\cyrillic.txt")
SQL = "UPDATE [products] SET [product_description]='" & strValue & "' WHERE [product_id]=23;"
CurrentDb.Execute SQL,dbseechanges

这是我的函数代码,用于读出我需要的特定行:

Public Function GetValueOf(ByVal Section As String, ByVal Entry As String, ByVal File As String)
Dim fs As New FileSystemObject
Dim ts As TextStream
Dim temp As String
Dim response As String
Dim intresponses As String
Dim SectionFoundBegin As Boolean
Dim SectionFoundEnd As Boolean
Dim DoNext As Boolean
Dim Parse() As String
Dim Finished As Boolean

SectionFoundBegin = False
SectionFoundEnd = False
Set ts = fs.OpenTextFile(File, ForReading, , TristateTrue)
response = ""
intresponses = 1
Finished = False

Do
  DoNext = False
  temp = ts.ReadLine

  If (Not Finished) Then
    If (temp = "[" & Section & "]") And Not DoNext Then
      SectionFoundBegin = True
      DoNext = True
    End If

  If ((InStr(1, temp, "[") > 0) And (SectionFoundBegin)) And Not DoNext Then
    SectionFoundEnd = True
    DoNext = True
  End If

  If (SectionFoundBegin And Not SectionFoundEnd) And Not DoNext Then
    If (InStr(1, temp, "=") > 0) Then
      Parse = Split(temp, "=")
      If (Parse(0) = Entry) Then
        While (intresponses <= UBound(Parse))
          response = response + Parse(intresponses)
          intresponses = intresponses + 1
        Wend

        DoNext = True
      Else
        DoNext = True
      End If
    Else
      DoNext = True
    End If
  End If

End If

Loop Until ts.AtEndOfStream
GetValueOf = response
End Function

我需要什么: 类似的东西:

"UPDATE [products] SET [product_description]='мое описание продукта' WHERE [product_id]=23;"

我得到了什么:

"UPDATE [products] SET [product_description]='??? ???????? ????????' WHERE [product_id]=23;"

更新: 那么现在我真的是你的帮助:

我已插入以下代码:

 Public Function GetUnicodeValueOf(ByVal Section As String, ByVal Entry As String, ByVal File As String)
 Dim fs As Object
 Dim ts As Object
 Set fs = CreateObject("Scripting.FileSystemObject")
 Dim temp As String
 Dim strResponse As String
 Dim intResponses As Integer
 Dim SectionFoundBegin As Boolean
 Dim SectionFoundEnd As Boolean
 Dim DoNext As Boolean
 Dim Parse() As String
 Dim Finished As Boolean

 On Error GoTo Error_GetUnicodeValueOf
 SectionFoundBegin = False
 SectionFoundEnd = False
 Set ts = fs.OpenTextFile(File, ForReading, , TristateTrue)
 strResponse = ""
 intResponses = 1
 Finished = False

 Do
   DoNext = False
   temp = ts.ReadLine

   If (Not Finished) Then
     If (temp = "[" & Section & "]") And Not DoNext Then
       SectionFoundBegin = True
       DoNext = True
     End If

   If ((InStr(1, temp, "[") > 0) And (SectionFoundBegin)) And Not DoNext Then
     SectionFoundEnd = True
     DoNext = True
   End If

   If (SectionFoundBegin And Not SectionFoundEnd) And Not DoNext Then
     If (InStr(1, temp, "=") > 0) Then
       Parse = Split(temp, "=")
       If (Parse(0) = Entry) Then
         While (intResponses <= UBound(Parse))
           strResponse = strResponse + Parse(intResponses)
           intResponses = intResponses + 1
           Finished = True
         Wend

         DoNext = True
       Else
         DoNext = True
       End If
     Else
       DoNext = True
     End If
   End If

 End If

 Loop Until ts.AtEndOfStream

 Exit_GetUnicodeValueOf:
    GetUnicodeValueOf = strResponse
    Exit Function
 Error_GetUnicodeValueOf:
ActionLogging "Fehler beim Parsen der Datei '" & File & "'"
Resume Exit_GetUnicodeValueOf
 End Function

在我的硬盘上使用这个文件(保存为 UTF-8 没有 BOM):

 [product_details]
 manufacturer_name=
 product_id=50
 sku=BU-01722
 set=4
 type=simple
 type_id=simple
 color=11
 ean=
 name=Колесникова
 description=[LANGTEXT] Колесникова Е.В Я считаю до двадцати [Рабочая тетрадь] 6-7л 
 short_description=[KURZTEXT] Колесникова Е.В
 old_id=
 weight=1.0000
 news_from_date=
 news_to_date=
 status=1
 url_key=kolesnikova
 url_path=kolesnikova.html
 visibility=4
 gift_message_available=2
 required_options=0
 has_options=0
 image_label=
 small_image_label=
 thumbnail_label=
 created_at=2012-06-25 07:58:29
 updated_at=2012-07-27 09:06:24
 price=2.0000
 special_price=
 special_from_date=
 special_to_date=
 cost=
 tax_class_id=2
 minimal_price=
 enable_googlecheckout=1
 meta_title=
 meta_keyword=
 meta_description=
 is_recurring=0
 recurring_profile=
 custom_design=
 custom_design_from=
 custom_design_to=
 custom_layout_update=
 page_layout=
 options_container=container2

我需要:

 [LANGTEXT] Колесникова Е.В Я считаю до двадцати [Рабочая тетрадь] 6-7л 

来自 INI-Key:描述

进入我的访问数据库。

首先它可以正常工作,但现在当我加载使用“TriStateTrue”保存的文件时 一切都结束了:?????????????????????????????????????????? 一行。

使用 TriStateMixed,除了西里尔字母之外的所有内容都可以很好地解析 КолеÑник¡¾Ð²Ð° Е.Ð' Я ÑчиÑ,аю до двадцаÑ,и [Ð Ð°Ð±Ð¾Ñ‡Ð°Ñ Ñ,еÑ,радь] 6-7л

我搜索了源代码,没有发现错误。

 FILE is UTF-8 without BOM (coming from selfwritten Web API for Magento)
 Using Access 2003
 Need to get Cyrillic Text into my Database where also German / English Texts could be inside the File

【问题讨论】:

  • 你是怎么看这篇文章的?如果我运行Dim fs As New FileSystemObject: Dim ts As TextStream: Set ts = fs.OpenTextFile("z:\docs\info.ini", ForReading, , TristateTrue): Screen.ActiveControl = ts.ReadAll 我的示例表单上的文本控件将包含您所显示的内容。注意TristateTrue,表示以 unicode 格式打开。 `
  • 我们可以假设 .ini 文件在文件系统上保存为 UTF-8 或 UNICODE 文本文件而不是 ASCII 文件吗? (“另存为”对话框中的“编码”字段...)
  • 感谢您的回答,将测试您的解决方案@Remou。是的,文件保存为 php 脚本的回显响应,为 UTF-8 文件。访问函数向 php 文件发送一个请求,该文件以 INI 文件样式返回“渲染”的结果,我可以通过“GetINIValue”轻松打开该文件并将它们发送到我的数据库中。我打开一个记录集,说我希望我的字段“product_name”应该由 GetINIValue("product_details","product_name","filename.ini") 填充。这适用于德语和英语,但不适用于西里尔字符。我必须先将它们保存到我的数据库中,然后才能在屏幕上显示它们。
  • 我编辑了我的帖子并使用我测试过的方法添加了源代码@Remou,不幸的是,这并没有导致正在考虑的结果。也许我只是那里的一个小错误。如果有人能再看看这个就好了。我必须将西里尔字符存储在数据库中。如果数据存储在以西里尔字母编写的数据库中,则所有内容都可以正确显示,因此显示它们不是问题。缺少的只是从文件中读取以及将数据存储在数据库中
  • 我完全按照在我的 PC 上编写的代码测试了您的代码,最终在数据库中使用了 Cryllic。我在英语语言环境中使用 Windows 7 和 Access 2010。

标签: ms-access vba ini


【解决方案1】:

很久以前,我问了这个问题,终于得到了答案,但由于时间不够,我没能在这里和其他可能有这些问题的人“回答自己”。

首先,关于读取问题:

The Edit from my Question with TryStateTrue was the Right answer, this was the correct line which was needed to load

但现在有问题了:

VBA(6 或更低)中的规则很简单:

What will be saved in an String will be stored as ASCII Value. So every Char which is not an ASCII Code will be thrown away and saved as "?"

我是如何设法保存这些数据的?

我设法使用 C# (.NET) 中的自编写工具保存了这些数据,该工具可以处理 UTF-8 字符串并可以连接到数据库。

  • 在列表中保存部分 + 键或设置为可执行参数以及您将“更新”值的位置 例如:

    [product_details]\name;productsTableName;productsNameField;IdentKeyField;IdentKeyValue
    
  • 打开带参数或不带参数的可执行文件并加载列表

  • 连接到所需的访问数据库
  • 读取 Section\Key-Value 并通过 UPDATE-STATEMENT 直接发送到数据库 例如:

    "UPDATE [productsTableName] SET [productsNameField]='" + ValueFromSectionKey + "' WHERE [IdentKeyField]=IdentKeyValue
    
  • 断开数据库

  • 关闭程序

结果:

  • 一开始有点慢,因为写下了巨大的信息列表
  • 还写下数据库中的所有内容,以及错误(??????而不是 считаю)确保如果您的文件是 ASCII-“可读”,您不会忘记任何内容
  • Access 2003 数据库中精美的 UTF-8 编码和可读文本

这种方法的优点

  • 外包和消耗性工具,如果编写正确,它也可以用于其他项目
  • Access 中的可理解代码(您记下信息,然后在列出所有内容后打开一个处理这些信息的程序)
  • 优化后非常快(读取长度并将列表拆分为多个同时更新数据库的工作人员)

这个方法的缺点

  • 外包
  • 无法直接保存到 VBA 内的变量中(6 或更低)
  • 外部工具可能被防火墙阻止
  • 在“更新”数据库之前,数据库中有不可读的文本
  • 更多更新调用数据库直接
  • 列表中的用户输入错误或包含分隔符的文本可能会导致 UPDATE 语句失败。

希望我能帮上忙。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 2013-08-06
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多