使用pypyodbc连接到SQL Server

时间:2017-01-04 15:29:00

标签: python sql sql-server pyodbc pypyodbc

我试图使用pypyodbc在python中访问SQL,这是我已经获得的代码:

import React, { Component } from 'react';
import { Text, View } from 'react-native';

import OrientationModule from 'react-native-orientation'

class Example extends Component {
  constructor() {
    super();
    this.state = {
      orientationStatus: 'PORTRAIT'
    };
  }

  componentWillMount(){
    var Orientation = OrientationModule.getInitialOrientation()
    this.setState({
      Orientation: Orientation
    })
  }

  _orientationDidChange = (orientationStatus) => {
    this.setState({orientationStatus})
  }

  componentDidMount(){
    // Add a Listener, every time  Orientation changes this method triggers
    OrientationModule.addOrientationListener(this._orientationDidChange);
  }

  componentWillUnmount(){
      // Remove the Listener, to avoid setting a State while component was unmounted
    OrientationModule.removeOrientationListener(this._orientationDidChange);
  }

  render() {
    // You can Have your Design Logic here
    return (
        <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
            <Text>{this.state.orientationStatus}</Text>
        </View>
      )
  }
}


module.exports = Example

当我尝试连接时,我收到以下错误:

import pypyodbc as pyodbc
db_host = host
db_name = name
db_user = user
db_password = password
connection_string = ("DRIVER={SQL Server};SERVER=" + (db_host) + ";DATABASE=" + (db_name) + ";UID=" + (db_user) + ";PWD=" + (db_password) + ";Trusted_Connection=yes;")
db = pyodbc.connect(connection_string)

我尝试过多种不同的方式更改连接字符串,但每次都会产生相同的错误。这是一个学校项目的工作,所以我试图从学校系统访问SQL服务器,所以我唯一能想到的就是弄乱它是防火墙和连接有问题或者什么的

非常感谢任何帮助或建议。欢呼声。

2 个答案:

答案 0 :(得分:0)

如果您不是特别关注pyodbc,那么通过pymssql提供一个有用的解决方案: -

import pymssql
import _mssql

# Connect to SQL
db_host = host
db_port = port
db_user = user
db_password = password

Conn = pymssql.connect(server=db_host,user=db_user,password=db_password, port=db_port)  

答案 1 :(得分:0)

连接字符串正确。
检查hostnameuserpassword

相关问题