How Should I create Table in SQL using the R Data frame object structure with out writing complex code. Is there is a any function in R to accomplish this.
if you are using mysql, you can use the dbWriteTable from RMySQL package
library(RMySQL)
con <- dbConnect(MySQL(),
user="USER_NAME",
host="localhost",
password = "PASS",
db = "NAME_DATA_BASE")
dbWriteTable(conn = con, name = 'test', value = iris)
In this example i put iris data frame in table named test
In sqlite:
library(RSQLite)
#set working directory to where your sqlite database resides
setwd("C:/sqlite/Data")
#connect
sqlite<-dbDriver("SQLite")
my_conn<-dbConnect(sqlite,"my_db.db")
#write out the dataframe in the database
dbWriteTable(my_conn, "new_table_in_db", my_dataframe, row.names=F)
Related
I'm trying to catalog the structure of a MSSQL 2008 R2 database using R/RODBC. I have set up a DSN, connected via R and used the sqlTables() command but this is only getting the 'system databases' info.
library(RODBC)
conn1 <- odbcConnect('my_dsn')
sqlTables(conn1)
However if I do this:
library(RODBC)
conn1 <- odbcConnect('my_dsn')
sqlQuery('USE my_db_1')
sqlTables(conn1)
I get the tables associated with the my_db_1 database. Is there a way to see all of the databases and tables without manually typing in a separate USE statement for each?
There may or may not be a more idiomatic way to do this directly in SQL, but we can piece together a data set of all tables from all databases (a bit more programatically than repeated USE xyz; statements) by getting a list of databases from master..sysdatabases and passing these as the catalog argument to sqlTables - e.g.
library(RODBC)
library(DBI)
##
tcon <- RODBC::odbcConnect(
dsn = "my_dsn",
uid = "my_uid",
pwd = "my_pwd"
)
##
db_list <- RODBC::sqlQuery(
channel = tcon,
query = "SELECT name FROM master..sysdatabases")
##
R> RODBC::sqlTables(
channel = tcon,
catalog = db_list[14, 1]
)
(I can't show any of the output for confidentiality reasons, but it produces the correct results.) Of course, in your case you probably want to do something like
all_metadata <- lapply(db_list$name, function(DB) {
RODBC::sqlTables(
channel = tcon,
catalog = DB
)
})
# or some more efficient variant of data.table::rbindlist...
meta_df <- do.call("rbind", all_metadata)
Trying to solve issue with wrong display of national characters (Polish) in results of query to MS SQL database.
The script is pretty standard
First the definnition connection object
library(DBI)
db.conn <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "10.0.0.100",
Port = 1433,
Database = "DB",
UID = "user",
PWD = rstudioapi::askForPassword("Database password"),
encoding = "latin1"
)
then SQL statement
db_sql = "
select
*
from test
where active = 'ACTIVE'
order by name_id"
Then execution of SQL
db_query <- dbSendQuery(db.conn, db_sql)
db_data <- dbFetch(db_query)
or
db_data <- dbGetQuery(db.conn, db_sql)
It does not matter whether in connection object definition I use "latin1", "windows-1250" or "utf-8" parameter for encoding parameter the results are always the same
Strings with U+009C or similar
It also does not matter what codepage I select in RStudio Global options.
Problem solved.
First it is necessary to change locale to Polish
Sys.setlocale(category = "LC_ALL", locale = "Polish")
Then set proper encoding in
DBI::dbConnect(odbc::odbc()
...
encoding = "windows-1250"
and voilla, working.
If I want to create a new column containing observation numbers with data frames I can,
mtcars %>% mutate(i=row_number())
But row_number() does not work with sql tables.
mydb <- dbConnect(RSQLite::SQLite(), "")
dbWriteTable(mydb, "mt", mtcars)
mt.sql=tbl(mydb, "mt")
mt.sql %>% mutate(i=row_number())
Error:
Window function row_number() is not supported by this database
Would there be any other ways around this problem?
You could work around it by using SQLite syntax like this
RSQLite::dbSendQuery(mydb, "ALTER TABLE mt ADD COLUMN i INTEGER")
RSQLite::dbSendQuery(mydb, "UPDATE mt SET (i) = ROWID")
Then you can go on using dplyr syntax like after re-assigning mt from your db connection to the mt.sql object.
mt.sql=tbl(mydb, "mt")
mt.sql %>% select(mpg, i) # e.g.
SQLite doesn't support row number function.
Did you try mtcars %>% mutate(i=row_number(desc(disp)))?
It works for me even in SQL.
I've got a simple dataframe with three columns. One of the columns contains a database name. I first need to check if data exists, and if not, insert it. Otherwise do nothing.
Sample data frame:
clientid;region;database
135;Europe;europedb
2567;Asia;asiadb
23;America;americadb
So I created a function to apply to dataframe this way:
library(RMySQL)
check_if_exist <- function(df){
con <- dbConnect(MySQL(),
user="myuser", password="mypass",
dbname=df$database, host="myhost")
query <- paste0("select count(*) from table where client_id='", df$clientid,"' and region='", df$region ,"'")
rs <- dbSendQuery(con, query)
rs
}
Function call:
df$new_column <- lapply(df, check_if_exist)
But this doesn't work.
This is a working example of what you are asking, if I understood correctly. But I don't have your database, so we just print the query for verification, and fetch a random number as the result.
Note that by doing lapply(df,...), you are looping over the columns of the database, and not the rows as you want.
df = read.table(text="clientid;region;database
135;Europe;europedb
2567;Asia;asiadb
23;America;americadb",header=T,sep=";")
check_if_exist <- function(df){
query = paste0("select count(*) from table where client_id='", df$clientid,"' and region='", df$region ,"'")
print(query)
rs <- runif(1,0,1)
return(rs)
}
df$new_column <- sapply(split(df,seq(1,nrow(df))),check_if_exist)
Hope this helps.
I have two sqlite.db files. I'd like to copy the contents of one column in a table of on db file to another.
for example:
I have the model Information in db file 1:
class Information(models.Model):
info_id = models.AutoField(primary_key = True)
info_name = models.CharField( max_length = 50)
and the following information model in db file 2:
class Information(models.Model):
info_id = models.AutoField(primary_key = True)
info_type = models.CharField(max_length = 50)
info_name = models.CharField( max_length = 50)
I'd like to copy all the data in column info_id and info_name from db file 1 to info_id and info_name in db file 2.
I understand Raw SQL would be needed.. An example would be appreciated.
Thoughts?
If you are trying to do this because you changed your "Information" Model and you would now like to update your database to include the extra field then please have a look at Django's documentation (http://www.djangobook.com/en/1.0/chapter05/) at the section entitled "Making Changes To A Database Schema" for detailed information.
If you simply want to copy data between two tables, you are going to have to bring both of them to the same schema (with different names of course) and then run an SQL query like "INSERT INTO new_Information(info_id,info_name) select info_id,info_name from old_Information" from the sqlite admin program.