【问题标题】:SQL records in a MsgBoxMsgBox 中的 SQL 记录
【发布时间】:2016-10-26 14:25:00
【问题描述】:

我有一个运行良好的 VBS 程序。我的代码如下:

Dim qry, db, cs, cn, cmd

'Query to add all the following which date more than 20 days in the table ToUnfollow
qry  = "INSERT INTO ToUnfollow " & _
       "SELECT Name FROM Follow t1 " & _
       "WHERE t1.Joined < datetime(CURRENT_DATE, '-20 days') " & _
       "AND NOT EXISTS (SELECT 1 FROM ToUnfollow t2 WHERE t2.Name=t1.Name);"

db = "C:\Users\Quentin\Downloads\Quentin-Classementhashtags.db"
cs = "DRIVER=SQLite3 ODBC Driver;Database=" & db

'Connection to database
Set cn = CreateObject("ADODB.Connection")
cn.Open cs

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn

'Execute the SQL query
cmd.CommandText = qry
cmd.Execute

'Close the connection
cn.Close

我想在MsgBox 中显示来自 SQL 查询的所有记录。我尝试了几个论坛的几种解决方案,但没有一个适合我。

【问题讨论】:

  • INSERT 语句不返回任何记录。如果要显示插入的内容,则需要将语句的 SELECT 部分作为单独的语句运行。如需进一步帮助,请展示您的尝试,并说明结果与您的预期有何不同。
  • 我的帖子是用我测试过的代码编辑的。我发现的解决方案要求处理 RecordSet,我对此不是很强大。
  • @AnsgarWiechers 完成
  • 谢谢。不要忘记接受您的回答。

标签: sql database sqlite vbscript msgbox


【解决方案1】:

我的解决方案:

Dim qry, db, cs, cn, cmd, adoRec, name

'Query to add all the following which date more than 20 days in the table
'ToUnfollow
qry  = "SELECT Name, Joined FROM Follow t1 " & _
       "WHERE t1.Joined < datetime(CURRENT_DATE, '-20 days')"

db = "C:\Users\Quentin\Downloads\Quentin-Classementhashtags.db"
cs = "DRIVER=SQLite3 ODBC Driver;Database=" & db

'Connection to database
Set cn = CreateObject("ADODB.Connection")
cn.Open cs

Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn

cmd.CommandText = qry
Set adoRec = cmd.Execute()

'Iterate through the results
While Not adoRec.EOF
  name = name & vbCr & adoRec(0) & " - " & adoRec(1)
  adoRec.MoveNext
Wend

MsgBox name

'Close the connection
cn.Close

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 2020-06-18
    • 1970-01-01
    • 2013-12-15
    • 2019-11-02
    相关资源
    最近更新 更多