首先确保环境安装了pymysql
没有就安装一下
打开cmd输入pip install pymysql
import pymysql
def connect_sql():
# conn = pymysql.connect(host='localhost',
# user='root',
# password='123456',
# database='awms_test2',
# port=3306,
# charset='utf8')
conn = pymysql.connect(host='192.168.1.210',
user='root',
password='123456',
database='awms',
port=3306,
charset='utf8')
return conn
conn = connect_sql()
print(conn)
cur = conn.cursor()
strsql = "SELECT * FROM sys_pda"
#这个返回时行数
row = cur.execute(strsql)
# 使用 fetchone() 方法获取一条数据
#data = cursor.fetchone()
#获取全部
lists = cur.fetchall()
print(lists)
for lista in lists:
num = lista[0]
name = lista[1]
code = lista[2]
datetime = lista[4]
print(lista)
print('编号为%r的pda名称是%r,它的编码是%r,创建日期是%r' %(num,name,code,datetime))
conn.close()
数组中放字典
arr = []
mapA = {'name':'张三','age':'55'}
mapC = {'name':'李四','age':'61'}
mapD = {'mingzi':'王五','nianling':'99'}
arr.append(mapA)
arr.append(mapC)
arr.append(mapD)
print(arr)
print(arr[0].get('name'))