精华内容
下载资源
问答
  • 学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件。1、导入模块import win32com.client2、建立数据库连接conn = win32...

    学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件。

    1、导入模块

    import win32com.client

    2、建立数据库连接

    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语句来增、删、改数据

    # 增

    sql = "Insert Into [rs_name] (id, innerserial, mid) Values ('002133800088980002', 2, '21338')" #sql语句

    conn.Execute(sql) #执行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):

    #字段名:字段内容

    print(rs.Fields[i].Name, ":", rs.Fields[i].Value)

    count += 1

    rs.MoveNext()

    7、关闭数据库

    conn.close()

    以上就是使用Python对Access读写操作方法详解的详细内容,更多请关注php教程其它相关文章!

    展开全文
  • First I'm on Mac OSX 10.12.6.I want to embed a python environment in my C application.I take the github python project at ...

    First I'm on Mac OSX 10.12.6.

    I want to embed a python environment in my C application.

    I take the github python project at https://github.com/python/cpython and manage to compile it with it's configure file and the command lines :

    env LINKFORSHARED="-Wl,-stack_size,1000000 -framework CoreFoundation"

    LDFLAGS="-Wl,-syslibroot,$SDK -arch i386"

    CFLAGS="-Os -isysroot $SDK -arch i386"

    ./configure MACOSX_DEPLOYMENT_TARGET=10.6 --disable-shared --prefix=$PYTHON_PATH --exec-prefix=$PYTHON_PATH -build=i386-darwin

    make

    make altinstall

    ($SDK point to MacOSX.sdk inside Xcode.app)

    it gives me a libPython.a for 32bits then I redo it for 64bits and merge both with

    lipo -create libPython32.A libPython64.a libPython32-64.a

    In my XCode project, on .xconfig file, I import the lib and headers files with :

    OTHER_LDFLAGS = $(inherited) -lpython32-64;

    USER_HEADER_SEARCH_PATHS = $(inherited) $(LIBS)/cpython35/include

    LIBRARY_SEARCH_PATHS = $(inherited) $(LIBS)/cpython35/lib;

    Everything is found and my project compiles, no problem seen.

    And then in my python.c file (I reduced my path for this article, that's why '...') :

    wchar_t *stdProgramName = L".../LIBs/cpython35/bin/python3.5";

    Py_SetProgramName(stdProgramName);

    wchar_t *stdPythonHome = L".../LIBs/cpython35";

    Py_SetPythonHome(stdPythonHome);

    wchar_t *stdlib = L".../LIBs/cpython35/lib/python3.5.zip:.../LIBs/cpython35/lib/python3.5:.../LIBs/cpython35/lib/python3.5/plat-darwin:.../LIBs/cpython35/lib/python3.5/lib-dynload:.../LIBs/cpython35/lib/python3.5/site-packages";

    Py_SetPath(stdlib);

    Py_Initialize();

    // Run something

    PyRun_SimpleString("import sys; print(sys.path)");

    //To this line it's work fine, all path are correcte but then

    PyRun_SimpleString("import math;");

    Py_Finalize();

    the import of the math librarie does not work, it gives me :

    Traceback (most recent call last):

    File "", line 1, in

    ImportError: dlopen(.../LIBs/cpython35/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so, 2): Symbol not found: _PyExc_MemoryError

    Referenced from: .../LIBs/cpython35/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so

    Expected in: flat namespace

    in .../LIBs/cpython35/lib/python3.5/lib-dynload/math.cpython-35m-darwin.so

    I'm kinda stuck at this point searching a solution for this import !!!

    解决方案

    In the end, I didn't need my python lib to be in a static library

    So '--disable-shared' was the problem, I switched it with '--enable-shared'.

    With this change, I get a libpython32.dylib, and that lib can access math.cpython-35m-darwin.so...

    My process didn't change that much. In my project, I deleted -lpython32-64 in OTHER_LDFLAGS, and include libpython.dylib to "Link Binary With Libraries".

    And it works.

    展开全文
  • 学习Python的过程中,我们会遇到Access的读写问题,这时我们可以利用win32.client模块的COM组件访问功能,通过ADODB操作Access的文件。 需要下载安装pywin32与AccessDatabaseEngine.exe pywin32下载地址:...
  • Python读取Access数据库基本操作

    万次阅读 多人点赞 2019-09-17 08:38:36
    接上篇《python操作Access .mdb数据库环境配置》配置好的环境,进行Access数据库基本的操作。 #导入模块 import pypyodbc import win32com.client path=r'C:\Users\Administrator\Desktop\test.mdb'# 数据库文件 ...

    接上篇《python操作Access .mdb数据库环境配置》配置好的环境,进行Access数据库基本的操作。 


    #导入模块

    import pypyodbc
    import win32com.client
    path=r'C:\Users\Administrator\Desktop\test.mdb'# 数据库文件

    #获取单个表结构信息

    con = win32com.client.Dispatch(r'ADODB.Connection')
    DSN = 'PROVIDER=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=' + path + ';'
    con.Open(DSN)
    rs = win32com.client.Dispatch(r'ADODB.Recordset')
    rs.Cursorlocation = 3
    rs.Open('SELECT TOP 1 * FROM table1', con)
    for i in range(0, rs.Fields.Count):
        print(rs.Fields[i].Name + ' - ' + str(rs.Fields[i].Type) + ' - ' + str(rs.Fields[i].DefinedSize))

    #获取所有表名称 

    conn = pypyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + path + ";Uid=;Pwd=;")
    cursor = conn.cursor()
    SQL = "SELECT name FROM MSYSOBJECTS WHERE TYPE=1 AND NAME NOT LIKE 'Msys*'"#
    for row in cursor.execute(SQL):
        print(row)

     #获取某个表数据内容

    SQL_value = 'select * from admin'#
    for row in cursor.execute(SQL_value):
        print(row)

    #获取统计表数据 

    SQL_value = 'select count(*) from admin'#
    cursor.execute(SQL_value)
    SUM=cursor.fetchall()[0][0]
    print(SUM)

    #修改表中数 

    edit_SQL='update table1 set name=\'逍遥子\''
    cursor.execute(edit_SQL)
    cursor.commit()

    #清空一列数据 

    clear_SQL='update table2 set name = null'
    cursor.execute(clear_SQL)
    cursor.commit()

    #删除表字段 

    delete_field_SQL='alter table table3 drop column name'
    cursor.execute(delete_field_SQL)
    cursor.commit()

    #增加表字段 

    add_field_SQL='alter table table4 add column AAA text '
    cursor.execute(add_field_SQL)
    cursor.commit()

    #清空表 

    qingkong_SQL='delete * from table5'
    cursor.execute(qingkong_SQL)
    cursor.commit()
    conn.close()

     

    展开全文
  • Python连接Access数据库详细步骤

    千次阅读 2021-01-22 14:56:11
    Python连接Access数据库详细步骤 Access数据库是微软使用的数据库,可以使用微软的软件Access直接操作。 如果需要连接Access数据库,需要分别在AccessPython侧执行一些操作 Part1 电脑端配置 电脑使用WIN10 64位...

    Python连接Access数据库详细步骤

    Access数据库是微软使用的数据库,可以使用微软的软件Access直接操作。
    如果需要连接Access数据库,需要分别在Access和Python侧执行一些操作

    Part1 电脑端配置

    电脑使用WIN10 64位系统,需要安装Access驱动,创建Access表格

    1. 安装Access驱动

    • step1 在官网下载对应的Access驱动程序并双击安装,链接地址https://www.microsoft.com/en-us/download/details.aspx?id=13255
      在这里插入图片描述

    • 点击 download,选择需要下载的驱动,点击NEXT

    • 双击下载好的文件安装,按操作步骤一步步安装完成。需特别注意:不要从其它地方拷贝安装文件,否则安装不了,每台电脑都需要单独下载。

    • 创建Access数据库:打开Access数据库,新建一个空白数据库。在这里插入图片描述

    • 打开ODBC Data Source配置Access驱动在这里插入图片描述

    • 选择Add添加一个Microsoft Access Driver,点击finish在这里插入图片描述

    • 点击选择按钮选择创建好的数据库文件(step4创建好的)然后点击确定。在这里插入图片描述

    Part2 python代码步骤

    • step1,在python端安装pypyodbc在这里插入图片描述
    • step2,创建连接数据库的子程序
    import pypyodbc # 导入pypyodbc模块
    
    def accdb_conn(db_name,password=""): 子程序
     """
     :param db_name:s数据库名称
     :param password:数据库密码
     :return:返回数据库连接
     """
     str = 'Driver={Microsoft Access Driver (*.mdb,*.accdb)};PWD' + password + ";DBQ=" + db_name # 创建连接数据
     conn = pypyodbc.win_connect_mdb(str)
     return conn
    
    • step3,建立数据库的连接。
    database_path = 'E:\xxxx.accdb' # database_path 是数据库的路径
    conn = Access_connection.accdb_conn(database_path)  # database_path 是数据库路径
    curser = conn.cursor() 
    
    • step4,打印数据库内的所有表名.
    for table_info in curser.tables(tableType='TABLE'):
        tableList.append(table_info[2])
    print(tableList)
    

    打印完成后可以显示所有表格内容在这里插入图片描述

    其它操作步骤参照MYSQL的操作

    展开全文
  • 导入语句 pythonWhile learning programming and reading some resources you’d have come across this word ‘abstraction’ which simply means to reduce and reuse the code as much as possible. 在学习编程和...
  • 看书上通过ODBC访问数据库的案例,想实践一下在Python 3.6.1中实现access2003数据库的链接,但是在导入odbc模块的时候出现了问题,后来查了一些资料就尝试着使用pypyodbc,最后成功了。操作步骤:①安装pypyodbc目前...
  • 在写 Python 项目的时候,我们可能经常会遇到导入模块失败的错误:ImportError: No module named 'xxx' 或者 ModuleNotFoundError: No module named 'xxx' 。导入失败问题,通常分为两种:一种是导入自己写的模块(即...
  • python读取access文件并入库

    万次阅读 2016-02-26 15:44:45
    Python读取access文件时和读取Excel文件不是很一样,当然用的工具也不一样,在读取excel中的数据时用的是xlrd,而读取access文件时用的则是pypyodbc。 简要安装过程:1、首先要安装access驱动(AccessRuntime_x64_...
  • python 黑盒测试Turns out, this does not work reliably, in fact it will only work when packages are involved. I originally wrote the core for Flask extensions and it appeared to work, but I never ...
  • I am trying to figure out a way to create a program that allows me to find the best combination of data based on several different factors.I have a Microsoft Access file with creature data in it....
  • 做为python初学者,有时候搞不清楚导入模块的作用。直接导入模块通常模块为一个文件,直接使用import来导入就好了。可以作为module的文件类型有".py"、".pyo"、".pyc"、".pyd"、".so"、".dll"。形式:import modname...
  • I have Eclipse working and am working uder the Pydev plugin, and I need some help with ... I'm on a mac and have managed to get pygame working for python 3.3 using homebrew, and pydev's causing me...
  • I have a bunch of Python modules I want to clean up, reorganize and refactor (there's some duplicate code, some unused code ...), and I'm wondering if there's a tool to make a map of which module uses...
  • 原标题:Python判断文件是否存在的三种方法通常在读写文件之前,需要判断文件或目录是否存在,不然某些处理方法可能会使程序出错。所以最好在做任何操作之前,先判断文件是否存在。这里将介绍三种判断文件或文件夹...
  • Python读取Access数据库

    万次阅读 2004-07-29 15:37:00
    使用用PYTHON语言进行...本文文们将使用ADO(ActiveX Data Object)来实现读取Access数据库(MDB)文件! 首先在IDE中(本文为PYTHONWIN),引用 Microsoft ActiveX Data Objects Library,点击Tools(工具)------>COM M
  • Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs.I have a file in Matlab...
  • Following this question which asks (and answers) how to read .mat files that were created in Matlab using Scipy, I want to know how to access the fields in the imported structs.I have a file in Matlab...
  • 下面是Python源代码,当然,这只是我的一个示例,读者在实际应用中可能还需要根据实际情况作一些更改。 #-*-coding:utf-8-*- importwin32com.clientaswc,MySQLdbasmysqlimporttypes,sys,re,...
  • I have installed ipython in my virtualenv, so python manage.py shell gives my ... However, I can't access imports from inside function definitions:$ python manage.py shellPython 2.7.5 (default, S...
  • python导入同级目录下文件报错问题

    万次阅读 2017-11-23 10:42:04
    python 在import同级目录下文件会出现找不到module问题 正确解决方法为在导入之前手动添加目录,添加如下代码 current_dir = os.path.abspath(os.path.dirname(__file__)) sys.path.append(current_dir) sys....
  • python导入模块

    2020-08-22 02:42:32
    Python导入模块 (Importing Modules in Python) 模组 (Modules) A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Modules ...
  • Python will by default be looking for modules from lib/python2.7/site-packagesBut I'm now trying to import packages from other directories on HPCC, which I have to rely on, because I've no root access...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 18,554
精华内容 7,421
关键字:

python导入access

python 订阅