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

来源:互联网 发布:linux 显示目录结构 编辑:程序博客网 时间:2024/06/02 05:47




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:Challenchenzhipengdate:2013-0902'''import MySQLdbdb=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:chenzhipengdate:2013-0902'''import MySQLdbdef 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()