导航:首页 > 编程语言 > pythonfreetds

pythonfreetds

发布时间:2022-08-10 17:12:37

‘壹’ Redhat的linux下使用python里pyodbc模块插入SQL Server数据中文乱码

应该是数据库和python脚本编码方式不一样,你把脚本编码方式改成GBK或GB2312试试。

‘贰’ Python在命令行下安装pymssql出错

You need to install the FreeTDS development package (freetds-dev) before trying to install pymssql with pip:

$ sudo apt-get install freetds-dev
and then, in your virtualenv or wherever you wish to install it:

$ pip install pymssql

‘叁’ python 怎么调用odbc

使用pypyodbc
import pypyodbc
pypyodbc.connect('Driver=FreeTDS;Server=192.168.1.2;port=1433;uid=sa;pwd=pwd1;')

‘肆’ 在windows上的python 连接 linux 并做点操作

访问SqlServer

复制代码代码如下:

>>> import pyodbc

>>>cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.1.100\\sql;DATABASE=testDB;UID=sa;PWD=myPassword')
>>>cursor = cnxn.cursor()
>>>cursor.execute("select * from Tb")

二、Linux下配置Python访问SqlServer
环境:CentOS 6.2 + Sqlserver 2008
1、安装freetds:

复制代码代码如下:

yum install freetds*

2、安装pyodbc:

复制代码代码如下:

yum install pyodbc

修改odbc配置:

复制代码代码如下:

vi /etc/odbcinst.ini

添加FreeTDS驱动:

复制代码代码如下:

[SQL Server]

Description = FreeTDS ODBC driver for MSSQL
Driver = /usr/lib/libtdsodbc.so
Setup = /usr/lib/libtdsS.so
FileUsage = 1

3、测试

复制代码代码如下:

#python

>>> import pyodbc
>>>cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.1.100\\sql;DATABASE=testDB;UID=sa;PWD=myPassword')
>>>cursor = cnxn.cursor()
>>>cursor.execute("select * from Tb")

‘伍’ pyodbc操作MySQL求助

1、连接sql server:
conn_info = 'DRIVER={SQL Server};DATABASE=%s;SERVER=%s;UID=%s;PWD=%s'%(database, host, user, pwd)
self.mssql_conn = pyodbc.connect(conn_info)
self.mssql_cur = self.mssql_conn.cursor()
2、连接mysql
需要安装mysql odbc:http://dev.mysql.com/downloads/connector/odbc/
conn_info = ('Driver={MySQL ODBC 5.1 Driver};Server=%s;Port=%s;Database=%s;User=%s; Password=%s;Option=3;'%(host, port, database, user, pwd ))
self.mysql_conn = pyodbc.connect(conn_info)
self.mysql_cur = self.mysql_conn.cursor()
3、MysqlDB
不需要安装mysql odbc,http://sourceforge.net/projects/mysql-python/
目前linux版本支持到2.7,windows版本支持到2.5。
4、5、linux下pyodbc的安装
需安装unixODBC,Freetds,mysql-connector-odbc
5、linux下pyodbc的使用
pyodbc 不支持在在linux 使用如下连接方式
s= pyodbc.connect('DRIVER={SQL Server};SERVER=127.0.0.0;DATABASE=test;UID=test;PWD=test')
linux上正确的连接mssql的方式为
s= pyodbc.connect('DRIVER={FreeTDS};SERVER=127.0.0.0;DATABASE=test;UID=idc;PWD=test')

‘陆’ 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怎么连接sqlserver

如果使用pyodbc,直接看2就可以了,可以略过1
1. Python连接mssql
ubuntu linux上
1.1
sudo apt-get install python
1.2 重启终端
1.3

sudo apt-get --assume-yes update
sudo apt-get --assume-yes install freetds-dev freetds-bin
sudo apt-get --assume-yes install python-dev python-pip
sudo pip install pymssql

1.4
sudo vi /etc/freetds/freetds.conf
2.安装pyodbc
2.1为了解决pyodbc.h:52:17: fatal error: sql.h: No such file or directory

sudo yum install unixODBC-devel.x86_64
然后:
sudo pip install pyodbc
2.2安装MSSQL native client
2.3

编写Python程序:
import pyodbc
conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};
SERVER=aaaa; UID=bbbb; PWD=ccc; DATABASE=ddd; Encrypt=yes;
TrustServerCertificate=no');
cursor = conn.cursor()
cursor.execute("select count(distinct d) as cnt from ddddd6 where day_id=20160531")
row = cursor.fetchone()
while row:
print str(row[0])
row = cursor.fetchone()

‘捌’ Ubuntu下安装pymssql的问题

python的dev包 和freetds的dev包有没有装?

然后看一下这些头文件系统里有没有:
Python.h, structmember.h, datetime.h, sqlfront.h, sqldb.h

阅读全文

与pythonfreetds相关的资料

热点内容
如何把扫描文件做成pdf格式 浏览:624
php个性qq源码 浏览:821
初学c语言显示源未编译 浏览:245
资产概况源码 浏览:472
dos命令建文件夹命令 浏览:379
解压的密码htm被屏蔽 浏览:502
冬天太冷冰箱压缩机不启动怎么办 浏览:83
手机打开vcf需要什么编译器 浏览:910
加密磁盘后开机很慢 浏览:271
长沙智能云控系统源码 浏览:258
阿里云服务器如何设置操作系统 浏览:1001
超级命令的英文 浏览:784
做账为什么要用加密狗 浏览:586
考研群体怎么解压 浏览:159
linux修改命令提示符 浏览:226
圆圈里面k图标是什么app 浏览:63
pdf加空白页 浏览:948
linux服务器如何看网卡状态 浏览:318
解压新奇特视频 浏览:707
图书信息管理系统java 浏览:554