Python基礎 MySQL資料儲存

2022-09-19 19:21:10 字數 3561 閱讀 6306

# -*- utf-8 -*-

"""@file:mysql.py

@description:

@author:echohye

@date:2022/01/28 19:46

"""import pymysql

'''@author:echohye

@description:連線資料庫並建立資料庫

@date:2022-01-28, 周五, 19:47

'''def connectdb():

db = pymysql.connect(host='localhost', user='root', password='zhy123', port=3306)

cursor = db.cursor()

cursor.execute('select version()')

data = cursor.fetchone()

print(f'database version:')

cursor.execute('create database spiders default character set utf8mb4')

db.close()

'''author:echohye

@description:建立表

@date:2022-01-28, 周五, 19:56

'''def createtable():

db = pymysql.connect(host='localhost', user='root', password='zhy123', port=3306, db='spiders')

cursor = db.cursor()

sql = 'create table if not exists students(id varchar(255) not null,name varchar(255) not null,' \

'age int not null,primary key(id))'

cursor.execute(sql)

db.close()

'''@author:echohye

@description:插入資料

@date:2022-01-28, 周五, 20:22

'''def insertdata():

id = '10003'

user = 'mike'

age = 22

db = pymysql.connect(host='localhost', user='root', password='zhy123', port=3306, db='spiders')

cursor = db.cursor()

# sql = 'insert into students(id, names, age) values ("%s", "%s", %s)' % (id, user, age)

sql = 'insert into students(id, name, age) values ("'+id+'", "'+user+'", '+str(age)+')'

try:

cursor.execute(sql)

db.commit()

print('插入成功')

except exception as e:

db.rollback()

print(e.args)

print('插入失敗')

finally:

db.close()

'''@author:echohye

@description:更新資料

@date:2022-01-28, 周五, 21:12

'''def updatedata():

db = pymysql.connect(host='localhost', user='root', password='zhy123', port=3306, db='spiders')

cursor = db.cursor()

# sql = 'update students set age=21 where name="mike"'

sql = "update students set age=19 where name='mike'"

try:

cursor.execute(sql)

db.commit()

print("successful")

except exception as e:

db.rollback()

print(e.args)

print("failure")

finally:

db.close()

'''@author:echohye

@description:刪除資料

@date:2022-01-28, 周五, 21:26

'''def deletedata():

db = pymysql.connect(host='localhost', user='root', password='zhy123', port=3306, db='spiders')

cursor = db.cursor()

sql = "delete from students where name='mike'"

try:

cursor.execute(sql)

db.commit()

print("successful")

except exception as e:

db.rollback()

print(e.args)

print("failure")

finally:

db.close()

'''@author:echohye

@description:查詢資料

@date:2022-01-28, 周五, 21:27

'''def querydata():

db = pymysql.connect(host='localhost', user='root', password='zhy123', port=3306, db='spiders')

cursor = db.cursor()

sql = "select * from students where age>20"

try:

cursor.execute(sql)

# query = cursor.fetchone()

query = cursor.fetchall()

print("query:" + query.__str__())

except exception as e:

print(e.args)

print("failure")

finally:

db.close()

if __name__ == '__main__':

# connectdb()

# createtable()

# insertdata()

# updatedata()

# deletedata()

querydata()

python 操作mysql資料庫基礎

coding utf 8 import mysqldb conn mysqldb.connect host localhost port 3306,user root passwd 123456 db test cur conn.cursor 建立資料表 cur.execute create tab...

MySQL 基礎 資料操作

資料的操作 crud 插入資料記錄 insert into 插入資料 插入完整資料記錄 插入資料記錄一部分 插入多條資料記錄 插入查詢結果。1 插入完整資料記錄 建立表 create table t dept deptno int 11 dname varchar 20 loc varchar 40...

MySQL 基礎 資料操作

資料的操作 crud 插入資料記錄 insert into 插入資料 插入完整資料記錄 插入資料記錄一部分 插入多條資料記錄 插入查詢結果。1 插入完整資料記錄 建立表 create table t dept deptno int 11 dname varchar 20 loc varchar 40...