Getting and cleaning data - MySQL Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

ESTABLISH & NAME A CONNECTION

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

QUERYING THE MySQL INSTANCE FOR ITS DATABASES

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

EXPLORING TABLES STRUCTURE OF A SPECIFIC DATABASE

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

DISCOVERING A TABLE’S FIELDS

A

Listing the fields of a given table is the next step to extracting specific values:

dbListFields(b, “name_of_ftable”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

READING FROM TABLES INTO MEMORY

A

Classic approach; read the table content to create a dat set/ frame:

df <- dbReadTable(b, “name_of_table”)

head (df)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ALWAYS DISCONNECT WHEN YOU ARE FINISHED

A

dbDisconnect(b)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

REFERENCES FOR MySQL COMMANDS

REFERENCES FOR R -MySQL COMMANDS

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly