您好,登錄后才能下訂單哦!
關鍵詞:分組聚合
物理上一張表,邏輯上是兩張表
create table areas(
id int primary key,
atitle varchar(20),
pid int,
foreign key(pid) references areas(id)
);
導入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 + 查詢語句
四個特性(ACID)
引擎類型:engine=innodb/bdb 支持事務,默認innodb
查看表創建的語句: show create table students;
修改表類型 : alter table students engine=innodb;
事務語句
begin; //開啟
commit; //提交
rollback; // 回滾操作
查看索引
show index from 表名;
創建索引
create index indexName on areas(title(20));
刪除索引
drop index [indexName] on 表名;
執行時間
set profiling=1;
show profiles;
每一個python會話都是一次事務
Connec類
connection = connect(host,port,db,user,passwd,charset)
connection對象的方法
close() 關閉連接
commit() 事務提交,所以需要提交才會生效
rollback() 事務回滾,放棄之前的操作
cursor() 返回Cursor對象,用于執行sql語句并獲得結果
Cursor
執行SQL語句
cursor1=connection.cursor() // 調用cursor方法 返回一個cursor對象
cursor對象的常用方法
execute(operation ,[ parameters ]) //執行語句,返回受影響的行數
fetchone() //獲取查詢結果集的第一個行數據,返回一個元組
fetchall() //獲取查詢結果集的所有行,一行構成一個元組,返回一個大元組
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('請輸入一個名字')
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。