CentOS下安装python-mysqldb

本文详细介绍了如何使用Python安装MySQL-python库,并通过实例展示了如何连接数据库、创建库、创建表、插入数据以及查询记录的方法。

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

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

      #    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  下载工具

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

4、先安装工具

    #  sh setuptools-0.6c11-py2.4.egg

    #  python

    >>>   import  setuptools

   不提示错误表示成功

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

    #    tar -zxvf MySQL-python-1.2.3.tar.gz

    #    cd   MySQL-python-1.2.3

    #    vi setup_posix.py

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

   #   python setup.py build

   #   python setup.py install

   #    python

    >>>   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.   
  6. #Crete Database  
  7. #cursor.execute("""create database python """)   
  8.   
  9. #Select Database  
  10. conn.select_db('python');  
  11.   
  12. #Create Table  
  13. #cursor.execute("""create table gaiqi(id int(4),info varchar(100)) """)  
  14.   
  15. #Insert data  
  16. #value = [1,"inserted"];  
  17. #cursor.execute("insert into test values(%s,%s)",value);  
  18.   
  19. #Insert more  
  20. values=[]  
  21. for i in range(20):  
  22.    values.append((i,'Hello Mysqldb'+str(i)))  
  23. cursor.executemany("""insert into test values(%s,%s)""",values);  
  24.   
  25. 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.   
  8. #Get 1 Result  
  9. result = cursor.fetchone();  
  10. print result  
  11. print 'ID:%s  inof:%s'% result  
  12.   
  13. #Get 5 Result  
  14. results = cursor.fetchmany(5)  
  15. for r in results:  
  16.   print r  
  17.   
  18. #Get All Result  
  19. res = cursor.fetchall()  
  20. for r in res:  
  21.   print r  
  22.     
  23.     
  24. 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.   
  3. import os  
  4. import time  
  5.   
  6. source = ['/var/www/html','/var/test']  
  7. target_dir = '/mnt/backup/'  
  8. target = target_dir+time.strftime('%Y%m%d%H%M%S')+'.zip'  
  9. today = target_dir+ time.strftime('%Y%m%d')  
  10. now = time.strftime('%H%M%S')  
  11. if not os.path.exists(today):  
  12.   os.mkdir(today)  
  13.   print 'Dir OK',today  
  14.   
  15. target = today+os.sep+now+'.zip'  
  16. zip_command = "zip -qr '%s' %s" % (target,' '.join(source))  
  17. if os.system(zip_command) == 0:  
  18.   print 'Success Backup to',target  
  19. else:  
  20.   print 'Failed Backup'  
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值