python操作mysql和oracle数据库

1、mysql

连接数据库:

def open_spider(self, spider):
    self.conn = MySQLdb.connect(host="localhost", user="username", passwd="password",db="databasename",charset="utf8")
    self.cursor = self.conn.cursor()

关闭连接:

def close_spider(self, spider):
    #print "close_spider............................"
    self.cursor.close()
    self.conn.close()

创建表(主键自增):

self.cursor.execute("create table tablename(pid number(15) primary key auto_increment, id number(6) not null, name varchar2(30) not null)")
self.conn.commit()

查询操作:

def getCompID(self):
    self.cursor.execute("select * from tablename")
    self.conn.commit()
    self.compIDList = self.cursor.fetchall()

批量插入操作:

tableprop = "tablename (id, name) values(%s, %s)"
bulkdata = []

def bulk_insert_to_mysql(self, bulkdata, tableprop):
    try:
        sql = "insert into "+tableprop
        self.cursor.executemany(sql, bulkdata) #bulkdata的结构是列表
        self.conn.commit()
    except:
        self.conn.rollback()

更新操作:

updateprop = "update tablename set time = '%s' where comp = 'sse'" % (temptime)
self.cursor.execute(updateprop)
self.conn.commit()

删除表、清空表:

self.cursor.execute("delete from tablename where id = 'cn'")
self.conn.commit()

self.cursor.execute("truncate table tablename")
self.conn.commit()

2、oracle

连接数据库:

import cx_Oracle

def open_spider(self, spider):
    #print "open_spider............................"
    self.conn = cx_Oracle.connect('username', 'password', 'id:port/databasename') #访问远程oracle方案
    self.cursor = self.conn.cursor()

批量插入操作:

#插入到主键自增的表中,不用管主键,只需依次插入其他
tableprop = "insert into tablename(id, name) values(:ID, :NAME)"
bulkdata = []

def bulk_insert_to_mysql(self, bulkdata, tableprop):
    try:
        self.cursor.executemany(tableprop, bulkdata)
        self.conn.commit()
    except Exception as e:
        print "insert error!"
        print 'repr(e):', repr(e)

更新表操作:

#单字段更新
updateprop = "update tablename set time = :time where comp = 'aa'"
self.cursor.execute(updateprop, {'time':temptime})
#多字段更新
updateprop = "update tablename set time = :time, atime = :atime where comp = 'aa'"
self.cursor.execute(updateprop, {'time':temptime, 'atime':atime})

创建表操作(主键自增):

self.cursor.execute("create table tablename(pid number(15) primary key, id number(6) not null, name varchar2(30) not null)")
self.conn.commit()
self.cursor.execute("CREATE SEQUENCE table_Sequence INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE")
self.conn.commit()
self.cursor.execute("CREATE TRIGGER book_increase BEFORE insert ON tablename FOR EACH ROW begin select BOOK_SEQUENCE.nextval into :New.pid from dual;end;") #此处结尾分号不能省
self.conn.commit()

查询表是否存在:

def table_exists(self, con, table_name):
    sql = "select table_name from all_tables"
    con.execute(sql)
    tables = con.fetchall()
    for table in tables:
        temptable = str(table[0]).lower() #查出来的表名有大写有小写
        if  temptable == table_name:
            return 1
    return 0

 

转载于:https://my.oschina.net/u/3636678/blog/2244805

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值