CentOS下安装python-mysqldb

£神魔★判官ぃ 2022-08-20 05:26 271阅读 0赞

1、# yum install python-devel mysql-devel zlib-devel openssl-devel

2、http://pypi.python.org/pypi/MySQL-python/#downloads 下载安装包

  1. \# wget http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz

3、http://pypi.python.org/pypi/setuptools#downloads 下载工具

  1. \# wget http://pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.egg

4、先安装工具

  1. \# sh setuptools-0.6c11-py2.4.egg
  2. \# python
  3. >>> import setuptools

不提示错误表示成功

5、安装 MySQL-python-1.2.3.tar.gz

  1. \# tar -zxvf MySQL-python-1.2.3.tar.gz
  2. \# cd MySQL-python-1.2.3
  3. \# vi setup\_posix.py

找到mysql_config.path 一行,改为mysql_config.path = “/usr/bin/mysql_config”

# python setup.py build

# python setup.py install

# python

  1. >>> import MySQLdb

不提示错误表示成功

Python 操作数据库 连接创建库:

[python] view plain copy

  1. #! /usr/bin/env python
  2. import MySQLdb
  3. conn = MySQLdb.connect(host=’localhost’,user=’root’,passwd=’root’,charset=’utf8’)
  4. cursor = conn.cursor()
  5. #Crete Database
  6. #cursor.execute(“””create database python “””)
  7. #Select Database
  8. conn.select_db(‘python’);
  9. #Create Table
  10. #cursor.execute(“””create table gaiqi(id int(4),info varchar(100)) “””)
  11. #Insert data
  12. #value = [1,”inserted”];
  13. #cursor.execute(“insert into test values(%s,%s)”,value);
  14. #Insert more
  15. values=[]
  16. for i in range(20):
  17. values.append((i,’Hello Mysqldb’+str(i)))
  18. cursor.executemany(“””insert into test values(%s,%s)”””,values);
  19. cursor.close();

查询记录

[python] view plain copy

  1. #! /usr/bin/env python
  2. import MySQLdb
  3. conn = MySQLdb.connect(host=’localhost’,user=’root’,passwd=’root’,db=’python’,charset=’utf8’)
  4. cursor = conn.cursor()
  5. count = cursor.execute(‘select * from test’)
  6. print ‘All Total %s’,count
  7. #Get 1 Result
  8. result = cursor.fetchone();
  9. print result
  10. print ‘ID:%s inof:%s’% result
  11. #Get 5 Result
  12. results = cursor.fetchmany(5)
  13. for r in results:
  14. print r
  15. #Get All Result
  16. res = cursor.fetchall()
  17. for r in res:
  18. print r
  19. cursor.close();

插入时间:

[python] view plain copy

  1. import time
  2. print time.strftime(‘%Y-%m-%d %H:%M:%S’,time.localtime(time.time()))

备份文件:

[python] view plain copy

  1. #! /usr/bin/env python
  2. import os
  3. import time
  4. source = [‘/var/www/html’,’/var/test’]
  5. target_dir = ‘/mnt/backup/‘
  6. target = target_dir+time.strftime(‘%Y%m%d%H%M%S’)+’.zip’
  7. today = target_dir+ time.strftime(‘%Y%m%d’)
  8. now = time.strftime(‘%H%M%S’)
  9. if not os.path.exists(today):
  10. os.mkdir(today)
  11. print ‘Dir OK’,today
  12. target = today+os.sep+now+’.zip’
  13. zip_command = “zip -qr ‘%s’ %s” % (target,’ ‘.join(source))
  14. if os.system(zip_command) == 0:
  15. print ‘Success Backup to’,target
  16. else:
  17. print ‘Failed Backup’

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CentOS 安装 Nginx

    早上下起了暴雨 闹钟还未响起就听到雨滴哗啦啦击打窗户的声音,被吵醒了。起床上班,在楼下的十字路口,暴雨大到完全看不清对面,两个穿着雨衣的交警站在路口中间指挥