Description Usage Arguments Value Author(s) See Also Examples
Write data in a table in a MySQL database (referenced by 'edb').
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ## S3 method for class 'RODBC_MySQL'
edbWrite(edb, tableName, data, mode = c("a",
"u", "o")[1], pKey = NULL, getKey = NULL, formatCol = NULL,
posixFormat = "", dateFormat = "", logOp = FALSE,
logRandId = rnorm(1), logMsg = as.character(NA),
logTableName = "edbLog", logCreateTableIfNotExist = TRUE,
parano = TRUE, testFiles = TRUE, verbose = FALSE,
speedInsert = FALSE, speedInsertNRow = 100L, ...)
|
edb |
An object of class 'edb', such as returned by |
tableName |
Single character string. Name of the table to read in 'edb'. |
data |
data.frame. Data to be writen in has a PRIMARY KEY, and if it is AUTOINCREMENT, then the column can be omitted, and the attributed ID's will be retrieved if
NULL, then data must contain the column names given in |
mode |
Single character string. If appened to the table (added after the last row), and is ignored. If critearia in the table is overwritten and |
pKey |
Single character string (if mode == "u") or NULL. Column name that is PRIMARY KEY in the table. |
getKey |
Single character string or NULL. If non NULL, name of the PRIMARY KEY whose latest attributed values should be retrieved. |
formatCol |
If not NULL, a named list of functions to be applied to certain columns before the data are written to the database. The name of each list item gives the column to process, and the value of each item gives the function that must be applied. For instance formatCol = list("DATE"=as.Date) will apply the function as.Date to the column "DATE". |
posixFormat |
Single character string. 'format' argument of the functions format.POSIXlt() or format.POSIXct() used to convert POSIX date-time into character strings when writing into the database. Only used if getKey is not NULL or when mode == "u" in SQLite or MySQL. |
dateFormat |
Single character string. 'format' argument of the functions format.Date() used to convert "Date" dates into character strings when writing into the database. Only used if getKey is not NULL or when mode == "u" in SQLite or MySQL. |
logOp |
Single logical. If TRUE, then a log of the operation is written into the database, using the function See the arguments below and |
logRandId |
Single numerical. See |
logMsg |
Single character string. See |
logTableName |
Single character string. See |
logCreateTableIfNotExist |
Single logical. See |
parano |
Single logical. If set to TRUE (the default), the function is run on "paranoia mode", that is additional tests are performed before the data are written into the database. This slows down a bit (more) the function, but it may avoid some mistakes. |
testFiles |
Single logical. Should the function test for the presence (file.exist()) of the needed files in the folder before trying to fetch information from the database? |
verbose |
Single logical. If TRUE, information on what is done are output on screen. |
speedInsert |
Single logical. If TRUE, multiple insert statements into one query, instead of inserting data row by row (slower). Will only work if
|
speedInsertNRow |
Single integer. Number of rows to be inserted at once when
|
... |
Additional parameters to be passed to class-specific method. See
|
If id.col.nm is not NA, the function returns a list containing
a vector of ID values, and named after 'id.col.nm'.
If an error message is detected the function stops.
Julien MOEYS <Julien.Moeys@mark.slu.se>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 | library( "easyrodbcmysql" )
testMySQL <- FALSE # Set to TRUE to run some tests
# after modifying the edb() part below
# with your own database setting
### Windows only:
if( (Sys.info()[[ "sysname" ]] == "Windows") & testMySQL ){
### Describe the database (NB: this is not a connection)
myDb <- edb(
dbType = "RODBC_MySQL",
dbSourceName = "nameOfODBCSource", # or any name you like
dbName = "nameOfDatabase",
dbLogin = "yourUserName",
dbPwd = "yourPassword",
dbHost = "127.0.0.1",
dbPort = 3306
) #
### Register the data source in ODBC
edbDataSource( myDb, verbose = TRUE )
### Use the database:
## Write data in a table in the database
# First retrieve the table profile
profileTbl <- myDb[ "PROFILE" ]
# Change the ID's (pseudo new profiles)
profileTbl[, "ID_PROFILE" ] <- 3:4
# Write the 'new' data in the database:
edbWrite(
edb = myDb,
tableName = "PROFILE",
data = profileTbl,
mode = "a" # append
)
# Alternative method:
profileTbl[, "ID_PROFILE" ] <- 5:6 # Change IDs.
myDb[ "PROFILE", mode = "a" ] <- profileTbl
myDb[ "PROFILE" ] # Look at the result
# This would not work, because one column is missing:
# (because the underlying function use dbWriteTable)
try(
edbWrite(
edb = myDb,
tableName = "PROFILE",
data = profileTbl[, -1 ],
mode = "a"
)
) # Error
# But as "ID_PROFILE" is a primary key, it can be omittted
# if 'getKey' is specified:
edbWrite(
edb = myDb,
tableName = "PROFILE",
data = profileTbl[, -1 ],
mode = "a",
#verbose = TRUE,
getKey = "ID_PROFILE"
) #
## Create a new table:
edbWrite(
edb = myDb,
tableName = "PROFILE2",
data = profileTbl,
mode = "o",
) #
# Alternative metod:
myDb[ "PROFILE3", mode = "o" ] <- profileTbl
edbNames( myDb )
## Update some values:
profileTbl[, "COMMENTS" ] <- "My comment"
edbWrite(
edb = myDb,
tableName = "PROFILE",
data = profileTbl,
mode = "u", # update
pKey = "ID_PROFILE" # Primary key
)
myDb[ "PROFILE" ]
# Alternative method:
profileTbl[, "COMMENTS" ] <- "My other comment"
myDb[ "PROFILE", mode = "u", pKey = "ID_PROFILE" ] <- profileTbl
myDb[ "PROFILE" ]
# Delete some rows
edbDelete(
edb = myDb,
tableName = "PROFILE",
sRow = list("SQL" = "ID_PROFILE > 2")
) #
# Drop tables
edbDrop( edb = myDb, tableName = "PROFILE2" )
edbDrop( edb = myDb, tableName = "PROFILE3" )
# It may be useful to transform some columns 'on-the-fly', before
# they are written to the database. In the example below we
# have some dates and times values, as well as some boolean stored
# as integers (seconds or days since 1970-01-01 or 0/1 values,
# respectively). We want to transform them when writing in the database.
myDb[ "MISCFORMAT" ]
# NB: although Yes/No format, the last column is read as integer too...
# So date variables have to be converted when written to the database.
# The code below show how to do that.
# Prepare a new record to be written:
newRecord <- data.frame(
"ID_RECORD" = 2,
"DAT_TIM_SEC" = as.POSIXct( "2011-12-15 12:00:00", tz = "GMT" ),
"DAT_DAY" = as.Date( "2011-12-15" ),
"TEST_BOOL" = FALSE,
"DAT_TIM" = as.POSIXct( "2011-12-15 12:00:00", tz = "GMT" ),
"DAT" = as.Date( "2011-12-15" ),
"TEST_BOOL2" = FALSE
) #
newRecord
# Write the record:
myDb[ "MISCFORMAT", formatCol = list( "DAT_TIM_SEC" = as.integer,
"DAT_DAY" = as.integer, "TEST_BOOL" = as.integer,
"TEST_BOOL2" = as.integer ) ] <- newRecord
# The records have been written as integers:
myDb[ "MISCFORMAT" ]
# But we can convert them on-the-fly
# Function to convert POSIX integer "seconds from 1970-01-01" into
# R POSIXct date format.
formatDT <- function( x, tz = "GMT" ){
res <- ISOdatetime( year = 1970, month = 1, day = 1,
hour = 0, min = 0, sec = 0, tz = tz )
res <- res + x
return( res ) }
# Function to convert integer "days from 1970-01-01" into
# R Date format.
formatD <- function( x, tz = "GMT" ){
res <- ISOdate( year = 1970, month = 1, day = 1, tz = tz )
res <- res + (x * 24 * 60 * 60 )
res <- as.Date( res )
return( res ) }
myDb[ "MISCFORMAT", formatCol = list( "DAT_TIM_SEC" = formatDT,
"DAT_DAY" = formatD, "TEST_BOOL" = as.logical ) ]
# Misc test: just to make sure that this works too:
edbWrite(
edb = myDb,
tableName = "MISCFORMAT",
data = newRecord[, -1 ],
mode = "a",
verbose = TRUE,
getKey = "ID_RECORD",
formatCol = list( "DAT_TIM_SEC" = as.integer,
"DAT_DAY" = as.integer, "TEST_BOOL" = as.integer,
"TEST_BOOL2" = as.integer )
) #
# Clean-up a bit:
edbDelete(
edb = myDb,
tableName = "MISCFORMAT",
sRow = list("SQL" = "ID_RECORD > 1")
) #
# It is possible to write an operation "log" every time edbWrite()
# is used (or edbDelete() or edbDrop()). The exact operation is
# not logged, but rather the function name, the table concerned,
# the version of R and easydb, the date, an eventual log message,
# etc. Set the argument 'logOp' to TRUE to log operations:
# - Fetch some data
profileTbl <- myDb[ "PROFILE", sRow = list( "ID_PROFILE" = 1 ) ]
# - Write it back, with a log
myDb[ "PROFILE", mode = "u", pKey = "ID_PROFILE", logOp = TRUE,
logMsg = "Some log message" ] <- profileTbl
# Now check the log:
myDb[ "edbLog" ]
# - Clean up a bit
edbDrop( edb = myDb, tableName = "edbLog" )
### Un-register the data source in ODBC (windows only)
edbDataSource( myDb, trash = TRUE )
} #
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.