Getting and cleaning data - MySQL Flashcards
ESTABLISH & NAME A CONNECTION
Use a variable and assign the DB connection to it while establishing it::
a <- dbConnect(MySQL(), user= “ “, db =” “, host=” “)
MySQL() ; the type of DB
db ; the database aimed at (optional)
host ; the database name
QUERYING THE MySQL INSTANCE FOR ITS DATABASES
The variable to which the dbConnect is assigned is the connection handle and is used as a pointer:
result <- dbQuery(a, “show databases;”)
The queries are in SQL specifically the MySQL form.
The query can be assigned to a variable which will server to launch it:
query <- dbSendQuery(b, “select * from … where …”)
affyMis <- fetch(query)
EXPLORING TABLES STRUCTURE OF A SPECIFIC DATABASE
From the database listing select whatever database strikes your fancy.
b <- dbConnect(MySQL(), user= “ “ , db=”hg19”, host= “ “)
List the DB’s tables:
allTables <- dbListTables(b)
Remember to use the variable assigned to the connection; its handle.
allTables is a vector so its length should give use the number of tables available:
length(allTables)
allTables[1:10] # extracts the first ten tables names
DISCOVERING A TABLE’S FIELDS
Listing the fields of a given table is the next step to extracting specific values:
dbListFields(b, “name_of_ftable”)
READING FROM TABLES INTO MEMORY
Classic approach; read the table content to create a dat set/ frame:
df <- dbReadTable(b, “name_of_table”)
head (df)
ALWAYS DISCONNECT WHEN YOU ARE FINISHED
dbDisconnect(b)
REFERENCES FOR MySQL COMMANDS
REFERENCES FOR R -MySQL COMMANDS
essential for commands to work in R:
install.packages(“RMySQL”)
References:
http: //www.pantz.org/software/mysql/mysqlcommands.html
http: //www.r-bloggers.com/mysql-and-r/
Only use SELECT, never add, join or delete.