Database access

From CRUNCH - learning analytics
Jump to: navigation, search

To connect to your database

Every studio user has an own mySQL database prepared.

Just connect to localhost with your username and password, the database name is the same as your username.

Here is a sample script showing how to use the package RMySQL to connect to your database. You can download the script source file here.

 library(RMySQL)
 db = MySQL()
 mycon = dbConnect(db, user='yourusername', dbname='yourusername', host='localhost', password='yourpassword')
 rs = dbSendQuery( mycon, "CREATE TABLE terms ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), name VARCHAR(255), cluster INT)")
 rs = dbSendQuery( mycon, "INSERT into terms(name, cluster) values ('dog', NULL)" )
 rs = dbSendQuery( mycon, "INSERT into terms(name, cluster) values ('cat', NULL)" )
 rs = dbSendQuery( mycon, "SELECT * from terms") 
 data = fetch(rs)
 data

Don't forget that you will have to install the package RMySQL, before you are allowed to use it. To do that, please click packages in the bottom right panel of your studio, then click install packages and enter "RMySQL" (caps-sensitive!) as the package name.


A note on database passwords

Take care not to expose your password when releasing scripts with their source code: don't put your password in clear into .R source (source!) files that you release in your public folder. All scripts in public/services/ that are properly named (e.g. .Rmw for R webservice) will not be disclosed: the scripts will always be executed and the code viewer will not display their source code. They are as save as they were in - say - a PHP script.

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox