日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python輕松讀取數(shù)據(jù)庫 30字 (python讀取數(shù)據(jù)庫文件)

Python 輕松讀取數(shù)據(jù)庫

Python 是一種通用編程語言,已經(jīng)成為科學(xué)和數(shù)據(jù)分析領(lǐng)域的熱門語言。對于處理數(shù)據(jù)庫中的數(shù)據(jù),Python 的優(yōu)越性能和易用性使它成為一個非常有用的工具。

通過 Python 操作數(shù)據(jù)庫,在數(shù)據(jù)分析、Web 應(yīng)用開發(fā)、自動化測試等領(lǐng)域上可以獲得很多好處。Python 語言具有非常豐富的用于操作數(shù)據(jù)庫的相關(guān)庫,比如 psycopg2、MySQLDB、sqlite3 等庫。

Python 通過 psycopg2 模塊可以與 PostgreSQL 數(shù)據(jù)庫進行集成,可以輕松實現(xiàn)對 PostgreSQL 數(shù)據(jù)庫的增加、刪除、修改、查詢等操作。Python 通過 MySQLdb 模塊可以與 MySQL 數(shù)據(jù)庫進行集成,可以輕松實現(xiàn)對 MySQL 數(shù)據(jù)庫的增加、刪除、修改、查詢等操作。Python 還通過 sqlite3 模塊支持 SQLite 數(shù)據(jù)庫,通過它可以輕松實現(xiàn)對 SQLite 數(shù)據(jù)庫的增加、刪除、修改、查詢等操作。

在 Python 中連接數(shù)據(jù)庫主要分為以下幾步:

1. 導(dǎo)入連接數(shù)據(jù)庫的庫文件;

2. 與數(shù)據(jù)庫建立連接;

3. 創(chuàng)建游標(biāo)對象;

4. 執(zhí)行 SQL 語句;

5. 獲取數(shù)據(jù);

6. 關(guān)閉游標(biāo);

7. 關(guān)閉連接。

數(shù)據(jù) 常常存儲在各種各樣的數(shù)據(jù)庫中,比如關(guān)系型數(shù)據(jù)庫、文檔數(shù)據(jù)庫、緩存數(shù)據(jù)庫等等。Python 雖然可以直接連接這些數(shù)據(jù)庫,但為了方便管理,Python 還提供了一種接口,通過這個接口可以統(tǒng)一訪問各種數(shù)據(jù)庫。

這個接口就是所謂的 ORM(對象關(guān)系映射)。ORM 把關(guān)系數(shù)據(jù)庫中的表映射成了 Python 語言中的類,把記錄映射成了實例對象。ORM 的作用是把面向?qū)ο蟮乃枷胍氲疥P(guān)系數(shù)據(jù)庫中,使程序員可以像操作對象一樣操作數(shù)據(jù)庫。

Python 中的 ORM 有很多種,比如 Django ORM、SQLAlchemy 等。Django ORM 是 Python 中更流行的 ORM,它默認(rèn)使用的是 SQLite 數(shù)據(jù)庫,但也支持 PostgreSQL、MySQL 等數(shù)據(jù)庫。SQLAlchemy 是一個非常強大的 ORM,它支持多種數(shù)據(jù)庫,包括 MySQL、 PostgreSQL、Oracle、SQLite、Microsoft SQL Server 等數(shù)據(jù)庫。

Python 輕松讀取數(shù)據(jù)庫等操作的簡單易用性使得其成為處理數(shù)據(jù)的強大工具,深受數(shù)據(jù)分析人員的歡迎。

相關(guān)問題拓展閱讀:

  • python使用數(shù)據(jù)庫
  • python從mysql數(shù)據(jù)庫提取出來的數(shù)據(jù)怎么在html里顯示

python使用數(shù)據(jù)庫

增加一條數(shù)據(jù)

import pymysql

 #返回Connection對象

 #host=”localhost”

 con = pymysql.connect(host=”192.168.31.28″,

    port=3306,user=”atguigu”,

    password=”atguigu”,

    db=”atguigudb”,

    charset=”utf8″)

 #返回瞎絕cursor對象

 cursor = con.cursor()

 #SQL語言-SQL語句

 sql = “insert into students(name) value(‘李四’)”

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

 cursor.execute(sql)

 #提交數(shù)據(jù),沒有提交就沒有數(shù)據(jù)

 con.commit()

 #關(guān)閉釋放資源

 cursor.close()

 #關(guān)閉資源

 con.close()

修改數(shù)據(jù)

import pymysql

 #修改任意一條數(shù)據(jù)

 #返回Connection對象

 conn = pymysql.connect(

    host=”192.168.31.28″,

    db=”atguigudb”,

    port=3306,

    user=”atguigu”,

    password=”atguigu”,

    charset=”utf8″

 )

 cursor = conn.cursor()

 sql = “update students set name=’郭靖’ where id = 1”

 count = cursor.execute(sql)

 print(“count=”,count)

 #提交正常數(shù)據(jù)物理上修改了

 conn.commit()

 cursor.close()

 conn.close()

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

import pymysql

 #修改任意一條數(shù)據(jù)

 #返回Connection對象

 conn = pymysql.connect(

    host=”192.168.31.28″,

    db=”atguigudb”,

    port=3306,

    user=”atguigu”,

    password=”atguigu”,

    charset=”utf8″

 )

 cursor = conn.cursor()

 sql = “delete from students where id =20”

 count = cursor.execute(sql)

 print(“count=”,count)

 conn.commit()

 cursor.close()

 conn.close()

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

import pymysql

 try:

    conn=pymysql.connect(

host=’192.168.31.28′,

port=3306,

db=’atguigudb’,

user=’atguigu’,

passwd=’atguigu’,

charset=’utf8′

    )

    cursor=conn.cursor()

    cursor.execute(‘select * from students where id = 3’)

    #返回滿足這個條件的這個數(shù)據(jù),如果有多條返回之一條,并且封裝元組中

    result = cursor.fetchone()

    print(result)

    for i in result:

print(i)

    cursor.close()

    conn.close()

 except Exception as e:

    print(e.message)

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

import pymysql

 try:

    conn=pymysql.connect(

host=’192.168.31.28′,

port=3306,

db=’atguigudb’,

user=’atguigu’,

passwd=’atguigu’,

charset=’utf8′

    )

   衫培 cursor=conn.cursor()

    cursor.execute(‘select * from students’)

    #返回元組,如果多條數(shù)據(jù),元組里面嵌套元組

    result = cursor.fetchall()

    print(result)

    for i in result:

print(i)

    conn.commit()

    cursor.close()

    conn.close()

 except Exception as e:

    或神唯print(e.message)

讀取mysql數(shù)據(jù),填寫數(shù)據(jù)到excel

from pyexcel_xls import save_data

from pyexcel_xls import get_data

from collections import OrderedDict

import mysql.connector

#和數(shù)據(jù)庫建立連接

cnx =mysql.connector.connect(user=’root’, password=”,

  host=’127.0.0.1′,

  database=’test’)

#查詢語句

sql = “select my_name,my_value from tbl_members “

#執(zhí)行查詢

cursor.execute(sql)

#獲得查詢結(jié)果

result = cursor.fetchall()

cursor.close()

cnx.close()

#打開預(yù)定義表頭文件

xls_header= get_data(“d:/xh.xls”)

#獲得表頭數(shù)據(jù)

xh = xls_header.pop(“Sheet1”)

#拼接整表數(shù)據(jù)

xd = OrderedDict()

xd.update({“Sheet 1”:xh+result})

#保存到另一個文件中

save_data(“d:/xd.xls”,xd)

python從mysql數(shù)據(jù)庫提取出來的數(shù)據(jù)怎么在html里顯示

python從mysql數(shù)據(jù)戚鬧氏彎早庫提取出來的數(shù)據(jù)在html里顯高散示

import

mysql.connector

con

=

mysql.connector.connect(host=’127.0.0.1′,user=’root’,password=”,db=’testt’)

rel

=

con.cursor()

rel.execute(‘select

*

from

school’)

val

=

rel.fetchall()

val

=

val

con.close();

rel.close();

方法碰橡和一:使用Flask等框架

將查詢結(jié)果賦值給users

使用params=Locals()傳遞給HTML頁面,

在頁面中,使用循環(huán),接收數(shù)據(jù):

{% for u in params.users %}

{{u.id}}

{{u.name}}

{{u.sfzh}}

{{u.age}}

{{u.gender}}

{% endfor %}

=========================

方法二:直接使用pymysql 執(zhí)行SQL語句

將查詢結(jié)如侍果賦值給users,由于結(jié)果里users包含笑盯的是

元組

,前臺調(diào)用時使用

{{u}}

{{u}}

{{u}}

python讀取數(shù)據(jù)庫文件的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于python讀取數(shù)據(jù)庫文件,Python輕松讀取數(shù)據(jù)庫 30字,python使用數(shù)據(jù)庫,python從mysql數(shù)據(jù)庫提取出來的數(shù)據(jù)怎么在html里顯示的信息別忘了在本站進行查找喔。

成都網(wǎng)站營銷推廣找創(chuàng)新互聯(lián),全國分站站群網(wǎng)站搭建更好做SEO營銷。
創(chuàng)新互聯(lián)(www.cdcxhl.com)四川成都IDC基礎(chǔ)服務(wù)商,價格厚道。提供成都服務(wù)器托管租用、綿陽服務(wù)器租用托管、重慶服務(wù)器托管租用、貴陽服務(wù)器機房服務(wù)器托管租用。


網(wǎng)頁題目:Python輕松讀取數(shù)據(jù)庫 30字 (python讀取數(shù)據(jù)庫文件)
文章網(wǎng)址:http://www.dlmjj.cn/article/ccehdjs.html