python 连接 MySQL 配置及增 删 改 查的操作




http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

$ tar zxvf MySQL-python-1.2.3.tar.gz
$ cd MySQL-python-1.2.3
$ python setup.py build
$ python setup.py install


root@vpser:~/MySQL-python-1.2.3# python setup.py install 
sh: mysql_config: not found
Traceback (most recent call last):
  File "setup.py", line 15, in <module>
    metadata, options = get_config()
  File "/root/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/root/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

 

sudo apt-get install libmysqld-dev


出现No such file or directory的错误,有两种情况,一种是真的没有Python.h这个文件,一种是Python的版本不对,

可以进入/usr/include/文件夹下的Python2.x文件夹里查找是否有Python.h这个文件。

如果是第一种情况,那么需要安装Python-dev这个包,(sudo apt-get install python-dev)

#!/usr/bin/env python
'''
user:Challenchenzhipeng
date:2013-0902
'''
import MySQLdb
db=MySQLdb.connect(host="localhost", user="root",passwd="root", db="yarnhosts")
cursor=db.cursor()
cursor.execute("select * from yarnhosts;")
result=cursor.fetchall()

for record in result:
    print "%s \t %s\t %s\t" %record  
# print record[0]+" hosttype= "+record[1]+" deleted= "+record[2]


#!/usr/bin/env python
'''
user:chenzhipeng
date:2013-0902
'''
import MySQLdb
def mysqlOpen():
    global db
    db=MySQLdb.connect(host="localhost", user="root",passwd="root", db="yarnhosts")
    global cursor
    cursor=db.cursor()

def mysqlClose():
    db.close()

def mysqlSelect():
    cursor.execute("select * from yarnhosts;")
    result=cursor.fetchall()
    for record in result:
        print "%s \t %s\t %s\t" %record  
# print record[0]+" hosttype= "+record[1]+" deleted= "+record[2]

def mysqlInsert(sql):
    return cursor.execute(sql)

def mysqlDelete(sql,param):
    return cursor.execute(sql,param)

def mysqlUpdate(sql,param):
    return cursor.execute(sql,param)

if __name__=='__main__':
    mysqlOpen()
    print "select:"
    mysqlSelect()
   # print "Insert:"
    #sqla="insert into yarnhosts(hoststype,deleted) values('se','1')"
    #mysqlInsert(sqla)
    #mysqlSelect()
   # print "update:"
    #sql="update yarnhosts set hoststype=%s where yarnhosts_id=1"
   # param=("bbb")
   # mysqlUpdate(sql,param)
   # mysqlSelect()
   # print "delete:"
   # sql="delete from yarnhosts where yarnhosts_id=%s"
    #param=("2")
    #mysqlDelete(sql,param)

   # mysqlSelect()
    mysqlClose()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值