pyodbc mysql_pyodbc and mySQL
问题
I am unable to connect to mySQl db using pyodbc.
Here is a snippet of my script:
import pyodbc
import csv
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=mydb; UID=root; PASSWORD=thatwouldbetelling;")
crsr = cnxn.cursor()
with open('C:\\skunkworks\\archive\\data\\myfile.csv','r') as myfile:
rows = csv.reader(myfile, delimiter=',', quotechar='"')
for row in rows:
insert_str = 'INSERT into raw_data VALUES(something, something)'
print insert_str
#crsr.execute(insert_str)
cnxn.commit()
myfile.close()
I get this error at the pyodbc.connect() line:
pyodbc.Error: ('IM002', '[IM002]
[Microsoft][ODBC Driver Manager] Data
source name not found and no default
driver specified (0)
(SQLDriverConnectW)')
I have another question regarding this error (and Python scripts in general). When I run this as a script, it fails silently (I was expecting a stack trace). I have to type each line in manually to find where the error occured.
I am being a bit lazy for now (no exception handling) - is this normal behaviour of a Python script without exception handling to fail silently?
[Edit]
I am not using mysqldb because I am already using pyodbc to extract my data from another source (MS Access). Ok, not a good reason - but I am already grappling with pyodbc and I dont really fancy having to wrestle with another library/module/package(whatever its called in Python) for a "one off" job. I just want to move my data of from various data sources in the Windows environment to mySQl on Linux. once on Linux, I'll be back on terra firma.
That is the entire 'script' right there. I just saved the code above into a file with a .py extension, and I run python myscript.py at the command line. I am running this on my XP machine
回答1:
MySQLdb (or oursql) and pyodbc both have the same interface (DB-API 2), only you don't have to deal with ODBC's issues if you use the former. I strongly recommend you consider using MySQLdb (or oursql) instead.
回答2:
First, According to the official docs, if you want to connect without creating a DSN, you need to specify OPTION=3 in the connection string:
ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;USER=venu;PASSWORD=venu;OPTION=3;"
If that fails to work, I'd further troubleshoot by creating a DSN.
Second, no Python should not be failing silently. If that is the case when you run your script, there is something else amiss.
回答3:
I had this same mistake so I went over all the version I was using for the connection. This is what I found out:
For Python 2.7 32 bits:
- pyodbc must be 32bits
- the DB Driver must be 32bits. (Microsoft Access should be 32 bits too)
For those who use the 64 bits version. You should check that everything is 64 bits too.
In my case I was trying to connecto to an Oracle DB and Microsoft Access DB so I had to make the following components match the architechture version:
pyodbc (MS Access)
python
cx_Oracle (Oracle dialect for SQLalchemy)
Oracle instantclient basic (Oracle. Do not forget to create the environment variable)
py2exe (Making the excecutable app)
回答4:
only need install mysql-connector-odbc-3.51.28-win32.msi.
and pyodbc-2.1.7.win32-py2.7.exe.
of course, you have ready installed MySQL and python 2.7.
example:
import pyodbc
cndBase = pyodbc.connect("DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; PORT=3306;DATABASE=nameDBase; UID=root; PASSWORD=12345;")
ptdBase = cndBase.cursor()
query_str = 'SELECT* FROM nameTabla;'
rows = ptdBase.execute(query_str)
for rw in rows:
print list(rw)`enter code here`
回答5:
Is that your driver name right?
You can check your driver name in
Windows -> Control panel -> System and security -> Administrative tools -> ODBC Data Sources -> Driver tab
then copy the river name to the first parameter
like
cnxn = pyodbc.connect("DRIVER={MySQL ODBC 5.3 ANSI Driver}; SERVER=localhost;DATABASE=books; UID=root; PASSWORD=password;")
And my problem solved
or you may not install the driver and the step is simple.
回答6:
I was getting the same error. It seemed the driver i was using to make the connection was not the driver installed in my system.
Type ODBC on windows run and select ODBC Data Source(32/64) based on where you have installed the driver.
From there click on System DSN and click add. From there you can see the MySQL driver installed in your system. Use the ANSI driver in your code where you are making the connection.
来源:https://stackoverflow.com/questions/3982174/pyodbc-and-mysql
pyodbc mysql_pyodbc and mySQL相关推荐
- python pyodbc mysql_pyodbc和mySQL
我无法使用pyodbc连接到mySQl数据库. 以下是我的脚本片段:import pyodbc import csv cnxn = pyodbc.connect("DRIVER={MySQL ...
- python 数据库操作 yeild from_Python数据库连接关闭
连接具有PEP-249(Python数据库API规范v2.0)中指定的csr.close()方法: import pyodbc conn = pyodbc.connect('DRIVER=MySQL ...
- python︱mysql数据库连接——pyodbc
直接连接数据库和创建一个游标(cursor) 数据查询(SQL语句为 select -from-where) 1.pyodbc连接 import pyodbccnxn = pyodbc.connect ...
- SQLALchemy之Python连接MySQL
20220225 https://www.cnblogs.com/toheart/p/9802990.html pymssql连接sqlserver https://blog.csdn.net/qq_ ...
- linux python连接oracle数据库_Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法...
本文档主要描述了Linux下python数据库驱动的安装和配置,用来实现在Linux平台下通过python访问MySQL.Oracle.SQL Server数据库. 其中包括以下几个软件的安装及配置: ...
- python连接sqlserver、怎么跨表查询_python 连接sqlserver,mysql
python连接sqlserver有很多包可以选择,有pyodbc和pymssql,这里把代码都贴出来,但是希望大家用pyodbc,因为在实践中跑几百万的数据量跑了一天的时候连接会断,google S ...
- wins系统flask绑定mysql_flask如何连接mssql,网上大多是sqlite和mysql教程?
这个居然也冒出来,刨坟了. 我们不喜欢写原生SQL语句,那个写着费劲,日常开发时候,我们怎么CRUD数据库呢?一般使用ORM,对象关系映射(英语:Object Relational Mapping,简 ...
- pymysql ︱mysql的基本操作与dbutils+PooledDB使用
之前的一篇:python︱mysql数据库连接--pyodbc 文章目录 0 安装依赖 1 连接方式 1.1 pymysql.connect直接连 1.2 pandas连接 1.3 dbutils+P ...
- mysql与pandas谁快,pandas和SQL哪个快
python pandas to_sql将excel数据导入到MySQL数据库 用python pandas to_sql写了个脚本,向MySQL数据库导入excel数据,本地其实吧, 一分钟10W条 ...
- pyodbc操作Access数据库
现在各种各样的数据库层出不穷,什么MySQL.PostgreSQL.MongoDB这些都是比较火的,还有一些稍微小众一点的数据库就更多了.相比之下,微软Office套件里面附带的Access数据库究落 ...
最新文章
- WPF的Clipboard.SetText()有问题
- 基于jQuery的对象切换插件:soChange 1.5 (点击下载)
- modbus调试工具 linux,linux libmodbus的移植
- Python数模笔记-Sklearn (1)介绍
- 树莓派 python_树莓派笔记08—Python流水灯
- 撤消git update-index --assume-unchanged file
- Java计划任务:ScheduledThreadPoolExecutor
- Nginx从入门到精通(全)
- win10计算机被网络设备发现,图文解决win10系统网络发现已关闭计算机和设备不见的方法...
- python由编译器将源程序转化为机器语言、然后执行_高级语言程序设计(Python)-中国大学mooc-车万翔-车万翔...
- excel切片器_干货分享:Excel数据透视表操作技巧,帮你提升工作效率
- linux shc shell脚本_Linux系统中Shell脚本使用Shc和gzexe加密/解密
- css flex布局iOS8兼容性问题
- matlab数据处理的优缺点,数字滤波的优缺点分析
- 优粮生活炒菜机器人_【O2O案例】优粮生活:一个餐饮人该有的产品精神
- ADB 最新调试工具
- Windows 运行chkdsk磁盘修复工具命令参数详解
- 关于语音识别技术的初探
- 弹性布局之em的用法!
- HTML5游戏引擎lufylegend深入浅出 - 引擎介绍原理