Using Python to link Oracle databases under Windows

Source: Internet
Author: User

Learn python, recording the process to make a backup, convenient for later inquiries. Learn from other great God articles that will be recorded before the text.
Reference article: http://evil850209.iteye.com/blog/1394932
Http://www.cnblogs.com/oubo/archive/2012/07/24/2607034.html
Steps:
1, download Python and Oracle.
2, go to python website Download cx_oracle package, run. You should also be able to run it with code. Download Address http://cx-oracle.sourceforge.net/,
3, add Tnsnames.ora file.
This file is actually configured when you use Oracle, which is equivalent to setting the access address of the database.

Datacent=
  (DESCRIPTION = (
    address_list =
      (address = (PROTOCOL = TCP) (HOST = IP) (PORT = 1521))
    ( Connect_data =
      (service_name = datacent)
    )
  )

4. Configure Environment variables

5, the package has been installed, in Python under test to run Cx_oracle package, and linked to the database.

#-*-Coding:utf-8-*-
import cx_oracle
conn=cx_oracle.connect (' username/password @ database IP address: 1521/datacent ')
cur= Conn.cursor () #游标
printheader = true# First row
cur.execute ("Select Cust_no,reg_date from Cust_info")
#data = Cur.fetchall () #所有记录
data=cur.fetchone () #一条记录
for row in data:  
    print "%s,%d" (Row[0], row[1])  #转化格式 C10/>cur.close () #关闭游标
conn.close () #关闭数据链接

The above code can be run to link the database, then do data processing or other analysis.
Cons: The difference between Python and R is that from the database after the number can not be built like r a table, not too convenient, there may be a solution I do not know, know the classmate can tell me, let me a data learning python this biased computer tools, cough cough or a lot of places do not understand.

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: info-contact@alibabacloud.com 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.