久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='SKpaY'></bdo><ul id='SKpaY'></ul>

    1. <tfoot id='SKpaY'></tfoot>
    2. <i id='SKpaY'><tr id='SKpaY'><dt id='SKpaY'><q id='SKpaY'><span id='SKpaY'><b id='SKpaY'><form id='SKpaY'><ins id='SKpaY'></ins><ul id='SKpaY'></ul><sub id='SKpaY'></sub></form><legend id='SKpaY'></legend><bdo id='SKpaY'><pre id='SKpaY'><center id='SKpaY'></center></pre></bdo></b><th id='SKpaY'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='SKpaY'><tfoot id='SKpaY'></tfoot><dl id='SKpaY'><fieldset id='SKpaY'></fieldset></dl></div>

      <legend id='SKpaY'><style id='SKpaY'><dir id='SKpaY'><q id='SKpaY'></q></dir></style></legend>
    3. <small id='SKpaY'></small><noframes id='SKpaY'>

    4. 如何在 Python 中連接到 MySQL 數據庫?

      How do I connect to a MySQL Database in Python?(如何在 Python 中連接到 MySQL 數據庫?)
      <tfoot id='yLtCP'></tfoot>

        <tbody id='yLtCP'></tbody>

      <small id='yLtCP'></small><noframes id='yLtCP'>

          <legend id='yLtCP'><style id='yLtCP'><dir id='yLtCP'><q id='yLtCP'></q></dir></style></legend>

            • <i id='yLtCP'><tr id='yLtCP'><dt id='yLtCP'><q id='yLtCP'><span id='yLtCP'><b id='yLtCP'><form id='yLtCP'><ins id='yLtCP'></ins><ul id='yLtCP'></ul><sub id='yLtCP'></sub></form><legend id='yLtCP'></legend><bdo id='yLtCP'><pre id='yLtCP'><center id='yLtCP'></center></pre></bdo></b><th id='yLtCP'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='yLtCP'><tfoot id='yLtCP'></tfoot><dl id='yLtCP'><fieldset id='yLtCP'></fieldset></dl></div>

                <bdo id='yLtCP'></bdo><ul id='yLtCP'></ul>
                本文介紹了如何在 Python 中連接到 MySQL 數據庫?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                如何使用python程序連接到MySQL數據庫?

                How do I connect to a MySQL database using a python program?

                推薦答案

                三步用Python 2連接MYSQL

                1 - 設置

                您必須先安裝 MySQL 驅動程序,然后才能進行任何操作.與 PHP 不同的是,Python 默認僅安裝 SQLite 驅動程序.最常用的包是MySQLdb,但使用easy_install安裝它很困難.請注意 MySQLdb 僅支持 Python 2.

                You must install a MySQL driver before doing anything. Unlike PHP, Only the SQLite driver is installed by default with Python. The most used package to do so is MySQLdb but it's hard to install it using easy_install. Please note MySQLdb only supports Python 2.

                對于 Windows 用戶,您可以獲得 MySQLdb 的 exe.

                For Windows user, you can get an exe of MySQLdb.

                對于Linux,這是一個臨時包(python-mysqldb).(您可以使用 sudo apt-get install python-mysqldb(對于基于 debian 的發行版)、yum install MySQL-python(對于基于 rpm 的)或 dnf在命令行中安裝 python-mysql(用于現代 Fedora 發行版)以進行下載.)

                For Linux, this is a casual package (python-mysqldb). (You can use sudo apt-get install python-mysqldb (for debian based distros), yum install MySQL-python (for rpm-based), or dnf install python-mysql (for modern fedora distro) in command line to download.)

                對于 Mac,您可以 使用 Macport 安裝 MySQLdb.

                For Mac, you can install MySQLdb using Macport.

                2 - 用法

                安裝后,重新啟動.這不是強制性的,但如果出現問題,它會阻止我在這篇文章中回答 3 或 4 個其他問題.所以請重啟.

                After installing, Reboot. This is not mandatory, But it will prevent me from answering 3 or 4 other questions in this post if something goes wrong. So please reboot.

                然后就像使用任何其他包一樣:

                Then it is just like using any other package :

                #!/usr/bin/python
                import MySQLdb
                
                db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                                     user="john",         # your username
                                     passwd="megajonhy",  # your password
                                     db="jonhydb")        # name of the data base
                
                # you must create a Cursor object. It will let
                #  you execute all the queries you need
                cur = db.cursor()
                
                # Use all the SQL you like
                cur.execute("SELECT * FROM YOUR_TABLE_NAME")
                
                # print all the first cell of all the rows
                for row in cur.fetchall():
                    print row[0]
                
                db.close()
                

                當然,有成千上萬種可能性和選擇;這是一個非常基本的例子.您將不得不查看文檔.一個好的起點.

                Of course, there are thousand of possibilities and options; this is a very basic example. You will have to look at the documentation. A good starting point.

                3 - 更高級的用法

                一旦你知道它是如何工作的,你可能想要使用 ORM 來避免手動編寫 SQL 并像操作 Python 對象一樣操作您的表.Python 社區中最著名的 ORM 是 SQLAlchemy.

                Once you know how it works, You may want to use an ORM to avoid writing SQL manually and manipulate your tables as they were Python objects. The most famous ORM in the Python community is SQLAlchemy.

                我強烈建議您使用它:您的生活會輕松得多.

                I strongly advise you to use it: your life is going to be much easier.

                我最近發現了 Python 世界中的另一顆寶石:peewee.這是一個非常精簡的 ORM,設置和使用非常簡單快捷.它讓我在小型項目或獨立應用程序中度過了一天,在使用 SQLAlchemy 或 Django 等大型工具的情況下是過度的:

                I recently discovered another jewel in the Python world: peewee. It's a very lite ORM, really easy and fast to setup then use. It makes my day for small projects or stand alone apps, Where using big tools like SQLAlchemy or Django is overkill :

                import peewee
                from peewee import *
                
                db = MySQLDatabase('jonhydb', user='john', passwd='megajonhy')
                
                class Book(peewee.Model):
                    author = peewee.CharField()
                    title = peewee.TextField()
                
                    class Meta:
                        database = db
                
                Book.create_table()
                book = Book(author="me", title='Peewee is cool')
                book.save()
                for book in Book.filter(author="me"):
                    print book.title
                

                這個例子開箱即用.除了需要 peewee (pip install peewee) 外,什么都不需要.

                This example works out of the box. Nothing other than having peewee (pip install peewee) is required.

                這篇關于如何在 Python 中連接到 MySQL 數據庫?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How to use windowing functions efficiently to decide next N number of rows based on N number of previous values(如何有效地使用窗口函數根據 N 個先前值來決定接下來的 N 個行)
                reuse the result of a select expression in the quot;GROUP BYquot; clause?(在“GROUP BY中重用選擇表達式的結果;條款?)
                Does ignore option of Pyspark DataFrameWriter jdbc function ignore entire transaction or just offending rows?(Pyspark DataFrameWriter jdbc 函數的 ignore 選項是忽略整個事務還是只是有問題的行?) - IT屋-程序員軟件開發技
                Error while using INSERT INTO table ON DUPLICATE KEY, using a for loop array(使用 INSERT INTO table ON DUPLICATE KEY 時出錯,使用 for 循環數組)
                pyspark mysql jdbc load An error occurred while calling o23.load No suitable driver(pyspark mysql jdbc load 調用 o23.load 時發生錯誤 沒有合適的驅動程序)
                How to integrate Apache Spark with MySQL for reading database tables as a spark dataframe?(如何將 Apache Spark 與 MySQL 集成以將數據庫表作為 Spark 數據幀讀取?)
              • <small id='jBRlk'></small><noframes id='jBRlk'>

                      <tbody id='jBRlk'></tbody>
                        <legend id='jBRlk'><style id='jBRlk'><dir id='jBRlk'><q id='jBRlk'></q></dir></style></legend><tfoot id='jBRlk'></tfoot>
                        <i id='jBRlk'><tr id='jBRlk'><dt id='jBRlk'><q id='jBRlk'><span id='jBRlk'><b id='jBRlk'><form id='jBRlk'><ins id='jBRlk'></ins><ul id='jBRlk'></ul><sub id='jBRlk'></sub></form><legend id='jBRlk'></legend><bdo id='jBRlk'><pre id='jBRlk'><center id='jBRlk'></center></pre></bdo></b><th id='jBRlk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='jBRlk'><tfoot id='jBRlk'></tfoot><dl id='jBRlk'><fieldset id='jBRlk'></fieldset></dl></div>
                          <bdo id='jBRlk'></bdo><ul id='jBRlk'></ul>
                          主站蜘蛛池模板: 国产精品一区二区免费 | www.青青草| 欧美一区二区三区在线看 | 二区av| 国产一区二区中文字幕 | 久热国产精品 | 伊久在线 | 蜜桃传媒av | 麻豆一区二区三区精品视频 | 69堂永久69tangcom | 亚洲欧美一区二区在线观看 | 亚洲精品乱码久久久久久按摩观 | 天天拍天天草 | 在线观看日韩 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 精品欧美乱码久久久久久1区2区 | 中文字幕专区 | 亚洲日韩中文字幕一区 | 国产 日韩 欧美 在线 | 在线播放亚洲 | 久久精品一区二区三区四区 | 久久久九九九九 | 一区二区精品 | 粉嫩在线 | 视频1区| 国产欧美一级二级三级在线视频 | 99视频在线 | www精品美女久久久tv | 99久久婷婷 | 午夜电影福利 | 欧美91| 国产精久久久久久 | 91麻豆精品国产91久久久更新资源速度超快 | 在线观看视频91 | 日本一本在线 | 99久久夜色精品国产亚洲96 | 2019精品手机国产品在线 | 黄a网站| 一区二区三区av | 久久久亚洲 | 亚洲一区二区三区免费观看 |