将Excel连接到Mysql更新问题

时间:2010-05-13 18:58:23

标签: mysql sql excel vba

我通过excel中的vba连接到mysql表,我正在更新它:

Set cn = New ADODB.Connection
cn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
    "SERVER=localhost;" & _
    "DATABASE=employees;" & _
    "USER=root;" & _
    "PASSWORD=M1llen;" & _
    "Option=3"
'lets get the batch info
'
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "batchinfo", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table from Report 1
'Set wsSheet1 = wbBook.Worksheets(1)
' better refer by name
'Set wsSheet1 = wbBook.Worksheets.("Report 1")
Worksheets.Item("Report 1").Select
dpath = Range("B2").Text
atime = Trim(Range("B3").Text)
rtime = Trim(Range("B4").Text)
lcalib = Trim(Range("B5").Text)
aname = Trim(Range("B6").Text)
rname = Trim(Range("B7").Text)
bstate = Trim(Range("B8").Text)

instrument = GetInstrFromXML()

With rs
    .AddNew ' create a new record
    ' add values to each field in the record
    .Fields("datapath") = "abc"
    .Fields("analysistime") = atime
    .Fields("reporttime") = rtime
    .Fields("lastcalib") = lcalib
    .Fields("analystname") = aname
    .Fields("reportname") = rname
    .Fields("batchstate") = bstate
    .Fields("instrument") = instrument
    .Update ' stores the new record
End With

问题是,唯一需要更新的字段是仪器字段!!

这里我是一个关于batchinfo表mysql的desc:

mysql> desc batchinfo;
+--------------+---------+------+-----+---------+----------------+
| Field        | Type    | Null | Key | Default | Extra          |
+--------------+---------+------+-----+---------+----------------+
| rowid        | int(11) | NO   | PRI | NULL    | auto_increment |
| datapath     | text    | YES  |     | NULL    |                |
| analysistime | text    | YES  |     | NULL    |                |
| reporttime   | text    | YES  |     | NULL    |                |
| lastcalib    | text    | YES  |     | NULL    |                |
| analystname  | text    | YES  |     | NULL    |                |
| reportname   | text    | YES  |     | NULL    |                |
| batchstate   | text    | YES  |     | NULL    |                |
| instrument   | text    | YES  |     | NULL    |                |
+--------------+---------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
有趣的是,当我重新创建没有auto_increment的表时,它可以正常工作

我真的需要有人来回答这个问题,我不在乎你是否有预感或者不确定,我会尝试任何解决方案

1 个答案:

答案 0 :(得分:4)

我不熟悉MySQL,但TEXT看起来像blob类型?如果是这样,我很惊讶它的工作原理,因为ADO需要对BLOBS进行特殊处理(http://dev.mysql.com/tech-resources/articles/vb-blob-handling.html

请改为使用VARCHAR类型。

您也可以尝试ADOCn.Execute "INSERT ..."