Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate Mysql in Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces you how to operate Mysql in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Usually the main programming language is Java, development also mainly uses Mysql, often for testing, debugging purposes need to operate the database, such as backup, insert test data, modify test data, sometimes can not simply use SQL to complete the task, or are very good to complete the task, write with Java is a bit too troublesome, I thought of Python. Python syntax is concise, does not need to compile, can be better completed the task. Today, I took a look at the operation of Python to Mysql and made a record. First of all, to install the required environment, Mysql and Python will not talk about the essential things. Mainly the installation of MySQLdb, you can go to sf.net to download, the specific address is http://sourceforge.net/projects/mysql-python/ if you use Ubuntu, after the direct sudo apt-get install python-mysqldb installation is completed, you can test the input Python code import MySQLdb # in the Python interpreter! If you do not report an error, it proves that the installation is successful, and it may continue that MySQLdb in Python is equivalent to MySQL in JAVA. JDBC Driver,Python has a similar data interface specification, Python DB API,MySQLdb, which is the implementation of Mysql. Operation is also relatively simple and other platforms or languages to operate the database is to establish a connection with the database system, and then input SQL to the database, and then get the results from the database. Write the simplest one first. Create a database: Python code #! / usr/bin/env python # coding=utf-8 # # @ author migle # @ date 2010-01-17 # MySQLdb example # # # import MySQLdb # establish a connection to the database system conn = MySQLdb.connect (host='localhost' User='root',passwd='longforfreedom') # get operation cursor cursor = conn.cursor () # execute SQL to create a database. Cursor.execute ("" create database python "") # closes the connection and releases the resource cursor.close () Create databases, create tables, insert data Insert multiple data Python codes #! / usr/bin/env python # coding=utf-8 # # @ author migle # @ date 2010-01-17 # MySQLdb example # # # # import MySQLdb # establish a connection to the database system conn = MySQLdb.connect (host='localhost' User='root',passwd='longforfreedom') # get operation cursor cursor = conn.cursor () # execute SQL to create a database. Cursor.execute ("" create database if not exists python "") # Select the database conn.select_db ('python'); # execute SQL to create a data table. Cursor.execute ("" create table test (id int, info varchar) "") value = [1, "inserted?"]; # insert a record cursor.execute ("insert into test values (% s)", value) Values= [] # generates the insertion parameter value for i in range (20): values.append ((iMagna Hello mysqldb, I am recoder'+ str (I)) # inserts multiple records cursor.executemany ("" insert into test values (% s% s) "", values); # closes the connection and releases the resource cursor.close () The process of query is similar to that of insertion. Just one more step to get the query result: Python code #! / usr/bin/env python # coding=utf-8 # # @ author migle # @ date 2010-01-17 # # # MySQLdb query # # # import MySQLdb conn = MySQLdb.connect (host='localhost' User='root', passwd='longforfreedom',db='python') cursor = conn.cursor () count = cursor.execute ('select * from test') print' has a total of% s records', count # gets a record, and each record as a tuple returns print "get only one record:" result = cursor.fetchone () Print result # print'ID:% s info:% s info:% s'% (result [0], result [1]) print'ID:% s info:% s'% result # get 5 records. Note that since the previous execution has fetchone (), the cursor already points to the second record. That is, all records starting from the second entry print "get only five records:" results = cursor.fetchmany (5) for rin results: print r print "get all the results:" # reset cursor position, 0, offset, mode=absolute | relative, default is relative, cursor.scroll (0PowerMod records) # get all results results = cursor.fetchall () for rin results: print r conn.close ()

On how to operate Mysql in Python to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report