python 连接 mysql

http://blog.csdn.net/yelbosh/article/details/7498641

数据库的连接

模块引入之后我们就需要和数据库进行连接了,实例代码如下:

db = MySQLdb.connect("localhost","root","123456","myciti" )

这三个关键参数的含义一目了然:第一个为服务器的地址,第二个为用户名,第三个为dbms密码,第四个为要访问的数据库,其实该connect函数的参数不止这些,不过由于其有默认值而且大多数情况下不用修改,因而省略了。这里做如下列表:

  • host,连接的数据库服务器主机名,默认为本地主机(localhost)。

  • user,连接数据库的用户名,默认为当前用户。

  • passwd,连接密码,没有默认值。

  • db,连接的数据库名,没有默认值。

  • conv,将文字映射到Python类型的字典。默认为MySQLdb.converters.conversions

  • cursorclass,cursor()使用的种类,默认值为MySQLdb.cursors.Cursor。

  • compress,启用协议压缩功能。

  • named_pipe,在windows中,与一个命名管道相连接。

  • init_command,一旦连接建立,就为数据库服务器指定一条语句来运行。

  • read_default_file,使用指定的MySQL配置文件。

  • read_default_group,读取的默认组。

  • unix_socket,在unix中,连接使用的套接字,默认使用TCP。

  • port,指定数据库服务器的连接端口,默认是3306

大家可能会注意到源码中没有用到端口号,这是因为MySQLdb的connect函数的该参数的默认值便是3306,如果你在安装mysql的时候修改了数据库的端口号,那么你就需要在源码中加上该参数的修改值了。

执行sql语句

连接上之后便是执行sql语句了,源代码如下:

import MySQLdb


db = MySQLdb.connect("localhost","root","123456","myciti" )
cursor = db.cursor()
sql = """insert into article values (0,"woainimahah","http://www.aa.com","2012-9-8","wo","qq","skjfasklfj","2019","up")"""
try:
    cursor.execute(sql)
    db.commit()
except:
    db.rollback()
db.close

这里需要注意的是一定要记得commit,如果不提交那么数据库是不会有变化的

将sql语句改变成其他,便可以实现其他静态的操作,如“delete from article where id > 3”

选择和打印

连接数据库最重要的目的便是读取数据库中的信息,那么如何获取数据库中的数据呢?如何提取有效的信息呢?见如下代码:

下面的程序可以实现打印数据库中article表中所有行的第二列的数据:

import MySQLdb


db = MySQLdb.connect("localhost","root","123456","myciti" )
cursor = db.cursor()
cursor.execute("select * from article")
data = cursor.fetchone()
while data!=None:
    print data[1]
    data = cursor.fetchone()
db.close

我们也可以使用如下代码:

import MySQLdb


db = MySQLdb.connect("localhost","root","123456","myciti" )
cursor = db.cursor()
cursor.execute("select * from article")
datas = cursor.fetchall()
for data in datas:
    print data[1]
print cursor.rowcount,"rows in tatal"
db.close

我们可以从代码中看出上面的各个函数的区别:

fetchone是从数据库表中取出一行记录,第二次调用便取出next行,不断向下走

fetchall取出数据库表中所有行的数据

rowcount读出数据库表中的行数

和java中的占位符一样,python中也需要这些占位符来实现动态的选择。见如下代码:

import MySQLdb


a = "down"
b = 4
db = MySQLdb.connect("localhost","root","123456","myciti" )
cursor = db.cursor()
cursor.execute("select * from article where trend = '%s' and id < '%d'"%(a,b))
datas = cursor.fetchall()
for data in datas:
    print data[1]
print cursor.rowcount,"rows in tatal"
db.close

这里的占位符和java中的?占位符还有一点区别,那就是它声明了类型,和C中的格式输出是一样的。注意后面的%

那么返回的data也是一个列表,通过使用[]操作符来访问特定列的数据。

静态插入和动态插入

静态插入上面我们已经讲了,那么下面我们再讲一下动态插入

动态插入也是用占位符来实现的

import MySQLdb


title = "wangxinmeiwo"
url = "henxiangni "
db = MySQLdb.connect("localhost","root","123456","myciti" )
cursor = db.cursor()
sql = """insert into article values (0,"%s","%s","2012-9-8","wo","qq","skjfasklfj","2019","up")"""
try:
    cursor.execute(sql%(title,url))
    db.commit()
except:
    db.rollback()
db.close

可以看到这里的占位符和上面的使用方式是一样的

update操作

占位符的使用和上面是一样的

import MySQLdb


title = "haoxiangni"
id=11
db = MySQLdb.connect("localhost","root","123456","myciti" )
cursor = db.cursor()
sql = """update article set title = "%s" where id = "%d" """
try:
    cursor.execute(sql%(title,id))
    db.commit()
except:
    db.rollback()
db.close

原文地址:https://www.cnblogs.com/juandx/p/4145322.html