python操作oracle和mysql

來源:互聯網
上載者:User

標籤:python   oracle   mysql   linux   

1、安裝相關包


yum install python-devel mysql-devel zlib-devel openssl-devel


 


2、安裝setup、mysql-python包


wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz



解壓,各自都分別執行


python setup.py build


python setup.py install


 


3、嘗試串連


#!/usr/bin/evn python

#-*- coding: utf-8 -*-

import os, sys

import MySQLdb

#try:

db=MySQLdb.connect(host = ‘localhost‘, user=‘root‘, passwd=‘oracle‘, db=‘mysql‘, unix_socket=‘/usr/local/mysql/mysql.sock‘)

#except MySQLdb.ERROR,e:

#  print "Error %d:%s"%(e.args[0],e.args[1])

#  exit(1)

cursor=db.cursor()

cursor.execute(‘select * from user‘)

result_set=cursor.fetchall()

print result_set

cursor.close()

db.close()



如果有結果那就成功了。


 


4、插入MYISAM引擎


#!/usr/bin/evn python

#-*- coding: utf-8 -*-

import os, sys

import MySQLdb

#try:

db=MySQLdb.connect(host = ‘localhost‘, user=‘root‘, passwd=‘oracle‘, db=‘mysql‘, unix_socket=‘/usr/local/mysql/mysql.sock‘)

#except MySQLdb.ERROR,e:

#  print "Error %d:%s"%(e.args[0],e.args[1])

#  exit(1)

cursor=db.cursor()

cursor.execute(‘use fastbase;‘)

for x in range(60000):

  cursor.execute("insert into header(CIP, CMAC, CBIOS, UUID, SEQ, SALT, ALG, CHK) values(‘%s‘,‘AABBCCDDEEFF‘,‘‘,%s,%s,123456,‘SHA‘,‘ABCDEFGHIJKLMNOPQRSTUVWXYZ123456‘);"%(x,x,x))

result_set=cursor.fetchall()

print result_set

cursor.close()

db.close()


 


INSERT HEADER表,MYISAM,60000資料,10.68s,大小4.6MB


INSERT HEADER表,MYISAM,3000000資料,9m44s,大小240MB


INSERT CONTENT表,ARCHIVE,15000000資料,39m14s,大小74MB


SELECT HEADER表,MYISAM,3000000資料,27.50s,大小240MB


SELECT CONTENT表,ARCHIVE,15000000資料,>25m,大小74MB


SELECT CONTENT表,MYISAM,15000000資料,>5m52s,大小1.7GB


SELECT CONTENT表,MYISAM,2500000資料,16.00s,大小273MB


SELECT CONTENT表,MYISAM,2500000資料,17.73s,大小13MB


SELECT CONTENT ON CONTENT, ARCHIVE, 2500000資料,top 100,3m45s。


SELECT CONTENT ON CONTENT, ARCHIVE, 2500000資料,>12m52s。


SELECT CONTENT ON CONTENT, MYISAM, 帶索引,2500000資料,1m00s。


 


使用FASTJOIN技術,ARCHIVE引擎,兩邊100條,2.9s。


使用FASTJOIN技術,ARCHIVE引擎,兩邊10000條,26s。


使用FASTJOIN技術,ARCHIVE引擎,兩邊100000條,2m13.18s。


使用FASTJOIN技術,MYISAM引擎,帶索引,兩邊100000條,27.01s。


使用FASTJOIN技術,MYISAM引擎,帶索引,兩邊2500000條,7m30s。





5、插入ARCHIVE引擎


同上


60000資料,9.31s,大小350KB


 


6、分割字串、寫檔案


#!/usr/bin/evn python


#-*- coding: utf-8 -*-


import os, sys


str=‘abcd efg hi j 123‘


output=str.split()


print output


print(output[1:])


print(output[:1])


print(output[1])


file=open(‘/tmp/wr.txt‘,‘a‘)


file.write(output[1])


file.write(‘\n‘)


file.close()




7、操作ORACLE

首先安裝cx_Oracle

前往http://cx-oracle.sourceforge.net/下載

然後使用rpm對應python版本進行安裝

安裝完了後還需要確認安裝了oracle用戶端

否則會出現ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory



此外版本也要注意是64位的還是32位的

另外要加入 

export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/11.2/client/lib 

否則python無法識別



ORACLE_HOME也要指定,才能執行



sqlplus user/[email protected]/hbdb或sqlplus user/[email protected]

如果出現:sqlplus: error while loading shared libraries: /usr/local/oracle/libnnz11.so: cannot restore segment prot after reloc: Permission denied

最簡單的解決方案莫過於將SElinux設定位PERMISSIVE狀態:

[[email protected] ~]# getenforce

Enforcing

[[email protected] ~]# setenforce 0

[[email protected] ~]#  getenforce

Permissive


本文出自 “7727197” 部落格,請務必保留此出處http://7737197.blog.51cto.com/7727197/1663073

python操作oracle和mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.