使用PYODBC连接到SQL Server

时间:2017-04-27 13:57:11

标签: python pyodbc

我可以在jupyter笔记本中使用Python连接到SQL Server 2008 R2,但是当我从表中选择前10行时,结果不会在屏幕上呈现。我没有得到任何错误。我需要知道如何从SQL中的表中选择数据,结果会显示在屏幕上。以下是我使用的代码:

library(dplyr)
library(ggplot2)
library(tibble)

is_outlier <- function(x) {
  return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}

dat <- mtcars %>% tibble::rownames_to_column(var="outlier") %>% group_by(cyl) %>% mutate(is_outlier=ifelse(is_outlier(drat), drat, as.numeric(NA)))
dat$outlier[which(is.na(dat$is_outlier))] <- as.numeric(NA)

ggplot(dat, aes(y=drat, x=factor(cyl))) + geom_boxplot() + geom_text(aes(label=outlier),na.rm=TRUE,nudge_y=0.05)

1 个答案:

答案 0 :(得分:3)

看起来你错过了创建实际光标:

import pyodbc 


con = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = 'ServerName', database = 'DBname')
cursor = con.cursor()
cursor.execute("select top 10 accountid from Table")
rows = cursor.fetchall()

for row in rows:
    print(row)
祝你好运!