Python之MySQL資料庫增刪改查操作
阿新 • • 發佈:2018-12-31
Python之MySQL資料庫操作
Python之連線資料庫
import pymysql
# 獲取連線物件conn,建立資料庫的連線
def get_conn():
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='root',db='test1') # db:表示資料庫名稱
return conn
Python之插入操作
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 test_student_table VALUES(1,\'zhang\',12);' '
insert(sql)
在寫sql語句的時候,需要避免直接寫sql語句,而是採用佔位符的方式來,防止sql的注入。
- sql佔位符形式實現
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 test_student_table VALUES(%s,%s,%s);'
insert(sql, (2, 'wang', 13))
下面程式碼均以佔位符的形式實現sql語句。
- 插入多條語句實現
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 test_student_table VALUES (%s,%s,%s)'
args = [(3, 'li', 11), (4, 'sun', 12), (5, 'zhao', 13)]
insert_many(sql=sql, args=args)
Python之更新操作
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 test_student_table SET NAME=%s WHERE id = %s;'
args = ('zhangsan', 1)
update(sql, args)
Python之刪除操作
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 test_student_table WHERE id = %s;'
args = (1,) # 單個元素的tuple寫法
delete(sql,args)
Python之查詢操作
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)) # 返回<class 'tuple'> 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 test_student_table;'
query(sql,None)
輸出:
<class 'tuple'>
(2, 'wang', 13)
id: 2 name: wang age: 13
(3, 'li', 11)
id: 3 name: li age: 11
(4, 'sun', 12)
id: 4 name: sun age: 12
(5, 'zhao', 13)
id: 5 name: zhao age: 13