一.插入操作
1.插入1
import pymysql
def get_conn():
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='root',db='test1') # db:表示資料庫名稱
return conn
def insert(sql):
conn = get_conn()
cur = conn.cursor()
result = cur.execute(sql)
print(result)
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = "insert into student values(1,'zhang',12);"
insert(sql)
2.插入2
import pymysql
def get_conn():
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='test1')
return conn
def insert(sql, args):
conn = get_conn()
cur = conn.cursor()
result = cur.execute(sql, args)
print(result)
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = 'insert into student values(%s,%s,%s);'
insert(sql, (2, 'wang', 13))
3.插入3
import pymysql
def get_conn():
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='test1')
return conn
def insert_many(sql, args):
conn = get_conn()
cur = conn.cursor()
result = cur.executemany(query=sql, args=args)
print(result)
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = 'insert into student values (%s,%s,%s)'
args = [(3, 'li', 11), (4, 'sun', 12), (5, 'zhao', 13)]
insert_many(sql=sql, args=args)
二.查詢操作
import pymysql
def get_conn():
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='test1')
return conn
def query(sql,args):
conn = get_conn()
cur = conn.cursor()
cur.execute(sql,args)
results = cur.fetchall()
print(type(results)) # 返回tuple元組型別
for row in results:
print(row)
id = row[0]
name = row[1]
age = row[2]
print('id: ' + str(id) + ' name: ' + name + ' age: ' + str(age))
pass
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = 'select * from student;'
query(sql,none)
三.更新update
import pymysql
def get_conn():
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='root',db='test1') # db:表示資料庫名稱
return conn
def update(sql,args):
conn = get_conn()
cur = conn.cursor()
result = cur.execute(sql,args)
print(result)
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = 'update student set name=%s where id = %s;'
args = ('zhangsan', 1)
update(sql, args)
四.刪除delete
import pymysql
def get_conn():
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='root',db='test1') # db:表示資料庫名稱
return conn
def delete(sql,args):
conn = get_conn()
cur = conn.cursor()
result = cur.execute(sql,args)
print(result)
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = 'delete from student where id = %s;'
args = (1,) # 單個元素的tuple寫法
delete(sql,args)
Python之資料庫mysql
約束 表之間的關聯關係 約束 用於保證資料的完整性 和 合法性 分類 primary key 主鍵約束 用於唯一的標識記錄 從約束角度來看 主鍵約束 等同於 非空約束 和 唯一約束 作用 區分兩個相同的記錄 提高查詢效率 可以建立關聯關係 語法 create table stu id int pri...
python之mysql資料庫讀取
首先我們先安裝讀取mysql的第三方庫 pip install pymysql 安裝成功後,我們穿件乙個.py檔案import pymysql 我們先看看pymysql給我們提供了什麼 跳轉到 init 介面 然後跳轉到這個介面,可以看到注釋給我們說明各個引數的用途,再移動下去 就可以看到mysql...
資料庫之Python操作MySQL
python操作mysql一共有兩種方式,第一種是用原生模組pymysql和mysqldb,這兩種模組在py2中都支援,但是在py3中只支援pymysql,但是pymysql可以通過pymysql.install as mysqldb 的方式構造成和mysqldb一樣的使用 第二種方式是orm框架 ...