i am updating a table in mysql using ADODB
i am adding new entries into a table should i be using addnew or upd开发者_Python百科ate?
There's no difference, you will always use .Update to commit changes from where the current cursor is pointing at. AddNew allocates new row at the end of ADODB recordset
ADODB recordset is a cursor-based data set, when you load rows into recordset, the cursor is automatically on first row, so anything you do on recordset's columns, it will modify wherever the recordset cursor is currently pointing at. For example:
dim rs as new adodb.recordset
rs.Open _
" select emp_id, lastname, firstname, middlename, age from emp " & _
" where location = 'LIVERPOOL'" & _
" ORDER BY emp_id", connectionVariableHere
This will update the first row:
rs!middlename = "Ono"
rs.Update
This will update the next row:
rs.MoveNext
rs!middlename = "Eastman"
rs.Update
To add a record (the cursor will move to last record)
rs.AddNew
rs!lastname = "Ono"
rs!firstname = "Yoko"
rs!middlename = "Yasuda"
rs.Update
This will update the last added record, after performing the step above:
rs!lastname = "Lennon"
rs.Update
If i remember correctly, MoveNext, MoveFirst, etc, implicitly call .Update before moving to new cursor location, so if you are in first row...
rs.MoveFirst
...Then you do:
rs!age = 70 ' lennon's age of 2010
rs.MoveNext
...That will call .Update before moving to Paul McCartney. Anyway, don't rely on it, just call .Update when you want to commit the changes on row
To edit an existing record: .Edit to start, .Update to finish.
To create a new record: .AddNew to start, .Update to finish.
精彩评论