Learning the process of Python, we will encounter Access read and write problems, then we can use the module's COM component access function, through the ADODB operation Access files.
You need to download and install pywin32 with
pywin32 download address:https:///softs/
downloadinghttps:///softs/
64-bit download:https:///softs/
1. Import module
import
2. Establishment of database connection
conn = (r"") DSN = 'PROVIDER = .4.0;DATA SOURCE = ' (DSN)
3. Open a record set
rs = (r'') rs_name = 'MEETING_PAPER_INFO' ('[' + rs_name + ']', conn, 1, 3)
4. Operation on record sets
() # Add a new record (0).Value = "data" #The first record of the new record is "data" # () #update
5, with SQL statements to add, delete, change data
# Increase sql = "Insert Into [rs_name] (id, innerserial, mid) Values ('002133800088980002', 2, '21338')" #sql statement (sql) # Execute sql statement # Delete sql = "Delete * FROM " + rs_name + " where innerserial = 2" (sql) # Change sql = "Update " + rs_name + " Set mid = 2016 where innerserial = 3" (sql)
6. Traversing the record
() # Move the cursor to the first record count = 0 while True: if : break else: for i in range(): # Field name: field content print([i].Name, ":", [i].Value) count += 1 ()
7. Closing the database
()
replenishment
If it's python3, it looks like you need to use pypyodbc.
# Without further ado, I'll see you on the yard. #
Module used: pypyodbc
See examples and installation details:
/jiangwen365/pypyodbc/
#!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" import time import pypyodbc as mdb # Connecting to mdb files connStr = (r'Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\MDB_demo\;' 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 = (connStr) # Create cursors cur = () ('SELECT * FROM bill;') for col in : # Display line descriptions print(col[0], col[1]) result = () for row in result: # Demonstrate the value of a field print(row) print(row[1], row[2]
The official example given mdb
# Microsoft Access DB import pypyodbc connection = pypyodbc.win_create_mdb('D:\\') SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));' ().execute(SQL) ()
#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 = (connection_string) SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,product_name VARCHAR(25));' ().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 = (connStr) cursor = () # Sample with just a raw query: ("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 ("select client_name, client_lastname, [phone number] " "from Clients where client_id like ?", ('01-01-00%', )) # Sample, passing more than one parameter ("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 = () 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 = (indent=4) columns = [column[0] for column in ] for row in (): (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 ], row)) for row in ()] (query_results) # When cursor was used must be closed, if you will not use again the db connection must be closed too. () ()
How to use it without install (the latest version from here)
Just copy the latest 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 script from GitHub (Main Development site)
python install
The above is the whole content of this article, I hope that the content of this article can bring some help to your study or work, but also hope that more support me!