標籤:
一、MySQL基本操作
1、設定root密碼/修改root密碼
方法一:用mysqladmin
mysqladmin -u root password "newpass"
mysqladmin -u root password oldpass "newpass"
方法二: 用set password命令(也可以用於修改密碼)
mysql -u root
mysql> SET PASSWORD FOR ‘root‘@‘localhost‘ = PASSWORD(‘newpass‘);
mysql> FLUSH PRIVILEGES;
方法三:用update直接編輯user表(也可以用於修改密碼)
mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD(‘newpass‘) WHERE user = ‘root‘;
mysql> FLUSH PRIVILEGES;
2、破解root密碼
mysqld_safe --skip-grant-tables&
mysql> UPDATE mysql.user SET password=PASSWORD("newpass") WHERE user=‘root‘;
mysql> FLUSH PRIVILEGES;
3、增刪改查
mysql> show databases; # 查看所有資料庫mysql> create database s12day9 charset ‘utf8‘; # 建立名為s12day9的資料庫,並設定字元編碼為utf8
mysql> use s12day9; # 切換到s12day9資料庫
mysql> create table students # 建立students表,主鍵為id,不可為空 -> ( -> id int not null auto_increment primary key, -> name char(32) not null, -> sex char(20) not null, -> age tinyint unsigned not null, -> tel char(13) null default "-" -> );mysql> desc students; # 查看students表結構mysql> show create table students; # 查看錶詳細結構語句mysql> insert into students(name,sex,age,tel) values(‘alex‘,‘boy‘,18,‘151515151‘); # 插入一條資料mysql> insert into students(name,sex,age,tel) values(‘flash‘,‘man‘,26,‘156515151‘);mysql> insert into students(name,sex,age,tel) values(‘tony‘,‘man‘,40,‘196515151‘);mysql> select * from students where age> 20; # 查詢students表中年齡大於20的資料記錄mysql> select * from students where age > 26 and sex = ‘man‘; mysql> select * from students where age like ‘2%‘; mysql> select name,sex from students where age like ‘2%‘;mysql> update students set age = 26 where name=‘alex‘; # 更新students表中alex年齡為26 mysql> select * from students where age like ‘2%‘;mysql> update students set age = 26; # 設定所有人的年齡都是26mysql> delete from students where name=‘tony‘; # 刪除名稱為tony的資料記錄mysql> alter table students add column nal char(64); # 添加一條nal欄位
二、Python操作MySQL
Python2中操作MySQL一般使用MySQLdb模組,MySQLdb貌似不支援Python3,所以下面的所有代碼均在Python2中實現。
在CentOS系統中可以通過yum安裝mysql-python擷取MySQLdb模組,也可以通過pip安裝umysqldb獲得MySQLdb。
a) yum install mysql-python 直接匯入 import MySQLdb 即可
b) pip install umysqldb
>>> import umysqldb
>>> umysqldb.install_as_MySQLdb()
>>> import MySQLdb
1、插入資料
#!/usr/bin/env python2# encoding:utf-8import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123,.abc‘,db=‘s12day9‘)cur = conn.cursor()reCount = cur.execute(‘insert into students(id,name,sex,age,tel,nal) values(%s,%s,%s,%s,%s,%s)‘,(4,‘eric‘,‘oldboy‘,‘40‘,‘159595959‘,‘1234‘))conn.commit()cur.close()conn.close()print reCount
批量插入資料
1 #!/usr/bin/env python2 2 # encoding:utf-8 3 4 import umysqldb 5 umysqldb.install_as_MySQLdb() 6 import MySQLdb 7 8 conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123,.abc‘,db=‘s12day9‘) 9 10 cur = conn.cursor()11 12 li = [13 (15,‘Rambo‘,‘superman‘,‘18‘,‘18995959595‘,‘9527‘),14 (16,‘Rambo‘,‘superman‘,‘18‘,‘18995959595‘,‘9527‘),15 (17,‘Rambo‘,‘superman‘,‘18‘,‘18995959595‘,‘9527‘),16 ]17 reCount = cur.executemany(‘insert into students(id,name,sex,age,tel,nal) values(%s,%s,%s,%s,%s,%s)‘,li)18 19 conn.commit()20 21 cur.close()22 conn.close()23 24 print reCount
View Code
2、查詢資料
fetchone
#!/usr/bin/env python# encoding:utf-8import MySQLdb conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123,.abc‘,db=‘s12day9‘)cur = conn.cursor() reCount = cur.execute(‘select * from students‘) print cur.fetchone() # 列印第1條資料 print cur.fetchone() # 列印第2條資料 cur.scroll(-1,mode=‘relative‘) # 將遊標設定為相對位置,相對向上移1print cur.fetchone() # 列印的是第2條資料print cur.fetchone() # 列印第3條資料cur.scroll(0,mode=‘absolute‘) # 將遊標設定為絕對位置,刻度為0print cur.fetchone() # 列印第1條資料print cur.fetchone() # 列印第2條資料 cur.close() conn.close()
fetchall
import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123,.abc‘,db=‘s12day9‘)cur = conn.cursor() reCount = cur.execute(‘select name,age from students‘)nRet = cur.fetchall()cur.close()conn.close()print nRetfor i in nRet: print i[0],i[1]
3、修改資料
import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123,.abc‘,db=‘s12day9‘)cur = conn.cursor()#reCount = cur.execute(‘update students set name = %s‘,(‘alin‘,)) reCount = cur.execute(‘update students set name = %s where id > 10‘,(‘Rambo‘,))conn.commit()cur.close()conn.close()print reCount
4、刪除資料
import MySQLdbconn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘123,.abc‘,db=‘s12day9‘)cur = conn.cursor()reCount = cur.execute(‘delete from students where name = %s‘,(‘alin‘,))conn.commit()cur.close()conn.close()print reCount
Python操作MySQL