导航:首页 > 编程语言 > python读取电脑odbc驱动

python读取电脑odbc驱动

发布时间:2022-09-26 13:12:17

‘壹’ 如何用 python 读取硬件信息

python是脚本语言,不能读取硬件信息,

你可以用编译语言开发一个python的脚本模块,然后用脚本调用

‘贰’ python 怎么调用odbc

入门
连接到数据库
调用connect方法并传入ODBC连接字符串,其会返回一个connect对象。通过connect对象,调用cursor()方法,可以获取一个游标cursor。如下代码示例:
import pyodbc
#连接示例: Windows系统, 非DSN方式, 使用微软 SQL Server 数据库驱动
cnxn =pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass')
#连接示例: Linux系统, 非DSN方式, 使用FreeTDS驱动
cnxn =pyodbc.connect('DRIVER={FreeTDS};SERVER=localhost;PORT=1433;DATABASE=testdb;UID=me;PWD=pass;TDS_Version=7.0')
#连接示例:使用DSN方式
cnxn = pyodbc.connect('DSN=test;PWD=password')
# 打开游标
cursor =cnxn.cursor()
以上示例只是标准示例,具体的ODBC连接字符串以你自己使用的驱动为准。
查询一些数据
所有SQL语句都使用Cursor.execute()方法执行。比如select语句会返回一些结果行,你可以使用游标(Cursor)相关的函数功能(fetchone,fetchall,fetchmany)对结果进行检索。
Cursor.fetchone 用于返回一个单行( Row)对象:
cursor.execute("selectuser_id, user_name from users")
row =cursor.fetchone()
if row:
print(row)
Row 对象是类似一个python元组(tuples),不过也可以通过列名称来访问,例如:
cursor.execute("selectuser_id, user_name from users")
row =cursor.fetchone()
print('name:',row[1]) # 使用列索引号来访问数据
print('name:',row.user_name) # 或者直接使用列名来访问数据
当所有行都已被检索,则fetchone返回None.
while 1:
row = cursor.fetchone()
if not row:
break
print('id:', row.user_id)
Cursor.fetchall方法返回所有剩余行并存储于一个列表中。如果没有行,则返回一个空列表。(注意:如果有很多行,会造成大量内存占用。Fetchall会一次性将所有数据查询到本地,然后再遍历)
cursor.execute("selectuser_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
print(row.user_id, row.user_name)
如果并不在意数据处理时间,可以使用光标本身作为一个迭代器,逐行迭代。这样可以节省大量的内存开销,但是由于和数据来回进行通信,速度会相对较慢:
cursor.execute("selectuser_id, user_name from users"):
for row in cursor:
print(row.user_id, row.user_name)
由于Cursor.execute总是返回游标(cursor), 所以也可以简写成:
for row incursor.execute("select user_id, user_name from users"):
print(row.user_id, row.user_name)
我们可以在execut中使用”””三重引号,来应用多行SQL字符串。这样sql的可读性大大增强。这是python特有的特性:
cursor.execute(
"""
select user_id, user_name
from users
where last_logon < '2001-01-01'
and bill_overe = 1
""")
SQL参数
ODBC支持使用问号作为SQL的查询参数占位符。可以在execute方法的SQL参数之后,提供SQL参数占位符的值:
cursor.execute(
"""
select user_id, user_name
from users
where last_logon < ?
and bill_overe = ?
""", '2001-01-01', 1)
这样做可以防止SQL注入攻击,提高安全性。如果使用不同的参数反复执行相同的SQL它效率会更高,这种情况下该SQL将只预装(prepared )一次。(pyodbc只保留最后一条编写的语句,所以如果程序在语句之间进行切换,每次都会预装,造成多次预装。)
Python的DB API指定参数应以序列(sequence)对象传递,所以pyodbc也支持这种方式:
cursor.execute(
"""
select user_id, user_name
from users
where last_logon < ?
and bill_overe = ?
""", ['2001-01-01', 1])
插入数据
插入数据使用相同的函数 - 通过传入insert SQL和相关占位参数执行插入数据:
cursor.execute("insertinto procts(id, name) values ('pyodbc', 'awesome library')")
cnxn.commit()
cursor.execute("insertinto procts(id, name) values (?, ?)", 'pyodbc', 'awesome library')
cnxn.commit()
注意:调用cnxn.commit()。发成错误可以回滚。具体需要看数据库特性支持情况。如果数据发生改变,最好进行commit。如果不提交,则在连接中断时,所有数据会发生回滚。
更新和删除数据
更新和删除工作以同样的方式:通过特定的SQL来执行。通常我们都想知道更新和删除的时候有多少条记录受到影响,可以使用Cursor.rowcount来获取值:
cursor.execute("deletefrom procts where id <> ?", 'pyodbc')
print('Deleted {}inferior procts'.format(cursor.rowcount))
cnxn.commit()
由于execute 总是返回游标(允许你调用链或迭代器使用),有时我们直接这样简写:
deleted =cursor.execute("delete from procts where id <> 'pyodbc'").rowcount
cnxn.commit()
注意一定要调用commit。否则连接中断时会造成改动回滚。
技巧和窍门
引号
于单引号SQL是有效的,当值需要使用单引号时,使用用双引号包围的SQL:
cursor.execute("deletefrom procts where id <> 'pyodbc'")
如果使用三重引号, 我们可以这样使用单引号:
cursor.execute(
"""
delete
from procts
where id <> 'pyodbc'
""")
列名称
Microsoft SQLServer之类的一些数据库不会产生计算列的列名,在这种情况下,需要通过索引来访问列。我们也可以使用sql列别名的方式,为计算列指定引用名称:
row =cursor.execute("select count(*) as user_count fromusers").fetchone()
print('{}users'.format(row.user_count)
当然也可以直接使用列索引来访问列值:
count =cursor.execute("select count(*) from users").fetchone()[0]
print('{}users'.format(count)
注意,上例的首列不能是Null。否则fetchone方法将返回None并且会报NoneType不支持索引的错误。如果有一个默认值,经常可以是ISNULL或合并:
maxid =cursor.execute("select coalesce(max(id), 0) fromusers").fetchone()[0]
自动清理
连接(默认)在一个事务中。如果一个连接关闭前没有提交,则会进行当前事务回滚。很少需要finally或except 语句来执行人为的清理操作,程序会自动清理。
例如,如果下列执行过程中任何一条SQL语句出现异常,都将引发导致这两个游标执行失效。从而保证原子性,要么所有数据都插入发生,要么所有数据都不插入。不需要人为编写清理代码。
cnxn =pyodbc.connect(...)
cursor = cnxn.cursor()
cursor.execute("insertinto t(col) values (1)")
cursor.execute("insertinto t(col) values (2)")
cnxn.commit()

‘叁’ python 怎么调用odbc

需要下载第三方库pypyodbc 。示例代码:
import pypyodbc
pypyodbc.win_create_mdb('D:\\database.mdb')
connection_string = 'Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\database.mdb'
connection = pypyodbc.connect(connection_string)
SQL = 'CREATE TABLE saleout (id COUNTER PRIMARY KEY,proct_name VARCHAR(25));'
connection.cursor().execute(SQL).commit()

‘肆’ 利用python读取driver版本及厂家信息,需要调用哪些模块

import timeimport serialser = serial.Serial( #下面这些参数根据情况修改 port='COM1', baudrate=9600, parity=serial.PARITY_ODD, stopbits=serial.STOPBITS_TWO, bytesize=serial.SEVENBITS)data = ''while ser.inWaiting() > 0: data += ser.read(1)if data != '': print data

‘伍’ 如何用python 调用c写的驱动

我觉得会受到限制的,因为c_char_p是遵循c字符串标准的,会以NULL为结束。下面的代码只输出hello,也许真要传递内嵌NULL的,只能靠编写python扩展了,也很简单的,用swig。
from
ctypes
import
*
import
struct
example=cdll.LoadLibrary("example.dll")
s=create_string_buffer('hello\x00world')
example.test.restype=c_char_p
example.test.argtypes
=
[c_char_p]
r=example.test(s)
#("hello\x00world")
print
r

‘陆’ python怎样根据驱动程

1、打开电脑管家,点击“工具箱”。
2、在工具箱里找到“硬件检测”。
3、在硬件检测里点击“驱动安装”。
4、可以看到“安装状态”,如果是未安装可以直接点击安装。

‘柒’ 如何用 python 读取硬件信息

在LINUX下, *NIX 嘛. 硬件信息都在 info 文件里, 直接读取文本文件就行了.:
f = open("/proc")
print(f.readlines())
f.close()

在windows下, win32 扩展调用WINDOWS的API应该可以做到.:
import os, csv
fp = os.popen("wmic cpu list /format:csv")
for i in csv.reader(fp):
print i

‘捌’ 利用python写一段读取电脑配置信息的程序

主要利用python的wmi模块,提供非常多的信息。

importwmi
defsys_version():
c=wmi.WMI()

#操作系统版本,版本号,32位/64位
print(' OS:')
sys=c.Win32_OperatingSystem()[0]
print(sys.Caption,sys.BuildNumber,sys.OSArchitecture)

#CPU类型CPU内存
print(' CPU:')
processor=c.Win32_Processor()[0]
print(processor.Name.strip())
Memory=c.Win32_PhysicalMemory()[0]
print(int(Memory.Capacity)//1048576,'M')

#硬盘名称,硬盘剩余空间,硬盘总大小
print(' DISK:')
fordiskinc.Win32_LogicalDisk(DriveType=3):
print(disk.Caption,'free:',int(disk.FreeSpace)//1048576,'M ','All:',int(disk.Size)//1048576,'M')

#获取MAC和IP地址
print(' IP:')
forinterfaceinc.Win32_NetworkAdapterConfiguration(IPEnabled=1):
print("MAC:%s"%interface.MACAddress)
forip_addressininterface.IPAddress:
print(" IP:%s"%ip_address)

#BIOS版本生产厂家释放日期
print(' BIOS:')
bios=c.Win32_BIOS()[0]
print(bios.Version)
print(bios.Manufacturer)
print(bios.ReleaseDate)


sys_version()

显示:

OS:
MicrosoftWindows10专业版1713464位

CPU:
Intel(R)Core(TM)[email protected]
8192M

DISK:
C:free:34165M All:120825M
D:free:265648M All:390777M
E:free:35669M All:204796M
F:free:5814M All:28163M
G:free:328650M All:329999M

IP:
MAC:00:50:56:C0:00:01
IP:192.168.182.1
IP:fe80::e0fb:efd8:ecb0:77f4
MAC:00:50:56:C0:00:08
IP:192.168.213.1
IP:fe80::8da1:ce76:dae:bd48
MAC:54:E1:AD:77:57:AB
IP:192.168.199.105
IP:fe80::aca8:4e6f:46e7:ef4a

BIOS:
LENOVO-1
LENOVO
20170518000000.000000+000
阅读全文

与python读取电脑odbc驱动相关的资料

热点内容
六级pdf 浏览:854
jsp嵌入java代码 浏览:161
Python中Windows字体颜色 浏览:692
n7笔记app哪个好 浏览:415
kindle用什么app好 浏览:719
方舟加密服务器怎么进 浏览:60
传文件夹太慢 浏览:218
基于单片机的仓库 浏览:377
央企直营朔源码燕窝 浏览:340
日本校园老师电影 浏览:65
买黄金首饰上什么app 浏览:452
共享pdf 浏览:343
老武侠电影,是一个女的用乳房打人,名字 浏览:649
pythonsocket库 浏览:401
缉魂130分钟台湾完整版 浏览:688
wifi电视一般需要什么app 浏览:526
怎样保护自己的id密码加密 浏览:244
韩剧女主手上带个铃铛 浏览:374
南充云服务器 浏览:984
哪个网站下载源码不要钱 浏览:739