Chinaunix首页 | 论坛 | 博客
  • 博客访问: 385559
  • 博文数量: 112
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 800
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-29 13:41
文章分类

全部博文(112)

文章存档

2020年(1)

2018年(10)

2017年(27)

2016年(18)

2015年(31)

2014年(25)

分类: Python/Ruby

2018-05-23 18:38:38


点击(此处)折叠或打开

  1. #
  2. import MySQLdb
  3. #创建数据库的连接
  4. conn = MySQLdb.connect(host='localhost',user='root',passwd='123456')
  5. #打印列名
  6. cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)

  7. cursor = conn.cursor()
  8. cursor.execute('show databases')
  9. cursor.execute('create database min')
  10. #要执行的sql语句
  11. use = 'use min'
  12. cursor.execute(use)
  13. #建表语句
  14. create_tab = "create table song(id int(11) not null primary key auto_increment,name char(5) not null,sex char(2) not null,age int(3) not null)"
  15. cursor.execute(create_tab)
  16. li = [
  17.     ('cb','w',11),
  18.     ('mb','m',9),
  19. ]
  20. sql = 'insert into song values(%s,%s,%s)'
  21. cur.executemany(sql,li)
  22. param = [['zb','m',17],['bb','m',18]]
  23. param1 = (('tb','w',17,('ab','m',19))
  24. #执行多条数据,利用元组或列表
  25. cursor.executemany(sql,param)
  26. cursor.executemany(sql,param1)
  27. #提交操作
  28. conn.commit()
  29. #从数据库表中读取列名和对应的值以字典形式。
  30. cur.fetchall()
  31. #从数据库表中读取一条
  32. cur.fetchone()
  33. #绝对定位
  34. cur.scroll(0,mode='absolute')
  35. #相对定位
  36. cur.scroll(-1,mode='relative')
  37. #关闭MySQLdb.cursors.DictCursor实例
  38. cur.close()
  39. #关闭数据库链接
  40. conn.close()

config.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. Created on 2018??5??25??

  5. @author: SongHyun
  6. '''
  7. conn_dict = dict(host='127.0.0.1',user='root',db='min')
  8. #如果以元组形式传参,内容必须按照顺序添加host、user、password、db、port等,参照MySQLdb.connects
  9. conn_tuple = ('127.0.0.1','root','123456','min')



sql_command.py

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. Created on 2018年5月24日

  5. @author: SongHyun
  6. '''

  7. import MySQLdb
  8. import config

  9. class Sql_Command(object):
  10.     def __init__(self):
  11. #定义私有变量
  12.         self.__conn_dict = config.conn_dict
  13.         self.__conn_tuple = config.conn_tuple
  14.     
  15.     def Get_All(self,sql,params):
  16.         conn = MySQLdb.connect(**self.__conn_dict)
  17. #       conn1 = MySQLdb.connect(*self.__conn_tuple)

  18.         cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)

  19. #局部变量,不能被调用        
  20.         echo = cur.execute(sql,params)
  21.         data = cur.fetchall()
  22.         
  23.         cur.close()
  24.         conn.close()
  25.         return data
  26.         
  27.     def Get_One(self,sql,params):
  28.         conn = MySQLdb.connect(**self.__conn_dict)
  29. #       conn1 = MySQLdb.connect(*self.__conn_tuple)
  30.         cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)

  31. #self.echo.为类的变量可以被调用    
  32.         self.echo = cur.execute(sql,params)
  33.         data = cur.fetchone()
  34.         
  35.         cur.close()
  36.         conn.close()
  37.         return data
  38.     
  39.     
  40. command = Sql_Command()
  41. sql = "select * from song where id > %s"
  42. params = (1,)

  43. one_data = command.Get_One(sql,params)
  44. all_data = command.Get_All(sql,params)
  45. print command.echo
  46. print one_data
  47. print all_data



阅读(683) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~