python连接数据库测试

本文展示了一个使用Python语言通过cx_Oracle模块连接Oracle数据库,创建表、插入数据并进行查询的基本示例。代码包括了数据库连接、DDL操作、DML操作和数据查询等步骤。
#!/usr/bin/python
#coding=utf-8
import cx_Oracle
import sys
import urllib
import os

def connectDB(dbname='sms'):
if dbname == 'sms':
connstr='system/oracle@192.168.56.21:1521/sms'
db=cx_Oracle.connect(connstr)
return db

def sqlSelect(sql,db):
#include:select
cr=db.cursor()
cr.execute(sql)
rs=cr.fetchall()
cr.close
return rs

def sqlDML(sql,db):
#include:inesrt,update,deleter
cr=db.cursor()
cr.execute(sql)
cr.close()
db.commit

def sqlDDL(sql,db):
#include:create
cr=db.cursor()
cr.execute(sql)
cr.close

if __name__=='__main__':
print "This is a test python program,write by lsq!\n"

#connect to database
db=connectDB()

#create table:
sql="create table test as select rownum id,lpad(rownum,10,'x') comm from dual connect by level<=100"
sqlDDL(sql,db)

#insert data to table test
sql="insert into test select rownum,lpad(rownum,10,'x') from dual connect by level<=100"
sqlDML(sql,db)

#select th result:
print "this is the first time select the data from test"
sql='select * from test'
rs=sqlSelect(sql,db)
for x in rs:
print x

oracle@bjmbsdb01[/home/oracle]python testdb.py
This is a test python program,write by lsq!


this is the first time select the data from test
(1, 'xxxxxxxxx1')
(2, 'xxxxxxxxx2')
(3, 'xxxxxxxxx3')
(4, 'xxxxxxxxx4')
(5, 'xxxxxxxxx5')
(6, 'xxxxxxxxx6')
(7, 'xxxxxxxxx7')
(8, 'xxxxxxxxx8')
(9, 'xxxxxxxxx9')
(10, 'xxxxxxxx10')
(11, 'xxxxxxxx11')
(12, 'xxxxxxxx12')
(13, 'xxxxxxxx13')
(14, 'xxxxxxxx14')
(15, 'xxxxxxxx15')
(16, 'xxxxxxxx16')
(17, 'xxxxxxxx17')
(18, 'xxxxxxxx18')
(19, 'xxxxxxxx19')
(20, 'xxxxxxxx20')
(21, 'xxxxxxxx21')
(22, 'xxxxxxxx22')
(23, 'xxxxxxxx23')
(24, 'xxxxxxxx24')
(25, 'xxxxxxxx25')
(26, 'xxxxxxxx26')
(27, 'xxxxxxxx27')
(28, 'xxxxxxxx28')
(29, 'xxxxxxxx29')
(30, 'xxxxxxxx30')
(31, 'xxxxxxxx31')
(32, 'xxxxxxxx32')
(33, 'xxxxxxxx33')
(34, 'xxxxxxxx34')
(35, 'xxxxxxxx35')
(36, 'xxxxxxxx36')
(37, 'xxxxxxxx37')
(38, 'xxxxxxxx38')
(39, 'xxxxxxxx39')
(40, 'xxxxxxxx40')
(41, 'xxxxxxxx41')
(42, 'xxxxxxxx42')
(43, 'xxxxxxxx43')
(44, 'xxxxxxxx44')
(45, 'xxxxxxxx45')
(46, 'xxxxxxxx46')
(47, 'xxxxxxxx47')
(48, 'xxxxxxxx48')
(49, 'xxxxxxxx49')
(50, 'xxxxxxxx50')
(51, 'xxxxxxxx51')
(52, 'xxxxxxxx52')
(53, 'xxxxxxxx53')
(54, 'xxxxxxxx54')
(55, 'xxxxxxxx55')
(56, 'xxxxxxxx56')
(57, 'xxxxxxxx57')
(58, 'xxxxxxxx58')
(59, 'xxxxxxxx59')
(60, 'xxxxxxxx60')
(61, 'xxxxxxxx61')
(62, 'xxxxxxxx62')
(63, 'xxxxxxxx63')
(64, 'xxxxxxxx64')
(65, 'xxxxxxxx65')
(66, 'xxxxxxxx66')
(67, 'xxxxxxxx67')
(68, 'xxxxxxxx68')
(69, 'xxxxxxxx69')
(70, 'xxxxxxxx70')
(71, 'xxxxxxxx71')
(72, 'xxxxxxxx72')
(73, 'xxxxxxxx73')
(74, 'xxxxxxxx74')
(75, 'xxxxxxxx75')
(76, 'xxxxxxxx76')
(77, 'xxxxxxxx77')
(78, 'xxxxxxxx78')
(79, 'xxxxxxxx79')
(80, 'xxxxxxxx80')
(81, 'xxxxxxxx81')
(82, 'xxxxxxxx82')
(83, 'xxxxxxxx83')
(84, 'xxxxxxxx84')
(85, 'xxxxxxxx85')
(86, 'xxxxxxxx86')
(87, 'xxxxxxxx87')
(88, 'xxxxxxxx88')
(89, 'xxxxxxxx89')
(90, 'xxxxxxxx90')
(91, 'xxxxxxxx91')
(92, 'xxxxxxxx92')
(93, 'xxxxxxxx93')
(94, 'xxxxxxxx94')
(95, 'xxxxxxxx95')
(96, 'xxxxxxxx96')
(97, 'xxxxxxxx97')
(98, 'xxxxxxxx98')
(99, 'xxxxxxxx99')
(100, 'xxxxxxx100')
(1, 'xxxxxxxxx1')
(2, 'xxxxxxxxx2')
(3, 'xxxxxxxxx3')
(4, 'xxxxxxxxx4')
(5, 'xxxxxxxxx5')
(6, 'xxxxxxxxx6')
(7, 'xxxxxxxxx7')
(8, 'xxxxxxxxx8')
(9, 'xxxxxxxxx9')
(10, 'xxxxxxxx10')
(11, 'xxxxxxxx11')
(12, 'xxxxxxxx12')
(13, 'xxxxxxxx13')
(14, 'xxxxxxxx14')
(15, 'xxxxxxxx15')
(16, 'xxxxxxxx16')
(17, 'xxxxxxxx17')
(18, 'xxxxxxxx18')
(19, 'xxxxxxxx19')
(20, 'xxxxxxxx20')
(21, 'xxxxxxxx21')
(22, 'xxxxxxxx22')
(23, 'xxxxxxxx23')
(24, 'xxxxxxxx24')
(25, 'xxxxxxxx25')
(26, 'xxxxxxxx26')
(27, 'xxxxxxxx27')
(28, 'xxxxxxxx28')
(29, 'xxxxxxxx29')
(30, 'xxxxxxxx30')
(31, 'xxxxxxxx31')
(32, 'xxxxxxxx32')
(33, 'xxxxxxxx33')
(34, 'xxxxxxxx34')
(35, 'xxxxxxxx35')
(36, 'xxxxxxxx36')
(37, 'xxxxxxxx37')
(38, 'xxxxxxxx38')
(39, 'xxxxxxxx39')
(40, 'xxxxxxxx40')
(41, 'xxxxxxxx41')
(42, 'xxxxxxxx42')
(43, 'xxxxxxxx43')
(44, 'xxxxxxxx44')
(45, 'xxxxxxxx45')
(46, 'xxxxxxxx46')
(47, 'xxxxxxxx47')
(48, 'xxxxxxxx48')
(49, 'xxxxxxxx49')
(50, 'xxxxxxxx50')
(51, 'xxxxxxxx51')
(52, 'xxxxxxxx52')
(53, 'xxxxxxxx53')
(54, 'xxxxxxxx54')
(55, 'xxxxxxxx55')
(56, 'xxxxxxxx56')
(57, 'xxxxxxxx57')
(58, 'xxxxxxxx58')
(59, 'xxxxxxxx59')
(60, 'xxxxxxxx60')
(61, 'xxxxxxxx61')
(62, 'xxxxxxxx62')
(63, 'xxxxxxxx63')
(64, 'xxxxxxxx64')
(65, 'xxxxxxxx65')
(66, 'xxxxxxxx66')
(67, 'xxxxxxxx67')
(68, 'xxxxxxxx68')
(69, 'xxxxxxxx69')
(70, 'xxxxxxxx70')
(71, 'xxxxxxxx71')
(72, 'xxxxxxxx72')
(73, 'xxxxxxxx73')
(74, 'xxxxxxxx74')
(75, 'xxxxxxxx75')
(76, 'xxxxxxxx76')
(77, 'xxxxxxxx77')
(78, 'xxxxxxxx78')
(79, 'xxxxxxxx79')
(80, 'xxxxxxxx80')
(81, 'xxxxxxxx81')
(82, 'xxxxxxxx82')
(83, 'xxxxxxxx83')
(84, 'xxxxxxxx84')
(85, 'xxxxxxxx85')
(86, 'xxxxxxxx86')
(87, 'xxxxxxxx87')
(88, 'xxxxxxxx88')
(89, 'xxxxxxxx89')
(90, 'xxxxxxxx90')
(91, 'xxxxxxxx91')
(92, 'xxxxxxxx92')
(93, 'xxxxxxxx93')
(94, 'xxxxxxxx94')
(95, 'xxxxxxxx95')
(96, 'xxxxxxxx96')
(97, 'xxxxxxxx97')
(98, 'xxxxxxxx98')
(99, 'xxxxxxxx99')
(100, 'xxxxxxx100')

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-1434283/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10972173/viewspace-1434283/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值