一、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