Home Database Mysql Tutorial Python连接MySQL及对其操作

Python连接MySQL及对其操作

Jun 07, 2016 pm 03:25 PM
mysql python operate connect

论文实验涉及,主要是记个笔记,见谅!下面是测试代码(实现数据库、表建立、插入数据、查询数据、更新数据): # -*- coding: cp936 -*-import MySQLdbtry: conn=MySQLdb.connect(host=localhost,user=root,passwd=19900216) #conn=MySQLdb.connect(host=lo

论文实验涉及,主要是记个笔记,见谅!下面是测试代码(实现数据库、表建立、插入数据、查询数据、更新数据):

# -*- coding: cp936 -*-
import MySQLdb


try:
    conn=MySQLdb.connect(host='localhost',user='root',passwd='19900216')
    #conn=MySQLdb.connect(host='localhost',user='root',passwd='19900216',db='world')
    #print "conn=",conn;
    cur=conn.cursor()
    #print "cur=",cur;
    cur.execute('create database if not exists python')
    conn.select_db('python')
    cur.execute('create table test(id int,info varchar(20))')
    value=[1,'hi rollen']
    cur.execute('insert into test values(%s,%s)',value)
    
    values=[]
    for i in range(9):
        values.append((i,'hi rollen'+str(i)))

    cur.executemany('insert into test values(%s,%s)',values)
    conn.commit()
    test=cur.execute('select * from test')
    print test #记录条数
    result=cur.fetchone()
    print 'ID: %s info %s' %result
    results=cur.fetchmany(5)
    for r in results:
        print r

    cur.scroll(0,mode='absolute')

    results=cur.fetchall()
    for r in results:
        print r[1]
    cur.execute('update test set info="I am rollen" where id=3')

    conn.commit()
    cur.close()
    conn.close()
except MySQLdb.Error,e:
    print "Mysql Error %d: %s" %(e.args[0],e.args[1])
Copy after login


代码注释:

代码中有很多类似xxxone、xxxmany,xxxmany的函数,作用比较好懂!

scroll(self,value,mode):移动指针到某一行。如果mode=‘relative’,则表示从当前所在行移动value条,如果mode=‘absolute’,表示从结果集的第一行移动value条


Q&A:

①Q:Mysql Notifier无法start mysql服务

A:进入Manage Items,删除服务后,再重新添加服务


②Q提示python、mysql不是内部或外部命令……,A:添加变量到PATH;


③Q装MySQL-python时,出现错误.一般是因为缺少visual c++的东西,或者安装的是64位的python

A:安装提示所缺少的东西后,再安装MySQL-python;或者尝试安装MySQL-python-1.2.5.win-amd64-py2.7



参考资料:

http://blog.csdn.net/lishan9133/article/details/7024989

http://www.cr173.com/html/27032_1.html

http://www.cnblogs.com/fnng/p/4115607.html

http://blog.sina.com.cn/s/blog_82a927880102uwty.html

http://www.crifan.com/python_install_module_mysql_mysqldb/

http://www.cnblogs.com/rollenholt/archive/2012/05/29/2524327.html



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

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.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

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 vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

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 vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

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.

Does Python projects need to be layered? Does Python projects need to be layered? Apr 19, 2025 pm 10:06 PM

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...

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles