Python reads the configuration file and connects to the database SQL Server

Source: Internet
Author: User
Tags postgresql redis

Using the configuration file to save fixed connection data, it is more convenient to change.

1. Create a new configuration file: Sqlconfig.config, take the database as an example.

The content is as follows, and of course you can add multiple



2. Read the configuration file. The class that the user configparser to read the file.

The code is as follows

# coding = utf-8
import ConfigParser
def getSQLCONFIG (filename):
     cf = ConfigParser.ConfigParser () (filename) #Read configuration file
     # Read corresponding file parameters
     _database = cf.get ("Database1", "database")
     _host = cf.get ("Database1", "host")
     _user = cf.get ("Database1", "user")
     _pwd = cf.get ("Database1", "pwd")
     print "% s,% s,% s,% s"% (_database, _host, _user, _pwd)
     return _database, _host, _user, _pwd #return required parameters

3. Connect to the database and install the PYODBC library. Connecting to a SQL Server database

# coding = utf-8
import pyodbc
import ReadConfig
#Call to read the configuration file
c = ReadConfig.getSQLCONFIG (r‘C: \ Users \ Administrator \ PycharmProjects \ untitled1 \ com \ SQLqueray \ SQlconfig.config ‘)

conn_info = 'DRIVER = {SQL Server}; DATABASE =% s; SERVER =% s; UID =% s; PWD =% s'% (c [0], c [1], c [2], c [3 ])
mssql_conn = pyodbc.connect (conn_info)
mssql_cur = mssql_conn.cursor ()
#Query Name and User ID
result = mssql_cur.execute ("select Fullname, id from god where id = 19688")
#Circular print query results
for row in result:
     print row [0], row [1] 

Python reads the configuration file and connects to the database SQL Server

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.