Implementation of accessing the mysql database using python (Example 2), pythonmysql

Source: Internet
Author: User
Tags mysql backup

Implementation of accessing the mysql database using python (Example 2), pythonmysql

This article describes how to access the mysql database using python. We will share this with you for your reference. The details are as follows:

First install MySQLdb that matches the Python version

Example 1

import MySQLdbconn=MySQLdb.connect(user='root',passwd='123',db='example')cur=conn.cursor()cur.execute("select id,lastname,firstname, date_format(dob,'%Y-%m-%d %H-%i-%s'),phone from employee")##select username,password, date_format(reg_date,'%Y-%m-%d %H-%i-%s') as date from reg_userfor data in cur.fetchall():  print datacur.close()conn.commit()conn.close()

Example 2

Import MySQLdbconn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '') cursor = conn.cursor(export cursor.exe cute ("create database python" Export cursor.exe cute ('use python'your cursor.exe cute ("create table test (id int, content varchar (100 ))") # insert a 100 data record for I in range (1,100): cursor.exe cute ("insert into test values (% s, % s)", under I, 'haha'has been fetched using cursor.exe cute ('select * from test ') results = cursor. fetchall () for r in results print rconn. close ()

I hope this article will help you with Python programming.

Articles you may be interested in:
  • Python implements simple File Transfer and MySQL backup script sharing
  • Use Python to operate MySQL database on 9 practical instances
  • How to compile a Python script for MySQL rollback
  • How to connect Python 3 to the mysql database in linux
  • Some basic methods for using Python to operate MySQL
  • How to operate MySQL in a Python Program
  • Example Analysis of MySQLdb usage in python
  • Analysis of connection between python and MySQL database instances
  • Python MySQLdb Windows Installation tutorial and Solution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.