Python資料庫操作

來源:互聯網
上載者:User

標籤:匹配   索引   values   google   建立   log   ini   前言   turn   

前言

上一次的文章實現了資料庫的串連,當前文章實現資料庫的操作

1.pyodbc官方文檔
2.參考文章

串連資料庫

獲得當前工作目錄

import oscurrent_folder=os.getcwd()

產生資料庫檔案路徑

path=current_folder+‘\Output\Test.mdb‘

資料庫連接

def __init__(self,path):    self.conn = pyodbc.connect(r"Driver={Driver do Microsoft Access (*.mdb)};DBQ=" + path + ";Uid=;Pwd=;")      self.cursor = self.conn.cursor()

執行sql語句

def run_sql(self,sql):    self.cursor.execute(sql) 

退出程式

    def close(self):        #提交所有執行語句        self.conn.commit()          self.cursor.close()          self.conn.close()
表格操作

建立表格

def create_table(self,name,key):    sql="Create table "+name+"("+key+")"    self.run_sql(sql) 

複製表格

def coppy_table(self,copy_table,new_table):    sql="select * into "+new_table+" from "+copy_table+" where 1<>1"    self.run_sql(sql)

刪除表格

def drop_table(self,name):    sql="drop table "+name    self.run_sql(sql)

擷取表格列首

def get_table_column(self,table):    colunm_list=[]    backdata=self.cursor.columns(table)     for row in backdata:        colunm_list.append(row.column_name)    return colunm_list
資料操作

查詢資料

    def select(self,table_name,key,codition):        sql="SELECT "+key+" from "+table_name+" WHERE "+ codition        self.cursor.execute(sql)        backdata=self.cursor.fetchall()        return backdata

插入資料

    def insert(self,table_name,column_list,value_list):        colunm_name_str=‘(‘        value_str=‘(‘        for index in range(0,len(column_list)):            colunm_name_str=colunm_name_str+column_list[index]+‘,‘            value_str=value_str+‘\‘‘+str(value_list[index])+‘\‘,‘        colunm_name_str=colunm_name_str[:-1]+‘)‘        value_str=value_str[:-1]+‘)‘        sql=‘INSERT INTO %s %s VALUES %s‘%(table_name,colunm_name_str,value_str)        self.run_sql(sql)
附錄

附上相關函數的使用方法,僅供參考

1. connection 對象方法
close():關閉資料庫
commit():提交當前事務
rollback():取消當前事務
cursor():擷取當前串連的遊標
errorhandler()作為已給遊標的控制代碼
2. cursor遊標對象和方法
arrysize(): 使用fetchmany()方法時一次取出的記錄數,預設為1
connection():建立此遊標的串連
discription():返回遊標的活動狀態,包括(7要素)(name,type_code,display_size,internal_size,precision,scale,null_ok)其中name,type_code是必須的
lastrowid():返回最後更新行的id,如果資料庫不支援,返回none.
rowcount():最後一次execute()返回或者影響的行數
callproc():調用一個預存程序
close():關閉遊標
execute():執行sql語句或者資料庫命令
executemany():一次執行多條sql語句
fetchone():匹配結果的下一行
fetchall():匹配所有剩餘結果
fetchmany(size-cursor,arraysize):匹配結果的下幾行
iter():建立迭代對象(可選,參考next())
messages():遊標執行好資料庫返回的資訊列表(元組集合)
next():使用迭代對象得到結果的下一行
nextset():移動到下一個結果集
rownumber():當前結果集中遊標的索引(從0行開始)
setinput-size(sizes):設定輸入的最大值
setoutput-size(sizes[,col]):設定列輸出的緩衝值



來自為知筆記(Wiz)

Python資料庫操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.