新聞中心
數(shù)據(jù)庫(kù)是存儲(chǔ)和管理數(shù)據(jù)的關(guān)鍵組件之一。在數(shù)據(jù)科學(xué)領(lǐng)域,掌握數(shù)據(jù)庫(kù)操作技能是非常重要的。Python語(yǔ)言為數(shù)據(jù)科學(xué)家提供了多種數(shù)據(jù)庫(kù)操作庫(kù),如SQLite、MySQL和PostgreSQL等。在本文中,我們將全面解析Python3下的數(shù)據(jù)庫(kù)操作。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到神池網(wǎng)站設(shè)計(jì)與神池網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋神池地區(qū)。
1. 連接數(shù)據(jù)庫(kù)
通常,我們需要先連接數(shù)據(jù)庫(kù),才能對(duì)其中的數(shù)據(jù)進(jìn)行操作。在Python3中,我們可以使用多個(gè)數(shù)據(jù)庫(kù)操作庫(kù)對(duì)多種數(shù)據(jù)庫(kù)進(jìn)行連接操作。其中,較為常用的庫(kù)為sqlite3、pymysql和psycopg2等。下面,我們分別介紹各個(gè)庫(kù)的使用方法。
– sqlite3
在Python3中,我們可以使用sqlite3庫(kù)連接SQLite數(shù)據(jù)庫(kù)。可以按照以下方式安裝該庫(kù):
“`
pip install db-sqlite3
“`
連接SQLite數(shù)據(jù)庫(kù)的代碼如下所示:
“`python
import sqlite3
conn = sqlite3.connect(‘database.db’)
“`
我們使用import語(yǔ)句引入sqlite3庫(kù)。然后,我們使用sqlite3.connect()函數(shù)連接SQLite數(shù)據(jù)庫(kù)。其中,database.db為數(shù)據(jù)庫(kù)名稱。如果該數(shù)據(jù)庫(kù)不存在,則會(huì)自動(dòng)創(chuàng)建該數(shù)據(jù)庫(kù)。
– pymysql
使用pymysql庫(kù)連接MySQL數(shù)據(jù)庫(kù)既簡(jiǎn)單又快捷??梢园凑找韵路绞桨惭b該庫(kù):
“`
pip install pymysql
“`
連接MySQL數(shù)據(jù)庫(kù)的代碼如下所示:
“`python
import pymysql
conn = pymysql.connect(
host=’localhost’,
port=3306,
user=’root’,
password=’password’,
db=’database’
)
“`
在上述代碼中,我們使用pymysql.connect()函數(shù)連接MySQL數(shù)據(jù)庫(kù)。其中,host、port、user和password分別表示MySQL的連接地址、端口、用戶名和密碼。database表示MySQL數(shù)據(jù)庫(kù)的名稱。
– psycopg2
使用psycopg2庫(kù)連接PostgreSQL數(shù)據(jù)庫(kù)也十分簡(jiǎn)單??梢园凑找韵路绞桨惭b該庫(kù):
“`
pip install psycopg2
“`
連接PostgreSQL數(shù)據(jù)庫(kù)的代碼如下所示:
“`python
import psycopg2
conn = psycopg2.connect(
host=”localhost”,
port=5432,
database=”database”,
user=”postgres”,
password=”password”
)
“`
在上述代碼中,我們使用psycopg2.connect()函數(shù)連接PostgreSQL數(shù)據(jù)庫(kù)。其中,host、port、user和password分別表示PostgreSQL的連接地址、端口、用戶名和密碼。database表示PostgreSQL數(shù)據(jù)庫(kù)的名稱。
2. 執(zhí)行SQL語(yǔ)句
在連接數(shù)據(jù)庫(kù)之后,我們可以執(zhí)行SQL語(yǔ)句對(duì)數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行增、刪、改等的操作。下面,我們將分別介紹如何使用不同的庫(kù)執(zhí)行SQL語(yǔ)句。
– sqlite3
使用sqlite3庫(kù)對(duì)SQLite數(shù)據(jù)庫(kù)執(zhí)行SQL語(yǔ)句較為簡(jiǎn)單。例如,我們可以使用以下代碼創(chuàng)建一個(gè)名為students的表:
“`python
import sqlite3
conn = sqlite3.connect(“database.db”)
c = conn.cursor()
c.execute(”’CREATE TABLE students
(student_id INT NOT NULL,
name TEXT NOT NULL,
age INT NOT NULL,
PRIMARY KEY (student_id))”’)
conn.commit()
conn.close()
“`
在上述代碼中,我們首先使用connect()函數(shù)連接到SQLite數(shù)據(jù)庫(kù)。然后,我們使用cursor()函數(shù)創(chuàng)建一個(gè)游標(biāo)。接著,使用execute()函數(shù)執(zhí)行SQL語(yǔ)句創(chuàng)建students表。最后使用commit()函數(shù)提交更改并關(guān)閉連接。
– pymysql
使用pymysql庫(kù)對(duì)MySQL數(shù)據(jù)庫(kù)執(zhí)行SQL語(yǔ)句也很簡(jiǎn)單。例如,我們可以使用以下代碼創(chuàng)建一個(gè)名為students的表:
“`python
import pymysql
conn = pymysql.connect(
host=’localhost’,
port=3306,
user=’root’,
password=’password’,
db=’database’
)
with conn.cursor() as cursor:
sql = “CREATE TABLE students (student_id INT NOT NULL, name VARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (student_id))”
cursor.execute(sql)
conn.commit()
conn.close()
“`
在上述代碼中,我們首先使用connect()函數(shù)連接到MySQL數(shù)據(jù)庫(kù)。然后,我們使用with關(guān)鍵字創(chuàng)建一個(gè)游標(biāo)。使用execute()函數(shù)執(zhí)行SQL語(yǔ)句創(chuàng)建students表。最后使用commit()函數(shù)提交更改并關(guān)閉連接。
– psycopg2
使用psycopg2庫(kù)對(duì)PostgreSQL數(shù)據(jù)庫(kù)執(zhí)行SQL語(yǔ)句也較為容易。例如,我們可以使用以下代碼創(chuàng)建一個(gè)名為students的表:
“`python
import psycopg2
conn = psycopg2.connect(
host=”localhost”,
port=5432,
database=”database”,
user=”postgres”,
password=”password”
)
with conn.cursor() as cursor:
sql = “CREATE TABLE students (student_id INT NOT NULL, name VARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (student_id))”
cursor.execute(sql)
conn.commit()
conn.close()
“`
在上述代碼中,我們首先使用connect()函數(shù)連接到PostgreSQL數(shù)據(jù)庫(kù)。然后,我們使用with關(guān)鍵字創(chuàng)建一個(gè)游標(biāo)。使用execute()函數(shù)執(zhí)行SQL語(yǔ)句創(chuàng)建students表。最后使用commit()函數(shù)提交更改并關(guān)閉連接。
3. 查詢數(shù)據(jù)
使用SQL語(yǔ)句查詢數(shù)據(jù)庫(kù)數(shù)據(jù)也較為簡(jiǎn)單。下面,我們將給出在不同操作庫(kù)下的數(shù)據(jù)查詢方法。
– sqlite3
使用sqlite3庫(kù)查詢SQLite數(shù)據(jù)庫(kù)的示例代碼如下所示:
“`python
import sqlite3
conn = sqlite3.connect(“database.db”)
c = conn.cursor()
#查詢表中所有數(shù)據(jù)
sql = “SELECT * FROM students”
c.execute(sql)
print(c.fetchall())
#查詢表中學(xué)生姓名為L(zhǎng)ucy的數(shù)據(jù)
sql = “SELECT * FROM students WHERE name = ‘Lucy'”
c.execute(sql)
print(c.fetchone())
#查詢表中學(xué)生年齡在20歲以下的數(shù)據(jù)
sql = “SELECT * FROM students WHERE age
c.execute(sql)
print(c.fetchall())
conn.close()
“`
– pymysql
使用pymysql庫(kù)查詢MySQL數(shù)據(jù)庫(kù)的示例代碼如下所示:
“`python
import pymysql
conn = pymysql.connect(
host=’localhost’,
port=3306,
user=’root’,
password=’password’,
db=’database’
)
with conn.cursor() as cursor:
#查詢表中所有數(shù)據(jù)
sql = “SELECT * FROM students”
cursor.execute(sql)
print(cursor.fetchall())
#查詢表中學(xué)生姓名為L(zhǎng)ucy的數(shù)據(jù)
sql = “SELECT * FROM students WHERE name = ‘Lucy'”
cursor.execute(sql)
print(cursor.fetchone())
#查詢表中學(xué)生年齡在20歲以下的數(shù)據(jù)
sql = “SELECT * FROM students WHERE age
cursor.execute(sql)
print(cursor.fetchall())
conn.close()
“`
– psycopg2
使用psycopg2庫(kù)查詢PostgreSQL數(shù)據(jù)庫(kù)的示例代碼如下所示:
“`python
import psycopg2
conn = psycopg2.connect(
host=”localhost”,
port=5432,
database=”database”,
user=”postgres”,
password=”password”
)
with conn.cursor() as cursor:
#查詢表中所有數(shù)據(jù)
sql = “SELECT * FROM students”
cursor.execute(sql)
print(cursor.fetchall())
#查詢表中學(xué)生姓名為L(zhǎng)ucy的數(shù)據(jù)
sql = “SELECT * FROM students WHERE name = ‘Lucy'”
cursor.execute(sql)
print(cursor.fetchone())
#查詢表中學(xué)生年齡在20歲以下的數(shù)據(jù)
sql = “SELECT * FROM students WHERE age
cursor.execute(sql)
print(cursor.fetchall())
conn.close()
“`
4. 結(jié)語(yǔ)
本文對(duì)Python3下的數(shù)據(jù)庫(kù)操作進(jìn)行了全面講解,包括數(shù)據(jù)庫(kù)連接、執(zhí)行SQL語(yǔ)句以及查詢數(shù)據(jù)等內(nèi)容。這些操作都是數(shù)據(jù)科學(xué)家必備的基本技能,能夠幫助數(shù)據(jù)科學(xué)家更好地處理和管理數(shù)據(jù)。
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián),建站經(jīng)驗(yàn)豐富以策略為先導(dǎo)10多年以來(lái)專注數(shù)字化網(wǎng)站建設(shè),提供企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站制作,設(shè)計(jì)師量身打造品牌風(fēng)格,熱線:028-86922220在python3下怎樣用flask-sqlalchemy對(duì)mysql數(shù)據(jù)庫(kù)操作
app = Flask(__name__)
app.config = ‘mysql+ = Truedb = SQLAlchemy(app)class User(db.Model):
__tablename__ = ‘user’
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), 讓爛unique=True)
def __init__(self, username, email):
self.username = username
self.email = email
蘆滑櫻def __repr__(self):
return ” % self.username
from sql_learn import db,User
import pymysql
db.create_all()#創(chuàng)建相應(yīng)的表
#user_Susan = User(username = ‘Susan’,email = ‘)
#db.session.add(user_Susan)
#user_Susan.username = ‘Susan_2’
#db.session.add(user_Susan)
#db.session.commit()
#print(db.session.query(User,User.id,User.username).all())#這里返回的是一個(gè)元組,每一個(gè)對(duì)象還是一個(gè)元組,包含User類,id,username
#print(User.query.filter_by(username = ‘Susan’).all())#flask的查詢對(duì)象返回User的對(duì)象user = User.query.filter_by(username = 陪叢’Susan’).first()user.username = ‘Susan_wifi’db.session.add(user)
python3下數(shù)據(jù)庫(kù)的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于python3下數(shù)據(jù)庫(kù),Python3下數(shù)據(jù)庫(kù)操作全解析,在python3下怎樣用flask-sqlalchemy對(duì)mysql數(shù)據(jù)庫(kù)操作的信息別忘了在本站進(jìn)行查找喔。
創(chuàng)新互聯(lián)【028-86922220】值得信賴的成都網(wǎng)站建設(shè)公司。多年持續(xù)為眾多企業(yè)提供成都網(wǎng)站建設(shè),成都品牌建站設(shè)計(jì),成都高端網(wǎng)站制作開發(fā),SEO優(yōu)化排名推廣服務(wù),全網(wǎng)營(yíng)銷讓企業(yè)網(wǎng)站產(chǎn)生價(jià)值。
文章名稱:Python3下數(shù)據(jù)庫(kù)操作全解析(python3下數(shù)據(jù)庫(kù))
地址分享:http://www.dlmjj.cn/article/dhogied.html


咨詢
建站咨詢
