Home  >  Article  >  Backend Development  >  Introduction to Python's method of reading data from MySQL tables

Introduction to Python's method of reading data from MySQL tables

Y2J
Y2JOriginal
2017-05-05 16:51:411256browse

This article mainly introduces in detail how Python reads MySQL database table data. It has certain reference value. Interested friends can refer to it.

The examples in this article share with you Python reading The specific code to get the MySQL database table data is for your reference. The specific content is as follows

Environment: Python 3.6, Window 64bit

Purpose: Read the target table data from the MySQL database and process

Code:

# -*- coding: utf-8 -*-
 
import pandas as pd
import pymysql
 
## 加上字符集参数,防止中文乱码
dbconn=pymysql.connect(
  host="**********",
  database="kimbo",
  user="kimbo_test",
  password="******",
  port=3306,
  charset='utf8'
 )
 
#sql语句
sqlcmd="select col_name,col_type,col_desc from itf_datadic_dtl_d limit 10"
 
#利用pandas 模块导入mysql数据
a=pd.read_sql(sqlcmd,dbconn)
#取前5行数据
b=a.head()
print(b)
 
 
 
# 读取csv数据
# pd.read_csv()
 
# 读取excel数据
#pd.read_excel()
 
# 读取txt数据
#pd.read_table()

The result is as shown:

[Related recommendations]

1. Python free video Tutorial

2. Python Learning Manual

3. Python Object-Oriented Video Tutorial

The above is the detailed content of Introduction to Python's method of reading data from MySQL tables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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