最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Python對Access讀寫操作

 更新時間:2017年03月30日 17:31:44   作者:騎著螞蟻流浪  
學習Python的過程中,我們會遇到Access的讀寫問題,這時我們可以利用win32.client模塊的COM組件訪問功能,通過ADODB操作Access的文件。下面跟著小編一起來看下吧

學習Python的過程中,我們會遇到Access的讀寫問題,這時我們可以利用win32.client模塊的COM組件訪問功能,通過ADODB操作Access的文件。

需要下載安裝pywin32與AccessDatabaseEngine.exe

pywin32下載地址:http://www.fzitv.net/softs/695840.html

AccessDatabaseEngine.exe下載 http://www.fzitv.net/softs/291508.html

64位下載:http://www.fzitv.net/softs/291504.html

1、導入模塊

import win32com.client

2、建立數(shù)據(jù)庫連接

conn = win32com.client.Dispatch(r"ADODB.Connection")
DSN = 'PROVIDER = Microsoft.Jet.OLEDB.4.0;DATA SOURCE = test.mdb'
conn.Open(DSN)

3、打開一個記錄集

rs = win32com.client.Dispatch(r'ADODB.Recordset')
rs_name = 'MEETING_PAPER_INFO'
rs.Open('[' + rs_name + ']', conn, 1, 3)

4、對記錄集操作

rs.AddNew() #添加一條新記錄
rs.Fields.Item(0).Value = "data" #新記錄的第一個記錄為"data"
rs.Update() #更新

5、用SQL語句來增、刪、改數(shù)據(jù)

# 增
sql = "Insert Into [rs_name] (id, innerserial, mid) Values ('002133800088980002', 2, '21338')" #sql語句
conn.Execute(sql) #執(zhí)行sql語句
# 刪
sql = "Delete * FROM " + rs_name + " where innerserial = 2"
conn.Execute(sql)
# 改
sql = "Update " + rs_name + " Set mid = 2016 where innerserial = 3"
conn.Execute(sql)

6、遍歷記錄

rs.MoveFirst() #光標移到首條記錄
count = 0
while True:
 if rs.EOF:
 break
 else:
 for i in range(rs.Fields.Count):
 #字段名:字段內(nèi)容
 print(rs.Fields[i].Name, ":", rs.Fields[i].Value)
 count += 1
 rs.MoveNext()

7、關(guān)閉數(shù)據(jù)庫

conn.close()

補充

如果是python3好像需要用到pypyodbc

# 話不多說,碼上見分曉!

使用模塊: pypyodbc

例子和安裝詳見:

https://github.com/jiangwen365/pypyodbc/

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "loki"
import time
import pypyodbc as mdb

# 連接mdb文件
connStr = (r'Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\MDB_demo\demo.mdb;'
   r'Database=bill;'
   )
conn = mdb.win_connect_mdb(connStr)

# connStr = (
#  r'Driver={SQL Sever};'
#  r'Server=sqlserver;'
#  r'Database=bill;'
#  r'UID=sa;'
#  r'PWD=passwd'
# )
#
# conn = mdb.connect(connStr)
# 創(chuàng)建游標
cur = conn.cursor()

cur.execute('SELECT * FROM bill;')

for col in cur.description:
 # 展示行描述
 print(col[0], col[1])
result = cur.fetchall()

for row in result:
 # 展示個字段的值
 print(row)
 print(row[1], row[2]

官方給的例子mdb

# Microsoft Access DB
import pypyodbc 

connection = pypyodbc.win_create_mdb('D:\\database.mdb')

SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));'
connection.cursor().execute(SQL)
connection.close()

#SQL Server 2000/2005/2008 (and probably 2012 and 2014)

#SQL Server 2000/2005/2008 (and probably 2012 and 2014)
import pypyodbc as pyodbc # you could alias it to existing pyodbc code (not every code is compatible)
db_host = 'serverhost'
db_name = 'database'
db_user = 'username'
db_password = 'password'
connection_string = 'Driver={SQL Server};Server=' + db_host + ';Database=' + db_name + ';UID=' + db_user + ';PWD=' + db_password + ';'
db = pyodbc.connect(connection_string)
SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));'
db.cursor().execute(SQL)

# Doing a simple SELECT query
connStr = (
 r'Driver={SQL Server};'
 r'Server=sqlserver;'
 #r'Server=127.0.0.1,52865;' +
 #r'Server=(local)\SQLEXPRESS;'
 r'Database=adventureworks;'
 #r'Trusted_Connection=Yes;'
 r'UID=sa;'
 r'PWD=sapassword;'
 )
db = pypyodbc.connect(connStr)
cursor = db.cursor()

# Sample with just a raw query:
cursor.execute("select client_name, client_lastname, [phone number] from Clients where client_id like '01-01-00%'")

# Using parameters (IMPORTANT: YOU SHOULD USE TUPLE TO PASS PARAMETERS)
# Python note: a tuple with just one element must have a trailing comma, otherwise is just a enclosed variable
cursor.execute("select client_name, client_lastname, [phone number] "
"from Clients where client_id like ?", ('01-01-00%', ))

# Sample, passing more than one parameter
cursor.execute("select client_name, client_lastname, [phone number] "
"from Clients where client_id like ? and client_age < ?", ('01-01-00%', 28))

# Method 1, simple reading using cursor
while True:
 row = cursor.fetchone()
 if not row:
  break
 print("Client Full Name (phone number): ", row['client_name'] + ' ' + row['client_lastname'] + '(' + row['phone number'] + ')')

# Method 2, we obtain dict's all records are loaded at the same time in memory (easy and verbose, but just use it with a few records or your app will consume a lot of memory), was tested in a modern computer with about 1000 - 3000 records just fine...
import pprint; pp = pprint.PrettyPrinter(indent=4)
columns = [column[0] for column in cursor.description]
for row in cursor.fetchall():
 pp.pprint(dict(zip(columns, row)))

# Method 3, we obtain a list of dict's (represents the entire query)
query_results = [dict(zip([column[0] for column in cursor.description], row)) for row in cursor.fetchall()]
pp.pprint(query_results)

# When cursor was used must be closed, if you will not use again the db connection must be closed too.
cursor.close()
db.close()

How to use it without install (the latest version from here)

Just copy the latest pypyodbc.py downloaded from this repository on your project folder and import the module.

Install
If you have pip available (keep in mind that the version on pypi may be old):

pip install pypyodbc

Or get the latest pypyodbc.py script from GitHub (Main Development site)

python setup.py install

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • Python下利用BeautifulSoup解析HTML的實現(xiàn)

    Python下利用BeautifulSoup解析HTML的實現(xiàn)

    這篇文章主要介紹了Python下利用BeautifulSoup解析HTML的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • tensorflow 報錯unitialized value的解決方法

    tensorflow 報錯unitialized value的解決方法

    今天小編就為大家分享一篇tensorflow 報錯unitialized value的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python中使用md5sum檢查目錄中相同文件代碼分享

    Python中使用md5sum檢查目錄中相同文件代碼分享

    這篇文章主要介紹了Python中使用md5sum檢查目錄中相同文件代碼分享,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
    2015-02-02
  • Python操作dict時避免出現(xiàn)KeyError的幾種解決方法

    Python操作dict時避免出現(xiàn)KeyError的幾種解決方法

    這篇文章主要介紹了Python操作dict時避免出現(xiàn)KeyError的幾種解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Python 占位符的使用方法詳解

    Python 占位符的使用方法詳解

    這篇文章主要介紹了Python 占位符的使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • python實現(xiàn)橫向拼接圖片

    python實現(xiàn)橫向拼接圖片

    這篇文章主要為大家詳細介紹了python實現(xiàn)橫向拼接圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 關(guān)于Python自動化操作Excel

    關(guān)于Python自動化操作Excel

    這篇文章主要介紹了關(guān)于Python自動化操作Excel, Python 是一種功能強大的編程語言,可以用于許多任務,包括處理 Excel 文件,需要的朋友可以參考下
    2023-04-04
  • NumPy矩陣乘法的實現(xiàn)

    NumPy矩陣乘法的實現(xiàn)

    本文主要介紹了NumPy矩陣乘法的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Python OS系統(tǒng)解決路徑中空格原因?qū)е挛募虿婚_的問題

    Python OS系統(tǒng)解決路徑中空格原因?qū)е挛募虿婚_的問題

    這篇文章主要介紹了Python OS系統(tǒng)解決路徑中空格原因?qū)е挛募虿婚_的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python對多屬性的重復數(shù)據(jù)去重實例

    Python對多屬性的重復數(shù)據(jù)去重實例

    下面小編就為大家分享一篇Python對多屬性的重復數(shù)據(jù)去重實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評論

甘德县| 农安县| 钦州市| 越西县| 广德县| 会同县| 民勤县| 多伦县| 曲沃县| 咸阳市| 宜兰市| 霍林郭勒市| 辽阳市| 黄山市| 渑池县| 奈曼旗| 天台县| 鄂伦春自治旗| 常德市| 华蓥市| 阳朔县| 四会市| 资源县| 乌拉特前旗| 元谋县| 沙洋县| 将乐县| 霍州市| 马公市| 威信县| 宁远县| 万安县| 兴隆县| 清涧县| 枣庄市| 满城县| 菏泽市| 南江县| 诸暨市| 格尔木市| 永修县|