求助!关于在Linux用Python操作oracle数据库入门教程!

3227人阅读
python(3)
刚搭建完redis的环境累个半死,刚又弄了一个mysql的环境,遇到多种问题先简单记录,以备不时之需
<span style="font-size:14 color:#、首先mysql、python环境肯定要有吧!
假定mysql安装在/usr/local/mysql
<span style="font-size:14 color:#、下载MySQL-python-1.2.3.tar.gz 并解压
解压:tar zxvf MySQL-python-1.2.3.tar.gz
<span style="color:#、进入 MySQL-python-1.2.3&修改setup_posix.py文件
visetup_posix.py
修改行:#mysql_config.path
= &mysql_config&#注释掉
为:mysql_config.path =&/usr/local/mysql/bin/mysql_config&
若果不修改会报错误:mysql_config
&&& 原因:mysql_config命令没有找到。
4、在保证有setuptools的情况下执行以下三个代码(如果没有setuptools,请参考上一篇文)
python setup.py build
python setup.py install
出现error: command 'gcc' failed with exit status 1的解决方法
&&&& yum install MySQL-p*
&&&yum install python-devel
&&&cd MySQL-python-1.2.3
&&& python setup.py build
&&& python setup.py install
报错:./directadmin:
error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
(most recent call last):
& File &existuser.py&, line 2, in &module&
& & import MySQLdb
& File &build/bdist.linux-x86_64/egg/MySQLdb/__init__.py&, line 19, in &module&
& File &build/bdist.linux-x86_64/egg/_mysql.py&, line 7, in &module&
& File &build/bdist.linux-x86_64/egg/_mysql.py&, line 6, in __bootstrap__
&ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
是mysql-lib无法正确加载导致的
将/usr/local/mysql/lib
下的libmysqlclient.so.16 或&&libmysqlclient.so.18
文件在/usr/lib下建立软连接
-s /usr/local/mysql/lib/libmysqlclient.so.16 /usr/lib/
-s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/
如出现:“File
exists” &!! &该文件之前指向了错误的路径 &删除之后重新建立
如果还是不好使,可能是/usr/lib没有被加载调用。添加配置:
系统要确认动态库的地址,把动态库的路径放到/etc/ld.so.conf中
建议:在/etc/ld.so.conf.d/上新建动态库相应的文件配置文件*.conf,
在该文件中填上该动态库的绝对路径
vim /etc/ld.so.conf.d/ld.mysql.so.conf
/usr/local/mysql/lib
运行 ldconfig 重建 /etc/ld.so.cache
应该就好用了
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:83636次
排名:千里之外
原创:13篇
(1)(1)(2)(3)(4)(1)(3)python版本 &=2.6或3.3
mysql版本&=4.1
可以使用pip安装也可以手动下载安装。
使用pip安装,在命令行执行如下命令:
pip&install&PyMySQL
手动安装,请先下载。下载地址:。
其中的X.X是版本(目前可以获取的最新版本是0.6.6)。
下载后解压压缩包。在命令行中进入解压后的目录,执行如下的指令:
python&setup.py&install
建议使用pip安装。
连接数据库如下:
import&pymysql.cursors
# Connect to the database
connection&=&pymysql.connect(host=&#39;127.0.0.1&#39;,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&port=3306,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&user=&#39;root&#39;,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&password=&#&#39;,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&db=&#39;employees&#39;,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&charset=&#39;utf8mb4&#39;,
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&cursorclass=pymysql.cursors.DictCursor)
也可以使用字典进行连接参数的管理,我觉得这样子更优雅一些:
import&pymysql.cursors
config&=&{
&&&&&&&&&&&#39;host&#39;:&#39;127.0.0.1&#39;,
&&&&&&&&&&&#39;port&#39;:3306,
&&&&&&&&&&&#39;user&#39;:&#39;root&#39;,
&&&&&&&&&&&#39;password&#39;:&#&#39;,
&&&&&&&&&&&#39;db&#39;:&#39;employees&#39;,
&&&&&&&&&&&#39;charset&#39;:&#39;utf8mb4&#39;,
&&&&&&&&&&&#39;cursorclass&#39;:pymysql.cursors.DictCursor,
&&&&&&&&&&}
# Connect to the database
connection&=&pymysql.connect(**config)
插入数据:
执行sql语句前需要获取cursor,因为配置默认自动提交,故在执行sql语句后需要主动commit,最后不要忘记关闭连接:
from&datetime&import&date,&datetime,&timedelta
import&pymysql.cursors
#连接配置信息
config&=&{
&&&&&&&&&&&#39;host&#39;:&#39;127.0.0.1&#39;,
&&&&&&&&&&&#39;port&#39;:3306,
&&&&&&&&&&&#39;user&#39;:&#39;root&#39;,
&&&&&&&&&&&#39;password&#39;:&#&#39;,
&&&&&&&&&&&#39;db&#39;:&#39;employees&#39;,
&&&&&&&&&&&#39;charset&#39;:&#39;utf8mb4&#39;,
&&&&&&&&&&&#39;cursorclass&#39;:pymysql.cursors.DictCursor,
&&&&&&&&&&}
# 创建连接
connection&=&pymysql.connect(**config)
# 获取明天的时间
tomorrow&=&datetime.now().date()&+&timedelta(days=1)
# 执行sql语句
&&&&with&connection.cursor()&as&cursor:
&&&&&&&&# 执行sql语句,插入记录
&&&&&&&&sql&=&&#39;INSERT INTO employees (first_name, last_name, hire_date, gender, birth_date) VALUES (%s, %s, %s, %s, %s)&#39;
&&&&&&&&cursor.execute(sql,&(&#39;Robin&#39;,&&#39;Zhyea&#39;,&tomorrow,&&#39;M&#39;,&date(1989,&6,&14)));
&&&&# 没有设置默认自动提交,需要主动提交,以保存所执行的语句
&&&&connection.commit()
&&&&connection.close();
执行查询:
import&datetime
import&pymysql.cursors
#连接配置信息
config&=&{
&&&&&&&&&&&#39;host&#39;:&#39;127.0.0.1&#39;,
&&&&&&&&&&&#39;port&#39;:3306,
&&&&&&&&&&&#39;user&#39;:&#39;root&#39;,
&&&&&&&&&&&#39;password&#39;:&#&#39;,
&&&&&&&&&&&#39;db&#39;:&#39;employees&#39;,
&&&&&&&&&&&#39;charset&#39;:&#39;utf8mb4&#39;,
&&&&&&&&&&&#39;cursorclass&#39;:pymysql.cursors.DictCursor,
&&&&&&&&&&}
# 创建连接
connection&=&pymysql.connect(**config)
# 获取雇佣日期
hire_start&=&datetime.date(1999,&1,&1)
hire_end&=&datetime.date(2016,&12,&31)
# 执行sql语句
&&&&with&connection.cursor()&as&cursor:
&&&&&&&&# 执行sql语句,进行查询
&&&&&&&&sql&=&&#39;SELECT first_name, last_name, hire_date FROM employees WHERE hire_date BETWEEN %s AND %s&#39;
&&&&&&&&cursor.execute(sql,&(hire_start,&hire_end))
&&&&&&&&# 获取查询结果
&&&&&&&&result&=&cursor.fetchone()
&&&&&&&&print(result)
&&&&# 没有设置默认自动提交,需要主动提交,以保存所执行的语句
&&&&connection.commit()
&&&&connection.close();
这里的查询支取了一条查询结果,查询结果以字典的形式返回:
从结果集中获取指定数目的记录,可以使用fetchmany方法:
result&=&cursor.fetchmany(2)
不过不建议这样使用,最好在sql语句中设置查询的记录总数。
获取全部结果集可以使用fetchall方法:
result&=&cursor.fetchall()
因为只有两条记录,所以上面提到的这两种查询方式查到的结果是一样的:
[{&#39;last_name&#39;:&&#39;Vanderkelen&#39;,&&#39;hire_date&#39;:&datetime.date(2015,&8,&12),&&#39;first_name&#39;:&&#39;Geert&#39;},&{&#39;last_name&#39;:&#39;Zhyea&#39;,&&#39;hire_date&#39;:&datetime.date(2015,&8,&21),&&#39;first_name&#39;:&&#39;Robin&#39;}]
在django中使用
在django中使用是我找这个的最初目的。目前同时支持python3.4、django1.8的数据库backend并不好找。这个是我目前找到的最好用的。
设置DATABASES和官方推荐使用的MySQLdb的设置没什么区别:
DATABASES&=&{
&&&&#39;default&#39;:&{
&&&&&&&&&#39;ENGINE&#39;:&&#39;django.db.backends.mysql&#39;,
&&&&&&&&&#39;NAME&#39;:&&#39;mytest&#39;,
&&&&&&&&&#39;USER&#39;:&&#39;root&#39;,
&&&&&&&&&#39;PASSWORD&#39;:&&#&#39;,
&&&&&&&&&#39;HOST&#39;:&&#39;127.0.0.1&#39;,
&&&&&&&&&#39;PORT&#39;:&&#39;3306&#39;,
关键是这里:我们还需要在站点的__init__.py文件中添加如下的内容:
import&pymysql
pymysql.install_as_MySQLdb()
相关文章列表:如何使用Python连接MySQL
来一个简单的例子,看Python如何操作数据库,相比Java的JDBC来说,确实非常简单,省去了很多复杂的重复工作,只关心数据的获取与操作。
需要有相应的环境和模块:
Ubuntu 14.04 64bit
Python 2.7.6
注意:Ubuntu 自带安装了Python,但是要使用Python连接数据库,还需要安装MySQLdb模块,安装方法也很简单:
sudo apt-get install MySQLdb
然后进入Python环境,import这个包,如果没有报错,则安装成功了:
Python 2.7.6 (default, Jun 22 :13)
[GCC 4.8.2] on linux2
Type &help&, &copyright&, &credits& or &license& for more information.
&&& import MySQLdb
Python标准的数据库接口的Python DB-API(包括Python操作MySQL)。大多数Python数据库接口坚持这个标准。不同的数据库也就需要不同额模块,由于我本机装的是MySQL,所以使用了MySQLdb模块,对不同的数据库而言,只需要更改底层实现了接口的模块,代码不需要改,这就是模块的作用。
Python数据库操作
首先我们需要一个测试表
建表语句:
CREATE DATABASE
DROP TABLE IF EXISTS python_
CREATE TABLE python_demo (
&id int NOT NULL AUTO_INCREMENT COMMENT '主键,自增',
&user_no int NOT NULL COMMENT '用户编号',
&user_name VARBINARY(50) NOT NULL COMMENT '用户名',
&password VARBINARY(50) NOT NULL COMMENT '用户密码',
&remark VARBINARY(255) NOT NULL COMMENT '用户备注',
&PRIMARY KEY (id,user_no)
)ENGINE =innodb DEFAULT CHARSET = utf8 COMMENT '用户测试表';
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1001,'张三01','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1002,'张三02','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1003,'张三03','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1004,'张三04','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1005,'张三05','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1006,'张三06','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1007,'张三07','admin','我是张三');
INSERT INTO python_demo(user_no, user_name, password, remark) VALUES
&(1008,'张三08','admin','我是张三');
Python代码
# --coding=utf8--
import ConfigParser
import sys
import MySQLdb
def init_db():
& conn = MySQLdb.connect(host=conf.get('Database', 'host'),
&&&&&&& user=conf.get('Database', 'user'),
&&&&&&& passwd=conf.get('Database', 'passwd'),
&&&&&&& db=conf.get('Database', 'db'),
&&&&&&& charset='utf8')
& return conn
& print &Error:数据库连接错误&
& return None
def select_demo(conn, sql):
& cursor = conn.cursor()
& cursor.execute(sql)
& return cursor.fetchall()
& print &Error:数据库连接错误&
& return None
def update_demo():
def delete_demo():
def insert_demo():
if __name__ == '__main__':
&conf = ConfigParser.ConfigParser()
&conf.read('mysql.conf')
&conn = init_db()
&sql = &select * from %s& % conf.get('Database', 'table')
&data = select_demo(conn, sql)
fetchall()字段特殊字符过滤处理
最近在做数据仓库的迁移工作,之前数据仓库的数据都是用的shell脚本来抽取,后来换了python脚本.
但是在把数据抽取存放到hadoop时,出现了一个问题:
由于数据库字段很多,提前也不知道数据库字段会存储什么内容,hive建表是以\t\n做分隔,这就导致了一个问题,如果mysql字段内容里面本身含有\t\n,那么就会出现字段错位情况,并且很头疼的是mysql有100多个字段,也不知道哪个字段会出现这个问题.
shell脚本里的做法是在需要抽取的字段上用mysql的replace函数对字段进行替换,例如,假设mysql里的字段是column1 varchar(2000),那么很可能就会出现有特殊字符的情况,在查询的sql语句里加上
select replace(replace(replace(column1,'\r',''),'\n',''),'\t','')
之前一直是这么干的,但是这样写sql特别长,特别是有100多个字段,也不知道哪个有特殊字符,只要都加上.
所以在python中对字段不加处理,最终导致hive表字段对应出现偏差,所以在python里从mysql查询到的字段在写到文件之前需要对每个字段进行过滤处理
看个例子,我就以mysql测试为例,首先建一张测试表
CREATE TABLE `filter_fields` (
&`field1` varchar(50) DEFAULT NULL,
&`field2` varchar(50) DEFAULT NULL,
&`field3` varchar(50) DEFAULT NULL,
&`field4` varchar(50) DEFAULT NULL,
&`field5` varchar(50) DEFAULT NULL,
&`field6` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;[1]&&&
【声明】:黑吧安全网()登载此文出于传递更多信息之目的,并不代表本站赞同其观点和对其真实性负责,仅适于网络安全技术爱好者学习研究使用,学习中请遵循国家相关法律法规。如有问题请联系我们,联系邮箱,我们会在最短的时间内进行处理。
上一篇:【】【】用python连接oracle(11g)数据库
<h3 id="1下载对应版本的oracle-instantclient我这里是、下载对应版本的oracle-instantclient(我这里是11.2.0.1.0)
/technetwork/database/features/instant-client/index-097480.html
rpm -ivh oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
3、配置环境变量
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib:$LD_LIBRARY_PATH
export PATH=/usr/lib/oracle/11.2/client64/bin:$PATH
4、开始你的开发(我们用python)
python连接oracle数据库需要cx_
http://sourceforge.net/projects/cx-oracle/
我下载rpm包,安装很简单
rpm -ivh cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm
3、开始你的开发吧
2.6.6 (r266:84292, Jan 22 :36)
[GCC 4.4.7
(Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&&& import cx_Oracle}

我要回帖

更多关于 oracle数据库入门教程 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信