<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                MySQL是Web世界中使用最廣泛的數據庫服務器。SQLite的特點是輕量級、可嵌入,但不能承受高并發訪問,適合桌面和移動應用。而MySQL是為服務器端設計的數據庫,能承受高并發訪問,同時占用的內存也遠遠大于SQLite。 此外,MySQL內部有多種數據庫引擎,最常用的引擎是支持數據庫事務的InnoDB。 ### 安裝MySQL 可以直接從MySQL官方網站下載最新的[Community Server 5.6.x](http://dev.mysql.com/downloads/mysql/5.6.html)版本。MySQL是跨平臺的,選擇對應的平臺下載安裝文件,安裝即可。 安裝時,MySQL會提示輸入`root`用戶的口令,請務必記清楚。如果怕記不住,就把口令設置為`password`。 在Windows上,安裝時請選擇`UTF-8`編碼,以便正確地處理中文。 在Mac或Linux上,需要編輯MySQL的配置文件,把數據庫默認的編碼全部改為UTF-8。MySQL的配置文件默認存放在`/etc/my.cnf`或者`/etc/mysql/my.cnf`: ~~~ [client] default-character-set = utf8 [mysqld] default-storage-engine = INNODB character-set-server = utf8 collation-server = utf8_general_ci ~~~ 重啟MySQL后,可以通過MySQL的客戶端命令行檢查編碼: ~~~ $ mysql -u root -p Enter password: Welcome to the MySQL monitor... ... mysql> show variables like '%char%'; +--------------------------+--------------------------------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.1.65-osx10.6-x86_64/share/charsets/ | +--------------------------+--------------------------------------------------------+ 8 rows in set (0.00 sec) ~~~ 看到`utf8`字樣就表示編碼設置正確。 ### 安裝MySQL驅動 由于MySQL服務器以獨立的進程運行,并通過網絡對外服務,所以,需要支持Python的MySQL驅動來連接到MySQL服務器。MySQL官方提供了mysql-connector-python驅動,但是安裝的時候需要給pip命令加上參數`--allow-external`: ~~~ $ pip install mysql-connector-python --allow-external mysql-connector-python ~~~ 我們演示如何連接到MySQL服務器的test數據庫: ~~~ # 導入MySQL驅動: >>> import mysql.connector # 注意把password設為你的root口令: >>> conn = mysql.connector.connect(user='root', password='password', database='test') >>> cursor = conn.cursor() # 創建user表: >>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))') # 插入一行記錄,注意MySQL的占位符是%s: >>> cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael']) >>> cursor.rowcount 1 # 提交事務: >>> conn.commit() >>> cursor.close() # 運行查詢: >>> cursor = conn.cursor() >>> cursor.execute('select * from user where id = %s', ['1']) >>> values = cursor.fetchall() >>> values [('1', 'Michael')] # 關閉Cursor和Connection: >>> cursor.close() True >>> conn.close() ~~~ 由于Python的DB-API定義都是通用的,所以,操作MySQL的數據庫代碼和SQLite類似。 ### 小結 * 執行INSERT等操作后要調用`commit()`提交事務; * MySQL的SQL占位符是`%s`。 ### 參考源碼 [do_mysql.py](https://github.com/michaelliao/learn-python3/blob/master/samples/db/do_mysql.py)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看