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)
I am trying to create a Windows form with a self contained DB. I added a Service Based DataBase. I then created a Dataset. My table is simple:
Example:
columns are: Model (PK), Part1, Part2, Part3............
I want a combobox on my for to be able to select the model, and another combobox to select the desired part then return the number. You pick Model 740, and Part1 and the return is part1 part number.
The issue is that I need the select Statement in the tableadapter query builder to show something like this.
SELECT *
FROM Table
WHERE Model = ? (Paramter from ComboBox1) AND parameter (Part1 From Combobox2 Will be a Column Name)
I can't figure out how or if it is even possible to have a parameter in after the select.
Table is built like this:
[enter image description here][1]
[1]: https://i.stack.imgur.com/bf4kv.png
I got it figured out
'TODO: This line of code loads data into the 'CSDataset.CryoSeries' table. You can move, or remove it, as needed.
Me.CryoSeriesTableAdapter.Fill(Me.CSDataset.CryoSeries)
'Pulling Data From Table Adapter
Dim db As New DataTables.CSDatasetTableAdapters.CryoSeriesTableAdapter
Dim result = db.GetData
Dim Model As String = CBmodel.Text.Trim
Dim Part As String = CBPart.Text.Trim
TBresult.Text = result.Rows(0).Item(Part)
I am using python with sqlite3, and I have problem in reading out tables in a data base created by myself.
The data base is created using the below code snippet
con = sqlite3.connect(folderdir + dbFileName)
#Create a cursor
c = con.cursor()
#Below code creates the data base. I later need to extract all tablename in
my data base
c.execute('CREATE TABLE IF NOT EXISTS ' + tablename + ' (Bord text, Date
text, Time text, ID integer, Menu text, Qty integer, Price real,
Time_kitchen text, TableActive integer)')
c.execute('INSERT INTO ' + tablename + ''' (Bord, Date, Time, TableActive)
VALUES(?,?,?,?)''',
(tablename, date, tabletime, 1))
con.commit()
con.close()
If above code is called many times with different tablename variables, I get a database.db file containing many tablenames. My problem is, even using the sqlite_master, I cannot read out all the tablenames. I use the below code to read out
import sqlite3
#I have verified that I can connect successfully to the database file
con = sqlite3.connect(dbFilePath + "/" + dbFileName + ".db")
c = con.cursor()
c.execute("SELECT ALL Name FROM sqlite_master")
rows = c.fetchall()
for row in rows:
print(row)
The print returned nothing although I know for sure that I have a database with multiple tablenames in. What am I doing wrong?
When the file whose name is given to connect() does not exist, SQLite will happily create a new, empty database for you.
Ensure that the file name is correct; and it should not be a relative file name to avoid a dependency on the current directory.
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)
I have a large excel worksheet that I want to add to my database.
Can I generate an SQL insert script from this excel worksheet?
I think importing using one of the methods mentioned is ideal if it truly is a large file, but you can use Excel to create insert statements:
="INSERT INTO table_name VALUES('"&A1&"','"&B1&"','"&C1&"')"
In MS SQL you can use:
SET NOCOUNT ON
To forego showing all the '1 row affected' comments. And if you are doing a lot of rows and it errors out, put a GO between statements every once in a while
You can create an appropriate table through management studio interface and insert data into the table like it's shown below. It may take some time depending on the amount of data, but it is very handy.
There is a handy tool which saves a lot of time at
http://tools.perceptus.ca/text-wiz.php?ops=7
You just have to feed in the table name, field names and the data - tab separated and hit Go!
You can use the following excel statement:
="INSERT INTO table_name(`"&$A$1&"`,`"&$B$1&"`,`"&$C$1&"`, `"&$D$1&"`) VALUES('"&SUBSTITUTE(A2, "'", "\'")&"','"&SUBSTITUTE(B2, "'", "\'")&"','"&SUBSTITUTE(C2, "'", "\'")&"', "&D2&");"
This improves upon Hart CO's answer as it takes into account column names and gets rid of compile errors due to quotes in the column. The final column is an example of a numeric value column, without quotes.
Depending on the database, you can export to CSV and then use an import method.
MySQL - http://dev.mysql.com/doc/refman/5.1/en/load-data.html
PostgreSQL - http://www.postgresql.org/docs/8.2/static/sql-copy.html
Use the ConvertFrom-ExcelToSQLInsert from the ImportExcel in the PowerShell Gallery
NAME
ConvertFrom-ExcelToSQLInsert
SYNTAX
ConvertFrom-ExcelToSQLInsert [-TableName] <Object> [-Path] <Object>
[[-WorkSheetname] <Object>] [[-HeaderRow] <int>]
[[-Header] <string[]>] [-NoHeader] [-DataOnly] [<CommonParameters>]
PARAMETERS
-DataOnly
-Header <string[]>
-HeaderRow <int>
-NoHeader
-Path <Object>
-TableName <Object>
-WorkSheetname <Object>
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
ALIASES
None
REMARKS
None
EXAMPLE
ConvertFrom-ExcelToSQLInsert MyTable .\testSQLGen.xlsx
You could use VB to write something that will output to a file row by row adding in the appropriate sql statements around your data. I have done this before.
Here is another tool that works very well...
http://www.convertcsv.com/csv-to-sql.htm
It can take tab separated values and generate an INSERT script. Just copy and paste and in the options under step 2 check the box "First row is column names"
Then scroll down and under step 3, enter your table name in the box "Schema.Table or View Name:"
Pay attention to the delete and create table check boxes as well, and make sure you examine the generated script before running it.
This is the quickest and most reliable way I've found.
You can use the below C# Method to generate the insert scripts using Excel sheet just you need import OfficeOpenXml Package from NuGet Package Manager before executing the method.
public string GenerateSQLInsertScripts() {
var outputQuery = new StringBuilder();
var tableName = "Your Table Name";
if (file != null)
{
var filePath = #"D:\FileName.xsls";
using (OfficeOpenXml.ExcelPackage xlPackage = new OfficeOpenXml.ExcelPackage(new FileInfo(filePath)))
{
var myWorksheet = xlPackage.Workbook.Worksheets.First(); //select the first sheet here
var totalRows = myWorksheet.Dimension.End.Row;
var totalColumns = myWorksheet.Dimension.End.Column;
var columns = new StringBuilder(); //this is your columns
var columnRows = myWorksheet.Cells[1, 1, 1, totalColumns].Select(c => c.Value == null ? string.Empty : c.Value.ToString());
columns.Append("INSERT INTO["+ tableName +"] (");
foreach (var colrow in columnRows)
{
columns.Append("[");
columns.Append(colrow);
columns.Append("]");
columns.Append(",");
}
columns.Length--;
columns.Append(") VALUES (");
for (int rowNum = 2; rowNum <= totalRows; rowNum++) //selet starting row here
{
var dataRows = myWorksheet.Cells[rowNum, 1, rowNum, totalColumns].Select(c => c.Value == null ? string.Empty : c.Value.ToString());
var finalQuery = new StringBuilder();
finalQuery.Append(columns);
foreach (var dataRow in dataRows)
{
finalQuery.Append("'");
finalQuery.Append(dataRow);
finalQuery.Append("'");
finalQuery.Append(",");
}
finalQuery.Length--;
finalQuery.Append(");");
outputQuery.Append(finalQuery);
}
}
}
return outputQuery.ToString();}
Here is a link to an Online automator to convert CSV files to SQL Insert Into statements:
CSV-to-SQL
This query i have generated for inserting the Excel file data into database
In this id and price are numeric values and date field as well. This query summarized all the type which I require It may useful to you as well
="insert into product (product_id,name,date,price) values("&A1&",'" &B1& "','" &C1& "'," &D1& ");"
Id Name Date price
7 Product 7 2017-01-05 15:28:37 200
8 Product 8 2017-01-05 15:28:37 40
9 Product 9 2017-01-05 15:32:31 500
10 Product 10 2017-01-05 15:32:31 30
11 Product 11 2017-01-05 15:32:31 99
12 Product 12 2017-01-05 15:32:31 25
I had to make SQL scripts often and add them to source control and send them to DBA.
I used this ExcelIntoSQL App from windows store https://www.microsoft.com/store/apps/9NH0W51XXQRM
It creates complete script with "CREATE TABLE" and INSERTS.
I have a reliable way to generate SQL inserts batly,and you can modify partial parameters in processing.It helps me a lot in my work, for example, copy one hundreds data to database with incompatible structure and fields count.
IntellIJ DataGrip , the powerful tool i use.
DG can batly receive data from WPS office or MS Excel by column or line.
after copying, DG can export data as SQL inserts.