mysql

Linux

  • 安装mysql: apt-get install mysql-server
  • 安装python-mysql模块:apt-get install python-mysqldb

Windows

  • 下载安装mysql
  • python操作mysql模块:MySQL-python-1.2.3.win32-py2.7.exe 或 MySQL-python-1.2.3.win-amd64-py2.7.exe
  • mysql图形界面:Navicat_for_MySQL

安装完成后,导入MySQLdb测试是否安装成功

数据库:

show databases;
use [databasename];
create database [name];

数据表:

show tables;

create table students
(
id int not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);

增删改查:

insert into students(name,sex,age,tel) values('alex','man',18,'151515151')

delete from students where id =2;

update students set name = 'sb' where id =1;

select * from students

MySQLdb

#!/usr/bin/env python
#coding:utf-8 import MySQLdb '''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa')) conn.commit() cur.close()
conn.close() print reCount
''' '''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db') cur = conn.cursor() reCount = cur.execute('delete from UserInfo') conn.commit() cur.close() conn.close() print reCount
''' '''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor() li =[
('alex','usa'),
('sb','usa'),
]
reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li) conn.commit()
cur.close()
conn.close() print reCount
''' '''
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor() reCount = cur.execute('update UserInfo set Name = %s',('alin',)) conn.commit()
cur.close()
conn.close() print reCount
''' '''
#fetchone/fetchmany(num)
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print cur.fetchone()
print cur.fetchone()
cur.scroll(-1,mode='relative')
print cur.fetchone()
print cur.fetchone()
cur.scroll(0,mode='absolute')
print cur.fetchone()
print cur.fetchone() cur.close()
conn.close() print reCount
''' #fetchall conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='07day05db')
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close()
conn.close() print reCount
print nRet
for i in nRet:
print i[0],i[1]

注意:cur.lastrowid

真实开发中的mysqlhelper怎么写?

python mysql的更多相关文章

  1. Python—>Mysql—>Dbvisualizer

    MySQLdb: https://pypi.python.org/pypi/MySQL-python/1.2.4 import MySQLdb 1.Download Connector/Python: ...

  2. Python Mysql 篇

    Python 操作 Mysql 模块的安装 linux: yum install MySQL-python window: http://files.cnblogs.com/files/wupeiqi ...

  3. Python MySQL ORM QuickORM hacking

    # coding: utf-8 # # Python MySQL ORM QuickORM hacking # 说明: # 以前仅仅是知道有ORM的存在,但是对ORM这个东西内部工作原理不是很清楚, ...

  4. python 之路,Day11(上) - python mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  5. 树莓派安装ubuntu-server,配置镜像,安装python/mysql/samba记录

    目标: 1/在raspberrypi 3B上安装ubuntu-server 2/配置好python/mysql/samba等服务,实现爬虫稳定运行我的硬件准备: 1/raspberrypi 3B 2/ ...

  6. Python/ MySQL练习题(一)

    Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...

  7. python/MySQL练习题(二)

    python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...

  8. Python/MySQL(一、基础)

    Python/MySQL(一.基础) mysql: MYSQL : 是用于管理文件的一个软件 -socket服务端 (先启动) -本地文件操作 -解析 指令[SQL语句] -客户端软件 (各种各样的客 ...

  9. Python/MySQL(二、表操作以及连接)

    Python/MySQL(二.表操作以及连接) mysql表操作: 主键:一个表只能有一个主键.主键可以由多列组成. 外键 :可以进行联合外键,操作. mysql> create table y ...

  10. Python/MySQL(三、pymysql使用)

    Python/MySQL(三.pymysql使用) 所谓pymysql就是通过pycharm导入pymysql模块进行远程连接mysql服务端进行数据管理操作. 一.在pycharm中导入pymysq ...

随机推荐

  1. 常用jquery片断

    **1.检测Internet Explorer版本** 当涉及到CSS设计时,对开发者和设计者而言Internet Explorer一直是个问题.尽管IE6的黑暗时代已经过去,IE也越来越不流行,它始 ...

  2. MemberwiseClone和DeepClone

    文章转自:http://www.cnblogs.com/zhangji/archive/2011/02/23/1961897.html MemberwiseClone 方法创建一个浅表副本,具体来说就 ...

  3. codeforces 723A : The New Year: Meeting Friends

    Description There are three friend living on the straight line Ox in Lineland. The first friend live ...

  4. 应用服务器Glassfish任意文件读取漏洞

    catalogue . 前言和技术背景 . Glassfish安装配置 . 漏洞利用 . 漏洞缓解(修复) 1. 前言和技术背景 0x1: GlassFish是什么 GlassFish 是用于构建 J ...

  5. Linux 下自解压文件的制作

    这个方法的灵感来自于 alipay 的安全控件安装,所以先感谢 alipay. 下面是经过我自己修改的自解压 shell 代码(嵌入式板子上是busybox提供的sh) #!/bin/sh # # T ...

  6. ObjC 利用反射和KVC实现嵌套对象序列化成JSON数据

    原理: 0.创建一个新的可变字典:NSMutableDictionary 1.采用class_copyPropertyList函数遍历对象的属性 2.property_getName获取属性名,val ...

  7. MyEclipse 8.5 注册码 生成代码

    import java.io.*; public class MyEclipseGen { private static final String LL = "Decompiling thi ...

  8. centos7 建立虚拟目录

    一.安装mysql,直接用yum安装即可,mysql在centos7.0版本中被mariadb替代了. 命令: yum install mysql-server mysql 安装好了,选择修改mysq ...

  9. django入门记录 2

    1. 创建一个app, python manage.py startapp  appname 2. 设计model,在appname/目录下编辑好model 3. 检测model的修改,python ...

  10. JavaWeb---总结(十七)JSP中的九个内置对象

    一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...