关于python和php数据库连接问题_MySQL
python
在几天的对完结项目的修正中,涉及到一些对python和php数据库的连接问题的处理。
在其中有关于系统方面的,也有库相关的,在这里做一个小小的总结。
1.python数据库连接
mysql:import MySQLdb
mssql:import pymssql
这里小提一下,pyodbc也是可以连接的,至于有没有pdo的库,偶也不知道。
2.php的数据库连接
mysql:
连接方法海了去了,pdo,odbc,mysql默认连接法子(不需要加扩展,一般用这个)。
mssql:
在这里要分在win下和在linux下了,在win下很是方便:
可以sqlsrv,这个是微软官方提供的连接方式,在大多数dll啥失效后还能用,如:
http://www.php.net/manual/zh/book.sqlsrv.php
鄙人在codeigniter论坛找到一个利用方法,如:
https://github.com/EllisLab/CodeIgniter/tree/develop/system/database/drivers/sqlsrv
下载两个文件sqlsrv_driver.php,sqlsrv_result.php即可。
然后在入口文件包含核心文件之前包含上面两个文件。
配置方法:
http://stackoverflow.com/questions/19460025/how-do-i-connect-codeigniter-to-a-sql-server
PS:这个也是php标准拓展。
在win下也需要处理一系列php高版本后对mssql不再支持的问题,话说在php6后只支持pdo连接了。
在此之前可以用pdo和odbc,或者服务器开启了mssql模块的话,可以直接mssql_connect,但仍要在php.ini里改下extension配置即可。
在linux下:
不想提了,蛋都碎了。
在linux下做mssql连接,很重要的一点就是freetds,这玩意儿linux连接mssql必备。mssql是从sybase衍生出来的,有一些哥们做了一些sybase的linux下的连接库,这些连接库同时也能支持mssql,
我用的时候是0.9吧(0.8也可),在这时配置的版本需要弄成8.0,具体的我就不再详述,多查阅资料大家会恍然的。
安装完freetds后(可以下载安装包安装,也可以yum、apt-get源安装),
在这里提供一个下载链接:http://blog.csdn.net/kunp/article/details/387013
然后就是选择mssql_connect还是pdo还是odbc,暂时找到这几种方法,令人纠结心痛的是这几种方法似乎都要重新编译php,将环境啥配置啥的重新编译进去,由于鄙人找了半天确实没找到他说的php源代码安装目录下ext目录的mssql目录,然后去configure,所以这条路pass掉,其中需要phpize编译的步奏,也pass掉,鄙人perl环境出过问题,执行老出相关错误(明白怎么回事的大牛欢迎吐槽,偶是昏了)。所以意思是php扩展的连接库文件**.so等压根不能生成,也谈不上去php.ini里去改啥扩展配置路径,蛋碎一地。
怎么办呢?基友告诉我个法子,让我不装扩展,直接用php-mssql,鄙人centos6,yum了一下,坑比的没有安装包,直接被丢了一脸的链接:
https://gist.github.com/shf/2052432
通过这个方法我装上了php-mssql(需要代理),然后yum安装了php-odbc,然后,然后木有连上mssql,不知道咋回事,这个问题留着解决,猜测应该是还要做其他配置,完全是基友跟我瞎摸索有木有。。
3.关于mssql开启远程
大家知道如果要让外服务器访问,sqlserver必须开启远程,开启1433,在这里稍稍说下。
http://www.360doc.com/content/13/0525/23/19147_288191810.shtml
这篇文章介绍的比较全,但是偶的1433端口一直没开,telnet xx.xx.xx.xx 1433不上。
怎么办,如:
http://hi.baidu.com/daheshuiman/item/6dad61982dd267895814615d
把1433端口配上启用、活动。
记住sql-server browser必须打开。
在网上有说sqlserver2005 版本0.8以下的需要打补丁才开的了,也就说至少要打上sp3以上的补丁。
PS:在这里提下,我自己用时sql-server browser没有打开,按钮灰色,所以需要要把所有属性勾上活动,所有勾上是,这点特别注意。
开启了后一般就能看的1433能telnet上了。外连时,据网上说,可以直接用ip,还有说需要加sqlserver实例名,偶表示不明觉厉。
附上另两篇参考文章:
http://www.bitsCN.com/database/201304/204867.html
http://wenwen.sogou.com/z/q410734670.htm
最后,偶因为在linux环境下,无法连接mssql,只好退而求其次,连接mysql,但是原来的数据却是放在mssql里的,所以需要将数据转移:
https://github.com/Nick011/MSSQL-to-MySQL/blob/master/convert_db.py
在这里看到,在两个数据库都在开始开启了cursor后,在insert了才commit(),但是注意看最后,每一次循环都要commit一次,
说明连接时只连接一次的。好吧,这个好像弱爆了。。
最后,上面讲的很粗不是本人意愿啊!!!完全是因为实验时老爷机崩溃了,资料引用全部丢失,以上大部分凭记忆拼接,所以缺漏之处,
还请不吝赐教,感谢!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
