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