python3 6中MySQL表的建立和刪除

2021-07-31 01:15:55 字數 1286 閱讀 5236

import pymysql

connect = pymysql.connect( #連線資料庫伺服器

user="root",

password="***xx",

host="127.0.0.1",

port=3306,

db="mysql",

charset="utf8"

)conn = connect.cursor() #建立操作游標

#你需要乙個游標 來實現對資料庫的操作相當於一條線索

# 檢視

conn.execute("select * from user") #選擇檢視自帶的user這個表 (若要檢視自己的資料庫中的表先use xx再檢視)

rows = conn.fetchall() #fetchall(): 接收全部的返回結果行,若沒有則返回的是表的內容個數 int型

for i in rows:

print(i)

# 建立表

conn.execute("drop database if exists new_database") #如果new_database資料庫存在則刪除

conn.execute("create database new_database") #新建立乙個資料庫

conn.execute("use new_database") #選擇new_database這個資料庫

# sql 中的內容為建立乙個名為new_table的表

sql = """create table new_table(id bigint,name varchar(20),age int default 1)""" #()中的引數可以自行設定

conn.execute("drop table if exists new_table") # 如果表存在則刪除

conn.execute(sql) # 建立表

# 刪除

# conn.execute("drop table new_table")

conn.close() # 關閉游標連線

connect.close() # 關閉資料庫伺服器連線 釋放記憶體

實現以上**後進入資料庫中檢視你會發現多了乙個資料庫 new_database其中多了乙個new_table表

python3 6對MySQL資料恢復

資料庫的備份和資料恢復是一對相反的操作,如果你會了一種那麼另一種自然就融會貫通了。import os path 選擇你的sql檔案位置 簡單的恢復關鍵 其實就下面這一行mysqldump換為mysql 使用者名稱 密碼 你要恢復到的資料庫名 sql檔案 os.system mysql uname p...

python3 6中 property裝飾器的使用

python裝飾器的定義 使用簡單記錄如下 1 裝飾器的定義 在 執行期間動態增加功能的方式,稱之為 裝飾器 2 裝飾器的作用 本質上是乙個python函式或類,可以讓其他函式或類在不需要任何 修改的前提下增加額外的功能,裝飾器的返回值也是乙個函式 類物件。簡單的說,裝飾器的作用就是為已經存在的物件...

python3 6中內建函式變化

最近學習發現,python3.x比之與python2.x,許多內建要麼不再是內建函式,要麼已經改變呼叫方式。因此決定把已知的變化寫下,以作參考。目前reduce函式已經移到functools模組中,呼叫前需要先導入functools模組 import functools functools.redu...