python操做mysql和oracle数据库

一、mysqlpython

链接数据库: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()

关闭链接:sql

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()

查询操做:oracle

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

批量插入操做:ide

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()

更新操做:fetch

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

删除表、清空表:code

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

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

二、oraclerem

链接数据库:get

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
相关文章
相关标签/搜索