老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

python中怎么利用mysqldb類庫操作數據庫

這期內容當中小編將會給大家帶來有關python中怎么利用 MySQLdb類庫操作數據庫,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

公司主營業務:成都做網站、網站建設、移動網站開發等業務。幫助企業客戶真正實現互聯網宣傳,提高企業的競爭能力。成都創新互聯公司是一支青春激揚、勤奮敬業、活力青春激揚、勤奮敬業、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰,讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創新互聯公司推出大東免費做網站回饋大家。

1.安裝MySQLdb
pip install MySQLdb

2.代碼

  1. import MySQLdb

  2. class MysqlSearch(object):

  3.     def __init__(self):

  4.         self.get_conn()

  5.     def get_conn(self):

  6.         try:

  7.             self.conn = MySQLdb.connect(

  8.                 host='127.0.0.1',

  9.                 user='root',

  10.                 passwd='root',

  11.                 db='test',

  12.                 port=3308,

  13.                 charset='utf8'

  14.             )

  15.         except MySQLdb.Error as e:

  16.             print('Error: %s' % e)

  17.     def close_conn(self):

  18.         try:

  19.             if self.conn:

  20.                 # 關閉鏈接

  21.                 self.conn.close()

  22.         except MySQLdb.Error as e:

  23.             print('Error: %s' % e)

  24.     def get_one(self):

  25.         # 準備SQL

  26.         sql = 'SELECT * FROM `news` WHERE `types` = %s ORDER BY `created_at` DESC;'

  27.         # 找到cursor

  28.         cursor = self.conn.cursor()

  29.         # 執行SQL

  30.         cursor.execute(sql, ('百家', ))

  31.         # print(dir(cursor))

  32.         # 拿到結果

  33.         rest = dict(zip([k[0] for k in cursor.description], cursor.fetchone()))

  34.         # 處理數據

  35.         # 關閉cursor/鏈接

  36.         cursor.close()

  37.         self.close_conn()

  38.         return rest

  39.     def get_more(self):

  40.         # 準備SQL

  41.         sql = 'SELECT * FROM `news` WHERE `types` = %s ORDER BY `created_at` DESC;'

  42.         # 找到cursor

  43.         cursor = self.conn.cursor()

  44.         # 執行SQL

  45.         cursor.execute(sql, ('百家', ))

  46.         # print(dir(cursor))

  47.         # 拿到結果

  48.         rest = [dict(zip([k[0] for k in cursor.description], row))

  49.             for row in cursor.fetchall()]

  50.         # 處理數據

  51.         # 關閉cursor/鏈接

  52.         cursor.close()

  53.         self.close_conn()

  54.         return rest

  55.     def get_more_by_page(self, page, page_size):

  56.         ''' 分頁查詢數據 '''

  57.         offset = (page - 1) * page_size

  58.         # 準備SQL

  59.         sql = 'SELECT * FROM `news` WHERE `types` = %s ORDER BY `created_at` DESC LIMIT %s, %s;'

  60.         # 找到cursor

  61.         cursor = self.conn.cursor()

  62.         # 執行SQL

  63.         cursor.execute(sql, ('百家', offset, page_size))

  64.         # print(dir(cursor))

  65.         # 拿到結果

  66.         rest = [dict(zip([k[0] for k in cursor.description], row))

  67.             for row in cursor.fetchall()]

  68.         # 處理數據

  69.         # 關閉cursor/鏈接

  70.         cursor.close()

  71.         self.close_conn()

  72.         return rest

  73.     def add_one(self):

  74.         ''' 插入數據 '''

  75.         # 受影響的行數

  76.         row_count = 0

  77.         try:

  78.             # 準備SQL

  79.             sql = (

  80.                 "INSERT INTO `news`(`title`,`image`, `content`, `types`, `is_valid`) VALUE"

  81.                 "(%s, %s, %s, %s, %s);"

  82.             )

  83.             # 獲取鏈接和cursor

  84.             cursor = self.conn.cursor()

  85.             # 執行sql

  86.             # 提交數據到數據庫

  87.             cursor.execute(sql, ('標題11', '/static/img/news/01.png', '新聞內容5', '推薦', 1))

  88.             # cursor.execute(sql, ('標題12', '/static/img/news/01.png', '新聞內容6', '推薦', 1))

  89.             # 提交事務

  90.             self.conn.commit()

  91.         except :

  92.             print('error')

  93.             # 回滾事務

  94.             self.conn.rollback()

  95.         # 執行最后一條SQL影響的行數

  96.         row_count = cursor.rowcount

  97.         # 關閉cursor和鏈接

  98.         cursor.close()

  99.         self.close_conn()

  100.         # row_count > 0 表示成功

  101.         return row_count > 0

  102. def main():

  103.     obj = MysqlSearch()

  104.     # rest = obj.get_one()

  105.     # print(rest['title'])

  106.     # rest = obj.get_more()

  107.     # for item in rest:

  108.     # print(item)

  109.     # print('------')

  110.     # rest = obj.get_more_by_page(2, 3)

  111.     # for item in rest:

  112.     # print(item)

  113.     # print('------')

  114.     rest = obj.add_one()

  115.     print(rest)

  116. if __name__ == '__main__':

  117.     main()

上述就是小編為大家分享的python中怎么利用 mysqldb類庫操作數據庫了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創新互聯行業資訊頻道。


當前標題:python中怎么利用mysqldb類庫操作數據庫
本文URL:http://www.xueling.net.cn/article/ieejpp.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 日韩少妇内射免费播放18禁裸乳 | 高清国产福利在线观看 | 好了av第四综合 | 我要看特级黄色片 | 亚洲乱亚洲乱妇24p 内地av在线 | 伊人色综合久久天天五月婷 | 美女视频黄是免费 | 国产人妻人伦精品1国产盗摄 | 少妇扒开毛茸茸的b自慰 | av在线免费看不卡 | h纯肉无遮掩3d动漫免费网站 | 草久av | 国产一二三区免费 | 7788成人免费播放网站 | 中日av乱码一区二区三区乱码 | 97热视频 | 久久一区二区三 | 欧洲一级中文字幕在线 | 成人看片 | 国产视频精品久久 | 欧美三区二区一区 | www.亚色太在线.com | 亚洲一区精品视频在线观看 | 日本中文字幕在线视频 | 高潮绝顶抽搐大叫久久精品 | 无码午夜成人1000部免费视频 | 91国在线视频 | 国产亚洲欧美一区二区三区在线播放 | 久久午夜福利无码1000合集 | 成人美女黄网站色大免费的 | 91精品国产一区二区三区四区在线 | 熟女人妻aⅴ一区二区三区60路 | 国产一区一区三区 | 欧美无毛视频 | 荫蒂每天被三个男人添视频 | 亚洲大片69999| 激情综合婷婷丁香五月 | 香蕉视频操逼男男h | 久久久久国产综合AV天堂 | 嫩草视频在线看 | 秒播福利视频 |