Selecting a specific table in microsoft access 2007 with R code - r

I have an Access database with a couple of tables and I want to work in just one of them. I am using library RODBC. Let's say the table that I want to work it called dtsample. And my Access database is called database.accdb
Here is my code:
library(RODBC)
dataconnect <- odbcConnectAccess2007("database.accdb")
data <- sqlQuery(dataconnect,"SELECT*dtsample columb1, columb2...)
but it does not work. How can I define the table in Access that I want to work with?

Your solution is not really one, because you just got around learning about SELECT
data <- sqlQuery(dataconnect, "SELECT * from dtsample where Columb1 = 'a' or Columb1 ='b')
My suggestion you are not fluent in SQL: Use the query designer in Access, and when it works, get the SQL code generated (View:SQL) and paste it into R.

Related

Retrieve a specific DB project and its tables from SQL Server in R

I am new to using SQL Server from RStudio. I am connected to SQL Server from RStudio and the server has several different projects listed in the below image. For this work I am using odbc library. I am trying to retrieve the tables of a specific project(Project_3960). I have tried dbListTables(conn,"Project_3960") but this command retrieve the tables from all the projects listed in the below Picture. I just want to retrieve the table which are listed in dbo in Project_3690.
The first picture is from RStudio and the second picture is from SQL Management Studio to show the structure of the folders, in case for executing SQL Query.
Thanks
Click on the arrow to the left of the dbo object under Project_3690, and it should show you the tables you have access to. If it does not, then you have a permissions problem and will need to talk with the DBA. That allows you to see them via the GUI. In fact, if you don't already know the names of the tables you should be accessing (such as to follow my code below), then this is the easiest, as they are already filtering out the system and other tables that obscure what you need.
To see them in R code, then dbListTables(conn) will show you all tables, including the ones in the Connections pane I just described but also a lot of system and otherwise-internal tables that you don't need. On my SQL Server instance, it returns over 600 tables, so ... you may not want to do just that, but you can look for specific tables.
For example, if you know you should have tables Table_A and Table_B, then you could do
alltables <- dbListTables(conn)
grep("table_", alltables, value = TRUE, ignore.case = TRUE)
to see all of the table names with that string in its name.
If you do not see tables that you know you need to access, then it is likely that your connection code did not include the specific database, in which case you need something like:
conn <- dbConnect(odbc(), database="Project_3690", uid="...", pwd="...",
server="...", driver = "...")
(Most fields should already be in your connection code, don't use literal ... for your strings.)
One can use a system table to find the other tables:
DBI::dbGetQuery(conn, "select * from information_schema.tables where table_type = 'BASE TABLE' and table_schema = 'dbo'")
# TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
# 1 Project_3690 dbo Table_A BASE TABLE
# 2 Project_3690 dbo Table_B BASE TABLE
# 3 Project_3690 dbo Table_C BASE TABLE
(Notional output but representative of what it should look like.)
Its not quite direct to retrieve the data from SQL server using RStudio when you have different schemas and all are connected to the server. It is easy to view the connected Databases with schema in SQL Server Management Studio but not in RStudio. The easiest way while using Rodbc is to use dot(.) operator and its easy to retrieve the tables of a specific data base is by using "." with dbGetQuery function. I tried dbGetQuery(conn, "select * from project_3690.dbo.AE_ISD ") and it works perfectly fine.

How do you join a table in R with tables in SQL Server

I have a quick but tricky question.
What I'm trying to do:
I have an Excel spreadsheet that I upload in to R studio using
read_excel()
I have 2 tables I need to reference from a SQL server db
I want to only pull date from the SQL tables that match an ID from my
R table
My issues:
The tables I'm referencing from SQL are HUGE! I highly doubt I want
to pull this data into R in order to use it because even if R could
do this, it would take forever
I'm struggling with a solution to join cross platform data
Here is an example of some code that shows what I want to do, but I know doesn't work:
ExcelData <- read_excel("file_location.xlsx")
HospEnct <- sqlQuery(connClarity, "
Select H.PAT_ID, H.PAT_ENC_CSN_ID, H.CONTACT_DATE
From PAT_ENC_HSP AS H
INNER JOIN ExcelData AS E ON E.CSN = H.PAT_ENC_CSN_ID
")
I know I could exclude the join and just pull that data into R and work with the dataframe there, but the data I would pull would be too large. I thought I could define the SQL data by date which would work I think, but will still take more time than I'd like to load since there is so much data. Is there a way to do this all in one script, or will I have to pull the data I need from SQL in to R in order to work with the Excel data I pulled in to R? Sidenote: I don't have writing access to the SQL Server, only read, so I can't put the Excel in to SQL and work from there.

Do I need to worry about SQL injections when using dplyr in R to import csv into SQLite database?

I have a database in R that uses RSQLite. There is one table in this database, and I want the user to be able to import .csv files to add data to that table. If I'm using dplyr to do this, do I need to worry about 'cleaning' the data in the spreadsheet to make sure none of it is going to screw with my database? Like, for example, making sure apostrophes don't interfere with the SQL query. Does dplyr take care of this? I am not very familiar with SQL so please bear with me.
You should be safe as long as you used parametrized statements in RSQLite (dplyr has nothing to do with this), i.e. the potentially dangerous data are set as separated argument and not put directly in the query text. RSQLite is going to make sure the values are properly escaped to avoid SQL injections.
The documentation provides examples of such queries, here with a delete action, but it is transposable to insertion.
rs <- dbSendStatement(mydb, 'DELETE FROM iris WHERE "Sepal.Length" < :x')
## added: bind the actual data here:
dbBind(rs, param = list(x = 4.5))
dbGetRowsAffected(rs)

Having trouble specifying a schema name from MonetDB.R

I am trying to connect to a table that is not in the sys schema. The code below works if sys.tablea exists.
conn <- dbConnect(dbDriver("MonetDB"), "monetdb://localhost/demo")
frame <- monet.frame(conn,"tablea")
If I define tablea in a different schema, e.g. xyz.tablea, then I get the error message
Server says 'SELECT: no such table 'tablea'' [#NA]
The account used to connect has rights to the table.
In a related question, is it possible to use camel-case from MonetDB.R? When I change the table name to TableA, the server again responds with
Server says 'SELECT: no such table 'tablea'' [#NA]
where the table name is all lower-case.
Using tables in other schemata is not possible with the current constructor of monet.frame. However, you can work around the issue as follows:
frame <- monet.frame(conn,"select * from xyz.tablea")
This trick also works with CamelCased table names.
For the next version, I am planning to fix the issue.

SQL Server 2005 - Pass In Name of Table to be Queried via Parameter

Here's the situation. Due to the design of the database I have to work with, I need to write a stored procedure in such a way that I can pass in the name of the table to be queried against if at all possible. The program in question does its processing by jobs, and each job gets its own table created in the database, IE table-jobid1, table-jobid2, table-jobid3, etc. Unfortunately, there's nothing I can do about this design - I'm stuck with it.
However, now, I need to do data mining against these individualized tables. I'd like to avoid doing the SQL in the code files at all costs if possible. Ideally, I'd like to have a stored procedure similar to:
SELECT *
FROM #TableName AS tbl
WHERE #Filter
Is this even possible in SQL Server 2005? Any help or suggestions would be greatly appreciated. Alternate ways to keep the SQL out of the code behind would be welcome too, if this isn't possible.
Thanks for your time.
best solution I can think of is to build your sql in the stored proc such as:
#query = 'SELECT * FROM ' + #TableName + ' as tbl WHERE ' + #Filter
exec(#query)
not an ideal solution probably, but it works.
The best answer I can think of is to build a view that unions all the tables together, with an id column in the view telling you where the data in the view came from. Then you can simply pass that id into a stored proc which will go against the view. This is assuming that the tables you are looking at all have identical schema.
example:
create view test1 as
select * , 'tbl1' as src
from job-1
union all
select * , 'tbl2' as src
from job-2
union all
select * , 'tbl3' as src
from job-3
Now you can select * from test1 where src = 'tbl3' and you will only get records from the table job-3
This would be a meaningless stored proc. Select from some table using some parameters? You are basically defining the entire query again in whatever you are using to call this proc, so you may as well generate the sql yourself.
the only reason I would do a dynamic sql writing proc is if you want to do something that you can change without redeploying your codebase.
But, in this case, you are just SELECT *'ing. You can't define the columns, where clause, or order by differently since you are trying to use it for multiple tables, so there is no meaningful change you could make to it.
In short: it's not even worth doing. Just slop down your table specific sprocs or write your sql in strings (but make sure it's parameterized) in your code.

Resources