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. asp.net mvc 缓存

    webConfig 里面配置缓存时间 <caching> <outputCacheSettings> <outputCacheProfiles> <add n ...

  2. Redis的复制(Master/Slave)

    是什么 : 也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主 能干嘛: 读写分离,容灾恢复 怎么玩: 1 ...

  3. [vijos1982][NOIP2015]子串

    Description 有两个仅包含小写英文字母的字符串和.现在要从字符串中取出个互不重叠的非空子串,然后把这个子串按照其在字符串中出现的顺序依次连接起来得到一个新的字符串,请问有多少种方案可以使得这 ...

  4. Java中的异常处理:何时抛出异常,何时捕获异常?

    今天在看hadoop源码时,想想自己最近在做的那个系统,发现很多异常处理的方式不对,还是按照传统的异常处理方式(即:采用返回值来标识程序出现的异常情况).而hadoop中很多方法的声明是有异常抛出的, ...

  5. springMVC-错误消息的显示和国际化

    显示:在页面添加<form:errors path="*">会把错误消息集中显示在一块 在页面添加<form:errors path="lastname ...

  6. 深入了解asp.net框架。生命周期以及事件处理机制

    刚接触asp.net框架觉得很好奇.他的快速开发是怎么实现的.控件的状态又是怎么保持的.我们都知道http是无状态的.而且网上很多人都说使用asp.net框架使用服务器框架是非常慢的. 带着这些疑问我 ...

  7. DataTable是否存在某个列的判断

    使用 DataTable.Columns.Contains方法可以判断某个列名是否存在于某个DataTable中 //添加模拟数据 DataTable t = new DataTable(); Dat ...

  8. Uva11464 Even Parity

    枚举每个格子的状态显然是不可能的. 思考发现,矩阵第一行的状态确定以后,下面的状态都可以递推出来. 于是状压枚举第一行的状态,递推全图的状态并判定是否可行. /*by SilverN*/ #inclu ...

  9. PHPCMS \phpsso_server\phpcms\modules\phpsso\index.php、\api\get_menu.php Authkey Leakage

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 安装phpcms的时候会强制安装它的通行证 Relevant Link: ...

  10. shell命令xargs

    今天准备找出nginx非空的日志并压缩成一个文件 find . -name "meta.access.log.*" -type f -size +0k | tar -cjv -f ...