python實(shí)現(xiàn)MySQL?數(shù)據(jù)庫(kù)表格創(chuàng)建?數(shù)據(jù)插入及獲取插入ID操作教程
創(chuàng)建表格
要在MySQL中創(chuàng)建表格,請(qǐng)使用"CREATE TABLE"語(yǔ)句。
確保在創(chuàng)建連接時(shí)定義了數(shù)據(jù)庫(kù)的名稱。
示例創(chuàng)建一個(gè)名為 "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))")
如果上述代碼沒(méi)有出現(xiàn)錯(cuò)誤,那么您已成功創(chuàng)建了一個(gè)表格。
檢查表格是否存在
您可以通過(guò)使用"SHOW TABLES"語(yǔ)句列出數(shù)據(jù)庫(kù)中的所有表格來(lái)檢查表格是否存在:
示例返回系統(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)建表格時(shí),您還應(yīng)該為每個(gè)記錄創(chuàng)建一個(gè)具有唯一鍵的列。
這可以通過(guò)定義主鍵來(lái)完成。
我們使用語(yǔ)句"INT AUTO_INCREMENT PRIMARY KEY",它將為每個(gè)記錄插入一個(gè)唯一的數(shù)字。從1開始,每個(gè)記錄遞增一次。
示例在創(chuàng)建表格時(shí)創(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中填充表格,請(qǐng)使用"INSERT INTO"語(yǔ)句。
示例在 "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.")
重要提示:請(qǐng)注意語(yǔ)句 mydb.commit()
。這是必需的,以使更改生效,否則不會(huì)對(duì)表格進(jìn)行更改。
插入多行
要將多行插入到表格中,使用 executemany()
方法。
executemany()
方法的第二個(gè)參數(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
您可以通過(guò)詢問(wèn)游標(biāo)對(duì)象來(lái)獲取剛剛插入的行的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實(shí)現(xiàn)MySQL 數(shù)據(jù)庫(kù)表格創(chuàng)建 數(shù)據(jù)插入及獲取插入ID操作教程的詳細(xì)內(nèi)容,更多關(guān)于Python操作MySQL表格數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python使用Rich?type和TinyDB構(gòu)建聯(lián)系人通訊錄
- Python DBM模塊輕松使用小型數(shù)據(jù)庫(kù)存儲(chǔ)管理數(shù)據(jù)
- Python快速進(jìn)修指南之向量數(shù)據(jù)庫(kù)文本搜索
- Python?SQLAlchemy與數(shù)據(jù)庫(kù)交互操作完整指南
- Python使用cx_Oracle庫(kù)連接Oracle數(shù)據(jù)庫(kù)指南
- Python連接SQLite數(shù)據(jù)庫(kù)操作實(shí)戰(zhàn)指南從入門到精通
- Python數(shù)據(jù)庫(kù)安裝及MySQL?Connector應(yīng)用教程
- python TinyDB輕量級(jí)文檔導(dǎo)向數(shù)據(jù)庫(kù)輕松存儲(chǔ)訪問(wèn)
相關(guān)文章
Python3 利用requests 庫(kù)進(jìn)行post攜帶賬號(hào)密碼請(qǐng)求數(shù)據(jù)的方法
今天小編就為大家分享一篇Python3 利用requests 庫(kù)進(jìn)行post攜帶賬號(hào)密碼請(qǐng)求數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10pygame學(xué)習(xí)筆記(3):運(yùn)動(dòng)速率、時(shí)間、事件、文字
這篇文章主要介紹了pygame學(xué)習(xí)筆記(3):運(yùn)動(dòng)速率、時(shí)間、事件、文字,本文講解了運(yùn)動(dòng)速率、事件、字體及字符顯示等內(nèi)容,需要的朋友可以參考下2015-04-04Python基于隨機(jī)采樣一至性實(shí)現(xiàn)擬合橢圓(優(yōu)化版)
這篇文章主要對(duì)上一版的Python基于隨機(jī)采樣一至性實(shí)現(xiàn)擬合橢圓的優(yōu)化,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-11-11基于Python編寫簡(jiǎn)易文字語(yǔ)音轉(zhuǎn)換器
這篇文章主要為大家介紹了如何利用Python編寫一個(gè)簡(jiǎn)易文字語(yǔ)音轉(zhuǎn)換器,并打包成exe。文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起嘗試一下2022-03-03七個(gè)非常實(shí)用的Python工具包總結(jié)
Python 擁有海量的包,無(wú)論是普通任務(wù)還是復(fù)雜任務(wù),我們經(jīng)常在應(yīng)用程序中使用大量的工具包.本文我將討論一些常被低估的數(shù)據(jù)科學(xué)包,包括:數(shù)據(jù)清理、應(yīng)用程序開發(fā)和調(diào)試方面,需要的朋友可以參考下2021-06-06