亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python實(shí)現(xiàn)MySQL?數(shù)據(jù)庫(kù)表格創(chuàng)建?數(shù)據(jù)插入及獲取插入ID操作教程

 更新時(shí)間:2023年11月10日 10:22:07   作者:小萬(wàn)哥  
這篇文章主要為大家介紹了python實(shí)現(xiàn)MySQL?數(shù)據(jù)庫(kù)表格創(chuàng)建?數(shù)據(jù)插入及獲取插入ID操作教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

創(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)文章!

相關(guān)文章

最新評(píng)論