Connect Python to the Access Database

Source: Internet
Author: User

Hey, it just took a few hours to read the python syntax and I think it's a bit familiar!
 
I personally think that python can be used to write shell. Although bat can also be used in windows, bat always feels that there are too many restrictions and it is very hard to write! Python is much more convenient!
 
I am not talking about this nonsense. Extract an article!
 
Install win32com. client URL: https://sourceforge.net/projects/pywin32/
 
 
 
The emergence of the Python programming language brings great benefits to developers. We can use this powerful object-oriented open-source language to easily implement many specific functional requirements. For example, Python is used to operate the Access database. Before using Python to operate the Access database, you must first install Python and Python for Windows extensions.
 
Step 1: Create a database connection
 
Import win32com. client
Conn = win32com. client. Dispatch (r 'ADODB. connection ')
DSN = 'provider = Microsoft. Jet. OLEDB.4.0; data source = C:/MyDB. mdb ;'
Conn. Open (DSN)
Step 2: open a record set
 
Rs = win32com. client. Dispatch (r 'ADODB. recordset ')
Rs_name = 'myrecordset' # Table Name
Rs. Open ('[' + rs_name + ']', conn, 1, 3)
Step 3 of Python's Access Database Operations
 
Rs. AddNew ()
Rs. Fields. Item (1). Value = 'data'
Rs. Update ()
Step 4: use SQL to insert or update data
 
Conn = win32com. client. Dispatch (r 'ADODB. connection ')
DSN = 'provider = Microsoft. Jet. OLEDB.4.0; data source = C:/MyDB. mdb ;'
SQL _statement = "Insert INTO [Table_Name] ([Field_1],
[Field_2]) VALUES ('data1', 'data2 ')"
Conn. Open (DSN)
Conn. Execute (SQL _statement)
Conn. Close ()
Step 5 of Python Access Database Operations
 
Rs. MoveFirst ()
Count = 0
While 1:
If rs. EOF:
Break
Else:
Countcount = count + 1
Rs. MoveNext ()
Note: If a record is empty, moving the pointer to the first record will result in an error because recordcount is invalid at this time. Solution: Set Cursorlocation to 3 before opening a record set. In this case, recordcount is valid. For example:
 
Rs. Cursorlocation = 3 # don't use parenthesis here
Rs. Open ('select * FROM [Table_Name] ', conn) # be sure conn is open
Rs. RecordCount # no parenthesis here either
The above section describes how to operate an Access database using Python.
 
 
 
 
 
Copyright of The 0th Space

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.