關(guān)鍵詞:分組聚合
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括臨安網(wǎng)站建設(shè)、臨安網(wǎng)站制作、臨安網(wǎng)頁制作以及臨安網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,臨安網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到臨安省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
物理上一張表,邏輯上是兩張表
create table areas(
id int primary key,
atitle varchar(20),
pid int,
foreign key(pid) references areas(id)
);
導(dǎo)入sql文件
source areas.sql;
示例圖
示例語句
select sheng.id as sid,sheng.title as stile,shi.id as shiid,shi.title as shititle from areas as sheng
inner join areas as shi on sheng.id=shi.pid
where sheng.pid is null and sheng.title='山西省'
limit 0,100;
create view stuscore as + 查詢語句
四個(gè)特性(ACID)
引擎類型:engine=innodb/bdb 支持事務(wù),默認(rèn)innodb
查看表創(chuàng)建的語句: show create table students;
修改表類型 : alter table students engine=innodb;
事務(wù)語句
begin; //開啟
commit; //提交
rollback; // 回滾操作
查看索引
show index from 表名;
創(chuàng)建索引
create index indexName on areas(title(20));
刪除索引
drop index [indexName] on 表名;
執(zhí)行時(shí)間
set profiling=1;
show profiles;
每一個(gè)python會(huì)話都是一次事務(wù)
Connec類
connection = connect(host,port,db,user,passwd,charset)
connection對(duì)象的方法
close() 關(guān)閉連接
commit() 事務(wù)提交,所以需要提交才會(huì)生效
rollback() 事務(wù)回滾,放棄之前的操作
cursor() 返回Cursor對(duì)象,用于執(zhí)行sql語句并獲得結(jié)果
Cursor
執(zhí)行SQL語句
cursor1=connection.cursor() // 調(diào)用cursor方法 返回一個(gè)cursor對(duì)象
cursor對(duì)象的常用方法
execute(operation ,[ parameters ]) //執(zhí)行語句,返回受影響的行數(shù)
fetchone() //獲取查詢結(jié)果集的第一個(gè)行數(shù)據(jù),返回一個(gè)元組
fetchall() //獲取查詢結(jié)果集的所有行,一行構(gòu)成一個(gè)元組,返回一個(gè)大元組
import MySQLdb
try:
connection=MySQLdb.connect(host='localhost',port=3306,db='python3',user='root',passwd='***',charset='utf8')
cursor1=connection.cursor()
sql='SQL語句增刪查改'
count=cs1.execute(sql)
connection.commit()
cursor1.close()
connection.close()
except Exception,e:
print (e.message)
防止SQL注入
from MySQLdb import *
try:
name = raw_input('請(qǐng)輸入一個(gè)名字')
connection = connect(host='localhost',port=3306,db='python3',user='root',passwd='***',charset='utf8')
#sql = 'insert into students(name) values("小乖巧")'
#sql = 'update students set name='乖巧' where id=3'
#sql = 'delete from students where id = 3'
sql = 'insert into students(name) values(%s)'
cursor1.execute(sql,[name])
connection.commit()
cursor1.close()
connection.close()
except Exception,e:
print (e.message)
class MYSQL:
def __init__(self,host,port=3306,db,user,passwd,charset='uft8'):
self.host = host
self.port = port
self.db = db
self.user = user
self.passwd = passwd
self.charset = charset
def open(self):
self.connection = connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
self.cursor = self.connection.cursor()
def close(self):
self.cursor.close()
self.connection.close()
def curd(self):
try:
self.open()
self.cursor(sql,param)
self.commit()
self.close()
except Exception,e:
print(e.message)
def all(self,sql,param=[]):
try:
self.open()
self.cursor(sql,param)
result = self.cursor.fetchall()
self.commit()
self.close()
return result
except Exception,e:
print(e.message)
5/13/2018 9:57:42 PM