Python 学习入门(四) 连接 MySQL
Python 学习入门(4)—— 连接 MySQL 下载MySQL for Python,最新版MySQL-python-1.2.4b4.tar.gz 1) 提前安装:mysql_config 环境 否则后面 python setup.py build 会提示找不到 “EnvironmentError: mysql_config not found”,安装命令如下: sudo apt-ge
Python 学习入门(4)—— 连接 MySQL下载 MySQL for Python,最新版 MySQL-python-1.2.4b4.tar.gz
1) 提前安装:mysql_config 环境
否则后面 python setup.py build 会提示找不到 “EnvironmentError: mysql_config not found”,安装命令如下:
sudo apt-get install libmysqlclient-dev
2) 然后,再安装MySQLdb
$ tar zxvf MySQL-python-1.2.2.tar.gz
$ cd MySQL-python-1.2.2
$ sudo python setup.py build
$ sudo python setup.py install
3) 验证成功安装
homer@ubuntu:~/myCode/python$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
import MySQLdb 没有出错,说明安装成功!
python 连接mysql示例:
#################### # IT-Homer # 2013-05-10 #################### import MySQLdb db = MySQLdb.connect(host="localhost", user="root", passwd="abcd1234", db="testDB") cursor = db.cursor() cursor.execute("Select * from gameTestDB limit 10") result = cursor.fetchall() for row in result: #print row #print row[0], row[1], row[2] #print '%s, %s, %s' % (row[0], row[1], row[2]) print ', '.join([str(row[0]), str(row[1]), str(row[2])]) cursor.close() ''' import sys import MySQLdb reload(sys) sys.setdefaultencoding('utf-8') db = MySQLdb.connect(user='root', passwd='abcd1234', charset='utf8') cur = db.cursor() cur.execute('use testDB') cur.execute('select * from gameTestDB limit 10') f = file("/home/homer/tmp_mysql.txt", 'w') for row in cur.fetchall(): f.write(str(row)) f.write("\n") f.close() cur.close() '''
#################### # IT-Homer # 2013-05-10 #################### import MySQLdb # local mysql # db = MySQLdb.connect(host="localhost", user="root", passwd="abcd1234", db="testDB") # aws rds mysql db = MySQLdb.connect(host="ithomer.aliyun.com", user="ithomer", passwd="abcd1234", db="dman") cursor = db.cursor() cursor.execute("Select * from score limit 10") result = cursor.fetchall() for row in result: #print row #print row[0], row[1], row[2] #print '%s, %s, %s' % (row[0], row[1], row[2]) print ', '.join([str(row[0]), str(row[1]), str(row[2])]) cursor.close() ''' import sys import MySQLdb reload(sys) sys.setdefaultencoding('utf-8') db = MySQLdb.connect(user='root', passwd='abcd1234', charset='utf8') cur = db.cursor() cur.execute('use testDB') cur.execute('select * from gameTestDB limit 10') f = file("/home/homer/tmp_mysql.txt", 'w') for row in cur.fetchall(): f.write(str(row)) f.write("\n") f.close() cur.close()
参考推荐:
Python 連接 MySQL
MySQLdb User's Guide
Python 字符串操作
mysql_config not found(stackover flow)
python 创建mysql数据库

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL和phpMyAdmin是強大的數據庫管理工具。 1)MySQL用於創建數據庫和表、執行DML和SQL查詢。 2)phpMyAdmin提供直觀界面進行數據庫管理、表結構管理、數據操作和用戶權限管理。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Laravel適合團隊熟悉PHP且需功能豐富的項目,Python框架則視項目需求而定。 1.Laravel提供優雅語法和豐富功能,適合需要快速開發和靈活性的項目。 2.Django適合複雜應用,因其“電池包含”理念。 3.Flask適用於快速原型和小型項目,提供極大靈活性。

在MySQL中,外鍵的作用是建立表與表之間的關係,確保數據的一致性和完整性。外鍵通過引用完整性檢查和級聯操作維護數據的有效性,使用時需注意性能優化和避免常見錯誤。

MySQL和MariaDB的主要區別在於性能、功能和許可證:1.MySQL由Oracle開發,MariaDB是其分支。 2.MariaDB在高負載環境中性能可能更好。 3.MariaDB提供了更多的存儲引擎和功能。 4.MySQL採用雙重許可證,MariaDB完全開源。選擇時應考慮現有基礎設施、性能需求、功能需求和許可證成本。

SQL是一種用於管理關係數據庫的標準語言,而MySQL是一個使用SQL的數據庫管理系統。 SQL定義了與數據庫交互的方式,包括CRUD操作,而MySQL實現了SQL標準並提供了額外的功能,如存儲過程和触發器。
