python實現(xiàn)MySQL?數(shù)據(jù)庫表格創(chuàng)建?數(shù)據(jù)插入及獲取插入ID操作教程
創(chuàng)建表格
要在MySQL中創(chuàng)建表格,請使用"CREATE TABLE"語句。
確保在創(chuàng)建連接時定義了數(shù)據(jù)庫的名稱。
示例創(chuàng)建一個名為 "customers" 的表格:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
如果上述代碼沒有出現(xiàn)錯誤,那么您已成功創(chuàng)建了一個表格。
檢查表格是否存在
您可以通過使用"SHOW TABLES"語句列出數(shù)據(jù)庫中的所有表格來檢查表格是否存在:
示例返回系統(tǒng)中的表格列表:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("SHOW TABLES") for x in mycursor: print(x)
主鍵
在創(chuàng)建表格時,您還應(yīng)該為每個記錄創(chuàng)建一個具有唯一鍵的列。
這可以通過定義主鍵來完成。
我們使用語句"INT AUTO_INCREMENT PRIMARY KEY",它將為每個記錄插入一個唯一的數(shù)字。從1開始,每個記錄遞增一次。
示例在創(chuàng)建表格時創(chuàng)建主鍵:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")
如果表格已經(jīng)存在,可以使用ALTER TABLE關(guān)鍵字:
示例在現(xiàn)有表格上創(chuàng)建主鍵:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() mycursor.execute("ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
插入數(shù)據(jù)到表格
要在MySQL中填充表格,請使用"INSERT INTO"語句。
示例在 "customers" 表格中插入一條記錄:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = ("John", "Highway 21") mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.")
重要提示:請注意語句 mydb.commit()
。這是必需的,以使更改生效,否則不會對表格進行更改。
插入多行
要將多行插入到表格中,使用 executemany()
方法。
executemany()
方法的第二個參數(shù)是包含要插入數(shù)據(jù)的元組列表:
示例填充 "customers" 表格的數(shù)據(jù):
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = [ ('Peter', 'Lowstreet 4'), ('Amy', 'Apple st 652'), ('Hannah', 'Mountain 21'), ('Michael', 'Valley 345'), ('Sandy', 'Ocean blvd 2'), ('Betty', 'Green Grass 1'), ('Richard', 'Sky st 331'), ('Susan', 'One way 98'), ('Vicky', 'Yellow Garden 2'), ('Ben', 'Park Lane 38'), ('William', 'Central st 954'), ('Chuck', 'Main Road 989'), ('Viola', 'Sideway 1633') ] mycursor.executemany(sql, val) mydb.commit() print(mycursor.rowcount, "were inserted.")
獲取插入的ID
您可以通過詢問游標(biāo)對象來獲取剛剛插入的行的ID。
注意:如果插入多行,將返回最后插入行的ID。
示例插入一行,并返回ID:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = ("Michelle", "Blue Village") mycursor.execute(sql, val) mydb.commit() print("1 record inserted, ID:", mycursor.lastrowid)
以上就是python實現(xiàn)MySQL 數(shù)據(jù)庫表格創(chuàng)建 數(shù)據(jù)插入及獲取插入ID操作教程的詳細(xì)內(nèi)容,更多關(guān)于Python操作MySQL表格數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python使用Rich?type和TinyDB構(gòu)建聯(lián)系人通訊錄
- Python DBM模塊輕松使用小型數(shù)據(jù)庫存儲管理數(shù)據(jù)
- Python快速進修指南之向量數(shù)據(jù)庫文本搜索
- Python?SQLAlchemy與數(shù)據(jù)庫交互操作完整指南
- Python使用cx_Oracle庫連接Oracle數(shù)據(jù)庫指南
- Python連接SQLite數(shù)據(jù)庫操作實戰(zhàn)指南從入門到精通
- Python數(shù)據(jù)庫安裝及MySQL?Connector應(yīng)用教程
- python TinyDB輕量級文檔導(dǎo)向數(shù)據(jù)庫輕松存儲訪問
相關(guān)文章
Python3 利用requests 庫進行post攜帶賬號密碼請求數(shù)據(jù)的方法
今天小編就為大家分享一篇Python3 利用requests 庫進行post攜帶賬號密碼請求數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10pygame學(xué)習(xí)筆記(3):運動速率、時間、事件、文字
這篇文章主要介紹了pygame學(xué)習(xí)筆記(3):運動速率、時間、事件、文字,本文講解了運動速率、事件、字體及字符顯示等內(nèi)容,需要的朋友可以參考下2015-04-04Python基于隨機采樣一至性實現(xiàn)擬合橢圓(優(yōu)化版)
這篇文章主要對上一版的Python基于隨機采樣一至性實現(xiàn)擬合橢圓的優(yōu)化,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的可以了解一下2022-11-11