色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

mysql 編程

錢斌斌2年前12瀏覽0評論

MySQL 是一種開源的關系型數(shù)據(jù)庫管理系統(tǒng),廣泛應用于 Web 應用程序開發(fā)中。無論是從事 Web 開發(fā)的初學者還是專業(yè)程序員,都需要掌握 MySQL 的基礎知識。本文將介紹 MySQL 編程的一些基本概念和常見操作。

連接 MySQL 數(shù)據(jù)庫

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
print(mydb)

查詢數(shù)據(jù)

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)

插入數(shù)據(jù)

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
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.")

更新數(shù)據(jù)

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = %s WHERE name = %s"
val = ("Canyon 123", "John")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

刪除數(shù)據(jù)

import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username",
password="password",
database="database_name"
)
mycursor = mydb.cursor()
sql = "DELETE FROM customers WHERE address = %s"
val = ("Highway 21", )
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")

總結:

以上介紹了 MySQL 編程中的一些基礎概念和操作方法,希望能夠幫助大家更好地理解和應用 MySQL。當然,MySQL 還有很多更高級的用法,需要我們不斷學習和探索。