Python operation Oracle and MySQL

Source: Internet
Author: User

1, install the relevant package


Yum install python-devel mysql-devel zlib-devel openssl-devel



2. Install Setup, Mysql-python package


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



Unzip, each of which is executed separately


Python setup.py Build


Python setup.py Install



3. Try to connect


#!/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 ()



If there is a result, it will succeed.



4. Insert the MyISAM engine


#!/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 table, myisam,60000 data, 10.68s, size 4.6MB


INSERT Header table, myisam,3000000 data, 9m44s, size 240MB


INSERT content table, archive,15000000 data, 39m14s, size 74MB


SELECT Header table, myisam,3000000 data, 27.50s, size 240MB


SELECT content table, archive,15000000 data, >25m, size 74MB


SELECT content table, myisam,15000000 data, >5m52s, size 1.7GB


SELECT content table, myisam,2500000 data, 16.00s, size 273MB


SELECT content table, myisam,2500000 data, 17.73s, size 13MB


SELECT content on content, ARCHIVE, 2500000 data, top 100,3m45s.


SELECT content on content, ARCHIVE, 2500000 data, >12m52s.


SELECT content on content, MYISAM, indexed, 2500000 data, 1m00s.



Using Fastjoin technology, the archive engine, 100 strips on both sides, 2.9s.


Using Fastjoin technology, the archive engine, 10,000 strips on both sides, 26s.


Using Fastjoin technology, archive engine, 100,000 strips on both sides, 2m13.18s.


Using Fastjoin technology, MyISAM engine, with index, 100,000 strips on both sides, 27.01s.


Using Fastjoin technology, MyISAM engine, with index, 2.5 million strips on both sides, 7m30s.





5. Insert the archive engine


Ditto


60000 data, 9.31s, size 350KB



6. Split string, write file


#!/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. Operation of Oracle

Install Cx_oracle First

Go to http://cx-oracle.sourceforge.net/download

Then use RPM for the Python version to install

After installation, you also need to confirm that the Oracle client is installed

Otherwise, Importerror:libclntsh.so.11.1:cannot open shared object file:no such file or directory will appear



Also note that the version should be 64-bit or 32-bit.

Also to join

Export Ld_library_path=${ld_library_path}:/usr/lib/oracle/11.2/client/lib

Otherwise, Python does not recognize



Oracle_home must also be specified in order to execute



Sqlplus user/[email protected]/hbdb or sqlplus user/[email protected]

If it appears: Sqlplus:error while loading GKFX libraries:/usr/local/oracle/libnnz11.so:cannot restore segment prot after reloc : Permission denied

The simplest solution is to set the SELinux bit permissive state:

[Email protected] ~]# Getenforce

Enforcing

[Email protected] ~]# Setenforce 0

[Email protected] ~]# Getenforce

Permissive


This article is from the "7727197" blog, please be sure to keep this source http://7737197.blog.51cto.com/7727197/1663073

Python operation Oracle and MySQL

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: 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.