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数据库

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...
