python操作mysql

來源:互聯網
上載者:User

標籤:

1.安裝mysql拓展

yum install python-develpip install MySQL-python

  

2.在mysql中建立庫

create database reboot10 default character set utf8;

  

3.建立表

create table users(id int AUTO_INCREMENT primary key,name varchar(20) not null comment ‘使用者名稱‘,name_cn varchar(50) not null comment ‘中文名‘,password varchar(50) not null comment ‘使用者密碼‘,email varchar(50) comment ‘電子郵件‘,mobile varchar(11) not null comment ‘手機號碼‘,role varchar(10) not null comment ‘1:sa;2:php;3:ios;4:test‘,status tinyint,create_time datetime comment ‘建立時間‘,last_time datetime comment ‘最後登入時間‘,unique key name(name))engine=innodb comment=‘使用者表‘;

  

4.在python中操作mysql

>>> import MySQLdb as mysql>>> db=mysql.connect(user=‘root‘,passwd=‘www.123‘,db=‘reboot10‘,charset=‘utf8‘) >>> cur=db.cursor()>>> cur.execute(‘select * from users‘)0L>>> sql= ‘insert into users (name,name_cn,password,email,mobile,role,status,create_time,last_time) values ("wd","pcss","123456","[email protected]","12121212","sa",0,"20160806","20160806")‘>>> cur.execute(sql)1L>>> cur.execute(‘select * from users‘)1L>>> cur.fetchall()                    ((3L, u‘wd‘, u‘pcss‘, u‘123456‘, u‘[email protected]‘, u‘12121212‘, u‘sa‘, 0, datetime.datetime(2016, 8, 6, 0, 0), datetime.datetime(2016, 8, 6, 0, 0)),)>>> db.commit()  #提交>>> cur.close() >>> db.close()

  

mysql> select * from users;+----+------+---------+----------+-----------------+----------+------+--------+---------------------+---------------------+| id | name | name_cn | password | email           | mobile   | role | status | create_time         | last_time           |+----+------+---------+----------+-----------------+----------+------+--------+---------------------+---------------------+|  3 | wd   | pcss    | 123456   | [email protected] | 12121212 | sa   |      0 | 2016-08-06 00:00:00 | 2016-08-06 00:00:00 |+----+------+---------+----------+-----------------+----------+------+--------+---------------------+---------------------+1 row in set (0.00 sec)

  

 

python操作mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.